문제

In my backbone.marionette app, I am trying to set a model attribute 'rank' as it's loop index. here is the code of my collection:

 AngryCats = Backbone.Collection.extend({
          model:AngryCat,
          initialize: function(cats){
              var rank = 1;
              _.each(cats, function(cat){
                    cat.set('rank', rank);
                  rank++;
              })
          }
      });

But I am getting error as :

TypeError: cat.set is not a function
cat.set('rank', rank);

any one tell me what is wrong here? ( please check the fiddle link for complete coding )

Live Demo

도움이 되었습니까?

해결책

You are passing array of javascript objects. But the set is only available in Backbone.Model instance. Only after initialisation, every single object converted into Backbone.Model Object.

You have to do like

 var rank = 1;
 _.each(cats, function(cat){
     cat.rank = rank;
     rank++;
  })
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top