我有一个名为score
的模型和一个名为scores
的集合。但是,该模型似乎不是从父集合继承localStorage
属性(或者就此而言,任何属性)。我在这里错过了什么吗?
使用RequireJS运行Backbone。
模型/ score.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;
});
集合/ scores.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 :(得分:0)
Backbone模型不会从Backbone Collections继承。它们只是您使用自己的属性和方法'扩展'的基础Backbone.Model的扩展。收藏,同样的交易。您可以指定集合的模型基于您定义的特定模型,以便在将集合添加到集合时,它使用模型构造函数为该集合中的每个模型创建实例,但那里没有直接继承关系。如果符合您的需要,您可以在模型上定义恰好是集合实例的属性。