本文介绍了骨干收集sortBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我的第一个应用程序的骨干,并得到一些问题的收集整理。
使用此之后

  VAR SortedFriends = MyFriends.sortBy(功能(朋友){
          返回friend.get(UID);
        });

执行console.log(SortedFriends)显示,SortedFriends包含排序模型,但是当我尝试使用收藏功能,如SortedFriends.each'或'SortedFriends.at使其错误:

类型错误:SortedFriends.each不是一个函数。

code:

  VAR好友= Backbone.Model.extend({});
      VAR友= Backbone.Collection.extend({
        型号:朋友,
      });      VAR MyFriends =新朋友();
      MyFriends.reset(小于?= $朋友和GT;?);      VAR FriendView = Backbone.View.extend({
          初始化:功能(){
              型号:朋友
          },
          标签名:TR,
          模板:_.template($('#项目模板)HTML()。)
          产品类别:文档行
          渲染:功能(){         这$ el.html(this.template(this.model.toJSON()));
              返回此;
          }
      });
    VAR SortedFriends = MyFriends.sortBy(功能(朋友){
      返回friend.get(UID);
    });    VAR addOne =功能(元素){
        VAR视图=新FriendView({模式:元素});
        $(#的朋友)追加(view.render()EL)。
    }
    的console.log(JSON.stringify(SortedFriends));
    SortedFriends.each(功能(朋友){
        VAR视图=新FriendView({模式:朋友});
        $(#的朋友)追加(view.render()EL)。
    });


解决方案

如果你使用的骨干集合那么你是可能更好使用比较,而不是收集方法

当你准备进行排序您的收藏:

  MyFriends.comparator =功能(朋友){
   返回friend.get(UID);
});
MyFriends.sort();

或者如果你想保持未分类收集的顺序,那么你需要先克隆它

  VAR SortedFriends = MyFriends.clone();
SortedFriends.comparator =功能(朋友){
   返回friend.get(UID);
});
SortedFriends.sort();

I make my first backbone app and get some problems with collection sorting.After using this

var SortedFriends = MyFriends.sortBy(function(friend) {
          return friend.get("uid");
        });

console.log(SortedFriends) show that SortedFriends contains sorted models, but when i try to use collection functions like 'SortedFriends.each' or 'SortedFriends.at' it make error:

TypeError: SortedFriends.each is not a function.

Code:

  var Friend = Backbone.Model.extend({});
      var Friends = Backbone.Collection.extend({
        model: Friend,
      });

      var MyFriends = new Friends();
      MyFriends.reset(<?=$friends?>);

      var FriendView = Backbone.View.extend({
          initialize: function(){
              model:Friend
          },
          tagName: "tr",
          template: _.template($('#item-template').html()),
          className: "document-row",
          render: function() {

         this.$el.html(this.template(this.model.toJSON()));
              return this;
          }
      });


    var SortedFriends = MyFriends.sortBy(function(friend) {
      return friend.get("uid");
    });

    var addOne = function(element){
        var view = new FriendView({model: element});
        $("#friends").append(view.render().el);
    }
    console.log(JSON.stringify(SortedFriends));
    SortedFriends.each(function(friend){
        var view = new FriendView({model: friend});
        $("#friends").append(view.render().el);
    });
解决方案

If youre using backbone collections then youre probably better off using the comparator rather than collection methods

http://backbonejs.org/#Collection-comparator

When youre ready to sort your collection:

MyFriends.comparator = function(friend){
   return friend.get("uid");
});
MyFriends.sort();

OR if you want to keep the order of the unsorted collection then you will need to clone it first

http://backbonejs.org/#Collection-clone

var SortedFriends = MyFriends.clone();
SortedFriends.comparator = function(friend){
   return friend.get("uid");
});
SortedFriends.sort();

这篇关于骨干收集sortBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:46