2013-03-09 5 views
0

score이라는 모델과 scores이라는 컬렉션이 있습니다. 그러나 모델은 부모 컬렉션의 localStorage 속성 (또는 모든 속성)을 상속하지 않는 것 같습니다. 내가 여기서 뭔가를 놓치고 있니?Backbone.js 모델이 컬렉션에서 상속받지 않습니다.

요구 사항이있는 백본을 실행 중입니다.

모델//

define([ 
    'underscore', 
    'backbone', 
    'localstorage' 
], function(_, Backbone, Store){ 
    var ScoreModel = Backbone.Model.extend({ 
     defaults: { 
      board_id: null, 
      ns_pair: null, 
      ew_pair: null, 
      ns_score: null 
     }, 
     validate: function(attrs, options){ 
      if(isNaN(attrs.board_id) || attrs.board_id < 1){ 
       return 'Invalid Board ID!'; 
      } 
     }, 
     localStorage: new Store("ScoreCollection") 
    }); 
    return ScoreModel; 
}); 

컬렉션을 score.js

define([ 
    'underscore', 
    'backbone', 
    'models/score', 
    'localstorage' 
], function(_, Backbone, ScoreModel, Store){ 
    var ScoreCollection = Backbone.Collection.extend({ 
     model: ScoreModel, 
     localStorage: new Store("ScoreCollection") 
    }); 
    return ScoreCollection; 
}); 

main.js

require.config({ 
    paths: { 
    // Major libraries 
    jquery: 'libs/jquery/jquery.min', 
    underscore: 'libs/underscore/underscore.min', 
    backbone: 'libs/backbone/backbone.min', 

    // Require.js plugins 
    text: 'libs/require/text', 

    // Backbone.js plugins 
    localstorage: 'libs/backbone/localstorage', 

    // Just a short cut so we can put our html outside the js dir 
    // When you have HTML/CSS designers this aids in keeping them out of the js directory 
    templates: '../templates' 
    } 
}); 

// Let's kick off the application 

require([ 
    'app' 
], function(App){ 
    App.initialize(); 
}); 
+0

Backbone.Collection 및 Backbone.Model 외에 다른 것을 상속받은 데이터가 표시되지 않습니다. 또한 귀하의 모델에 localStorage를 선언 할 필요가 없다고 생각합니다. – snedkov

+0

@svetoslavnedkov 문제는 모델에서 선언하지 않으면 localStorage가 작동하지 않는다는 것입니다. 그것은해야하지만, 그렇지 않습니다. – mushroom

+0

작동하지 않는 코드를 게시 할 수 있습니까? – snedkov

답변

0

백본 모델 scores.js s는 백본 컬렉션을 상속받지 않습니다. 그것들은 기본 Backbone 모델의 확장 일뿐입니다. 자신의 속성과 메서드로 확장하는 모델입니다. 컬렉션, 같은 거래. 컬렉션의 모델이 정의한 특정 모델을 기반으로 지정하여 컬렉션을 추가 할 때 모델 생성자를 사용하여 해당 컬렉션의 각 모델에 대한 인스턴스를 만들 수 있지만 직접 상속 관계는 없습니다. 필요에 따라 컬렉션의 인스턴스가되는 모델에서 속성을 정의 할 수 있습니다.

관련 문제