1

我有这样的事情:

var getArticles = function one() {

return articles.find({}, {sort: {'published': -1, 'votes': -1}, limit: 100});

};

我想将排序顺序从“已发布”更改为“投票”。我认为你可以这样做:

 Template.mytemplate.events({
 'click .sort_by_votes': function () {

Session.set('order', 'votes');
return getArticles();

 } 
});

但我没有运气。任何人都知道如何做到这一点?

4

1 回答 1

2

You have to use Session.get or Session.equals and make sure you dont split the helper functions and event functions

e.g

Template.mytemplate.events({
    'click .sort_by_votes': function () {
        Session.set('order', 'votes');
    }
});

then your template helper

Template.mytemplate.articles = function() {
    var sort = {published:-1, votes:-1};

    if(Session.equals("order", "votes")) sort["votes"] = 1

    articles.find({}, {sort: sort, limit: 100});
}

This is to hook up to your mytemplate template:

<template name="mytemplate">
    {{#each articles}}
         ...
    {{/each}}
</template>
于 2013-05-08T07:45:19.333 回答