最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting simple events in meteor - Stack Overflow

programmeradmin2浏览0评论

I'm trying out Leaderboard's example in Meteor but i'm doing something wrong in setting the click event. In this example, I have three buttons, one to change the sort by column, another to add 5 bonus points to everyone.

Here's the html:

    <div id="outer">
    {{> sorter}}
    {{> leaderboard}}
  </div>
   <template name="sorter">
   <span>Sorted by {{sortedBy}}</span>
   {{#if sortByName}}
    <input type="button" id="sortScore" value="sort by score" />
  {{else}}
    <input type="button" id="sortName" value="sort by name" />
  {{/if}}

    <input type="button" class="incAll" value="5 bonus points to all" />

</template>

And here's the js:

Template.sorter.events = {
'click #sortName': function(){
    Session.set('orderby', 'name');
},
'click #sortScore': function(){
    Session.set('orderby', 'score');
},
'click input.incAll': function(){
  Players.find().forEach(function(player){
      Players.update(player._id, {$inc: {score: 5}});
  });
}

}

Calling Session.set('orderby', 'name'); in the console works and updates the html accordingly but clicking in the buttons doesn't. So what am I missing?

Thanks

I'm trying out Leaderboard's example in Meteor but i'm doing something wrong in setting the click event. In this example, I have three buttons, one to change the sort by column, another to add 5 bonus points to everyone.

Here's the html:

    <div id="outer">
    {{> sorter}}
    {{> leaderboard}}
  </div>
   <template name="sorter">
   <span>Sorted by {{sortedBy}}</span>
   {{#if sortByName}}
    <input type="button" id="sortScore" value="sort by score" />
  {{else}}
    <input type="button" id="sortName" value="sort by name" />
  {{/if}}

    <input type="button" class="incAll" value="5 bonus points to all" />

</template>

And here's the js:

Template.sorter.events = {
'click #sortName': function(){
    Session.set('orderby', 'name');
},
'click #sortScore': function(){
    Session.set('orderby', 'score');
},
'click input.incAll': function(){
  Players.find().forEach(function(player){
      Players.update(player._id, {$inc: {score: 5}});
  });
}

}

Calling Session.set('orderby', 'name'); in the console works and updates the html accordingly but clicking in the buttons doesn't. So what am I missing?

Thanks

Share Improve this question edited Apr 11, 2012 at 22:40 scc asked Apr 11, 2012 at 19:51 sccscc 10.7k10 gold badges53 silver badges67 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

Event maps with selectors won't match top-level elements in a template. This is something we will fix ASAP.

There's an easy workaround though. Wrap your sorter template in a <div>.

http://docs.meteor.com/#eventmaps

发布评论

评论列表(0)

  1. 暂无评论