0

新流星用户。

想要修改故事大小工具的排行榜示例,团队中的成员可以同时对故事进行评分。

非常类似于排行榜,但希望为管理员用户添加一个标志/事件/按钮,以便能够打开和关闭显示的排行榜。

这是我对此的微弱尝试-

// Set up a collection to contain member information. On the server,'

// it is backed by a MongoDB collection named "members".

Members = new Meteor.Collection("members");
Flags = new Meteor.Collection("Flags");


if (Meteor.isClient) {
    Meteor.startup(function() {

    Session.set("viewall", false);
  });
  Template.leaderboard.members = function () {
  if (Session.get("viewall"))
  {
    return Members.find({}, {sort: {score: -1, name: 1}});
  }

  };

  Template.size.members = function() {
  return Members.find({}, {sort: {name: 1}});
  };

  Template.size.events ({
    'click input.submit': function(){
        var memname = document.getElementById('select_member').value;
        //alert(memname);
        var member = Members.findOne({_id: memname});
        if(member._id)
        {
        Session.set("selected_player", member._id);
            //alert(member.name);
        }
        var memsize = document.getElementById('select_size').value;
        alert(memsize);
        Members.update(Session.get("selected_player"), {$set: {score: memsize}});
    }
  });

  Template.leaderboard.isAdmin = function() {
    var member=Members.findOne(Session.get("selected_player"));
    var  memtype = member.utype;
    if (memtype=== "admin")
    {
        return true;
    }
    else
    {   
        return false;
    }
  };
  Template.leaderboard.selected_name = function () {
    var member = Members.findOne(Session.get("selected_player"));
    return member && member.name;
  };

  Template.leaderboard.viewAll = function() {
    var flag = Flags.findOne({name:"showAll"});
    if (flag)
    {
    alert("View All flag set for current user : " + flag.value);
    return flag && flag.value;
    }
    else return false;
 };


  Template.leaderboard.selected_size = function () {
    var member = Members.findOne(Session.get("selected_player"));
    return member && member.score;
  };


  Template.member.selected = function () {
    return Session.equals("selected_player", this._id) ? "selected" : '';
  };

  Template.leaderboard.events({
    'click input.inc1': function () {
        alert('setting it to 1');
        updatePrevScore();
    //  Members.find({_id: Session.get("selected_player")}).foreach(function(doc){
    //  doc.prev_score = doc.score;
    //  Member.save(doc);
    //  });
        //Members.update(Session.get("selected_player"), {$set: {prev_score: score}});
      Members.update(Session.get("selected_player"), {$set: {score: 1}});
    },
    'click input.inc2': function () {
    updatePrevScore();
      Members.update(Session.get("selected_player"), {$set: {score: 2}});
    },
    'click input.inc3': function () {
    updatePrevScore();
      Members.update(Session.get("selected_player"), {$set: {score: 3}});
    },
    'click input.inc5': function () {
    updatePrevScore();
      Members.update(Session.get("selected_player"), {$set: {score: 5}});
    },
    'click input.inc8': function () {
    updatePrevScore();
      Members.update(Session.get("selected_player"), {$set: {score: 8}});
    },
    'click input.inc13': function () {
    updatePrevScore();
      Members.update(Session.get("selected_player"), {$set: {score: 13}});
    },
    'click input.inc20': function(){
    updatePrevScore();
    Members.update(Session.get("selected_player"),{$set: {score:20}});
    },
    'click input.reset': function(){
     if (confirm('Are you sure you want to reset the points?')) {
        resetScores();  
        }
    },
    'click input.showAll': function() {
            setFlag();
    },
    'click input.hideAll': function() {
            resetFlag();
    }

  });



  Template.member.events({
    'click': function () {
      Session.set("selected_player", this._id);
    }
  });


function resetFlag() {
    Meteor.call("resetFlag", function(error, value) {
    Session.set("viewall", false);
    });
};

function setFlag() {
    Meteor.call("setFlag", function(error, value) {
    Session.set("viewall", true);
    });
};

function resetScores() {
        //alert('resetting scores');
                Members.find().forEach(function (member) {
                Members.update(member._id, {$set: {prev_score: member.score}});
                Members.update(member._id, {$set: {score: 0}});
  });
    Session.set("selected_player", undefined);
};

function updatePrevScore() {
        //alert('resetting scores');
         Members.find().forEach(function (member) {
         if (member._id === Session.get("selected_player"))
         {
                Members.update(member._id, {$set: {prev_score: member.score}});
                Members.update(member._id, {$set: {score: 0}});
                }
  });

};
}
// On server startup, create some members if the database is empty.
if (Meteor.isServer) {

    Members.remove({});
    Meteor.startup(function () {
    if (Members.find().count() === 0) {
      var names = ["Member 1",
      "Member 2",
                   "Member 3",
                   "Member 4",
                   "Member 5"
                  ];
      var type; 
      for (var i = 0; i < names.length; i++)
        {

            if (i===0)
                type = "admin";
            else
                type="user";

            Members.insert({name: names[i], score: 0, prev_score:0, utype:type});
        }

    }
    else resetScores();

    if (Flags.find().count() === 0) {
        Flags.insert({name: "showAll", value: false});
    }
    else Flags.update({name:"showAll"}, {$set: {value:false}});

  }



  );
  Meteor.methods({
    setFlag: function() {
            Flags.update({name:"showAll"}, {$set: {value:true}});
            console.log("Updated flag to true");
            return true;
            },
    resetFlag: function() {
      Flags.update({name:"showAll"}, {$set: {value:false}});
      console.log("Updated flag to false");
        return false;
            },

    }
  );

HTML =>

<head>
  <title>Story Points Exercise</title>
</head>

<body>
   <div id="story_id">
   Story Sizing for Story ID: 78972
   </div>



  <div id="outer">
    {{> leaderboard}}
  </div>
</body>

<template name="size">

    <div class="select_member">
    <select id="select_member"> 
    {{#each members}}
    <option value={{_id}}> {{name}} </option>
    {{/each}}
    </select>
    </div>
    <div class="select_size">

    <select id="select_size"> Select Size:
    <option value=1>1</option>
    <option value=2>2</option>
    <option value=3>3</option>
    <option value=5>5</option>
    <option value=8>8</option>
    <option value=13>13</option>
    <option value=20>20</option>
    </select>
    </div>
    <div class="submitbutton">
    <input type="button" class="submit" value="Submit" />
    </div>

</template>

<template name="leaderboard">

<div class="leaderboard">

  {{#if selected_name}}



  <div class="details">
    <div class="name">{{selected_name}}</div>
    <div class="size">{{selected_size}}</div>

    <div class="details">
    <input type="button" class="inc1" value="1 points" />
    <input type="button" class="inc2" value="2 points" />
    <input type="button" class="inc3" value="3 points" />
    <input type="button" class="inc5" value="5 points" />
    <input type="button" class="inc8" value="8 points" />
    <input type="button" class="inc13" value="13 points" />
    <input type="button" class="inc20" value="20 points" />
    </div>

    {{#if isAdmin}}
        <input type="button" class="showAll" value="showAll" />
        <input type="button" class="hideAll" value="hideAll" />
        <input type="button" class="reset" value="Reset"/>
    {{/if}}


  {{#if viewAll}}

    {{#each members}}
      {{> member}}
    {{/each}}

  {{/if}}
  </div>

  {{/if}}

  {{#unless selected_name}}

  {{>size}}
  {{/unless}}


  </div>
</template>

<template name="member">
  <div class="member {{selected}}">
    <span class="name">{{name}}</span>
    <span class="score">{{score}}</span>
    <span class="prevscore">{{prev_score}}</span>
  </div>
</template>

它适用于一个浏览器和一个用户(管理员类型能够为模板启用 viewAll 标志以显示所有成员。)但是,不适用于多个用户。

因此,如果我打开浏览器,选择我的姓名和故事大小并且我是管理员,我单击提交 - 我看到用于更改故事大小以及显示全部、隐藏全部和重置排行榜的按钮。

当我点击 showAll 时,我作为管理员可以看到排行榜。但其他用户无法看到排行榜。我验证了该用户的客户端正在接收更改显示事件(showAll flag=true)。

有任何想法吗?

4

1 回答 1

0

我能够通过使用一个名为 Views 的新集合并观察一个名为 showAll 的特定文档来解决这个问题。

showAll 设置为仅由管理员更新。

它通过 {{#if}} 连接到模板视图。

于 2013-06-27T19:44:53.827 回答