4

初めてバックボーン アプリを作成しましたが、コレクションの並べ替えで問題が発生しました。これを使用した後

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

console.log(SortedFriends) は、SortedFriends にソートされたモデルが含まれていることを示していますが、「SortedFriends.each」や「SortedFriends.at」などのコレクション関数を使用しようとすると、エラーが発生します:

TypeError: SortedFriends.each は関数ではありません。

コード:

  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);            
    });
4

2 に答える 2