Javascript 在按钮上对模板表中的集合排序按backboneJS

Javascript 在按钮上对模板表中的集合排序按backboneJS,javascript,backbone.js,backbone.js-collections,Javascript,Backbone.js,Backbone.js Collections,我想对集合进行排序,然后使用backboneJS进行渲染 模板为 <script type="text/template" id="user-list-template"> <a href="#" class="btn btn-primary" id="by-name">Sort By Name</a> <a href="#" class="btn btn-primary" id="by-salary">Sort by Salary

我想对集合进行排序,然后使用backboneJS进行渲染

模板为

<script type="text/template" id="user-list-template">
    <a href="#" class="btn btn-primary" id="by-name">Sort By Name</a>
    <a href="#" class="btn btn-primary" id="by-salary">Sort by Salary</a>
    <a href="#" class="btn btn-primary" id="by-hire-date">Sort by Hire Date</a>

    <table class="table stripped">
        <thead>
            <tr>
                <th>Employee Number</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Hire Date</th>
                <th>Salary</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <% _.each(users, function(user){ %>
                <tr>
                    <td><%= user.get('empNumber') %></td>
                    <td><%= user.get('firstName') %></td>
                    <td><%= user.get('lastName') %></td>
                    <td><%= user.get('hireDate') %></td>
                    <td><%= user.get('salary') %></td>
                    <td></td>
                </tr>
            <% }); %>
        </tbody>
    </table>

</script>

员工编号
名字
姓
租用日期
薪水
然后是脚本中执行FECTCH的部分

$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
        options.url = 'http://localhost:61855/api' + options.url;
    });


    var Users = Backbone.Collection.extend({
        sort_key: 'empNumber',
        url: '/UserModel',
        initialize: function() {
        },
        comparator: function(item) {
            return item.get(this.sort_key);
        },
        sortByField: function(fieldName) {
            this.sort_key = fieldName;
            this.sort();
        }
    });



    var UserList = Backbone.View.extend({
        el: '.page',
        render: function () {
            var that = this;
            var users = new Users();
            users.fetch({
                success: function (users) {
                    var template = _.template($('#user-list-template').html(), { users: users.models });
                    that.$el.html(template);
                }
            })
        }
    });

    var Router = Backbone.Router.extend({
        routes: {
            '': 'home'
        }
    });
    var userList = new UserList();

    var router = new Router();

    router.on('route:home', function () {
        userList.render();
    });

    Backbone.history.start();
</script>
$.ajaxPrefilter(函数(选项、原始选项、jqXHR){
options.url=http://localhost:61855/api“+options.url;
});
var Users=Backbone.Collection.extend({
排序键:'empNumber',
url:“/UserModel”,
初始化:函数(){
},
比较:功能(项目){
return item.get(此.sort_键);
},
sortByField:函数(字段名){
this.sort_key=字段名;
this.sort();
}
});
var UserList=Backbone.View.extend({
el:“.page”,
渲染:函数(){
var=这个;
var users=新用户();
users.fetch({
成功:功能(用户){
var template=35;.template($('#用户列表模板').html(),{users:users.models});
即.$el.html(模板);
}
})
}
});
var Router=Backbone.Router.extend({
路线:{
“家”
}
});
var userList=new userList();
var router=新路由器();
路由器.on('route:home',函数(){
render();
});
Backbone.history.start();

我对backboneJS是个十足的傻瓜。基本上在一个按钮点击我想按该类型渲染。我假设我需要处理某种单击事件。有人能给我指出正确的方向吗

下面的代码应该可以工作

  var UserList = Backbone.View.extend({
    initialize: function(){
        var that = this;
        var users = new Users();
        this.users = users;
        this.users.sort_key = default_sort_key;
        users.on('reset', this.render);
        users.fetch({
            success: function (usersResp) {
                users.reset(usersResp);
            }
        })
    },
    el: '.page',
    events:{
      'click th':'headerClickHandler'
    },
    headerClickHandler: function(){
       //figure out key to be sorted on;
       this.users.sort_key = key_figured_out_from_th_clicked;
       this.render(); 
    },

    render: function () {
        var users = this.users;
        var template = _.template($('#user-list-template').html(), { users: users.models });
        that.$el.html(template);
        var that = this;

    }
});