Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置主干模型';s基于集合属性的默认值_Javascript_Backbone.js - Fatal编程技术网

Javascript 如何设置主干模型';s基于集合属性的默认值

Javascript 如何设置主干模型';s基于集合属性的默认值,javascript,backbone.js,Javascript,Backbone.js,我需要所有新的MenuItem模型都具有父集合中的菜单属性。 下面是一个不起作用的基本示例(因为在MenuItem的defaults函数中未定义this.collection) 我已经通过重写集合的create方法解决了这个问题,我仍然不确定这是否是正确的方法 create: function(attributes, options) { return Backbone.Collection.prototype.create.call( this, _.extend({men

我需要所有新的MenuItem模型都具有父集合中的菜单属性。 下面是一个不起作用的基本示例(因为在MenuItem的defaults函数中未定义this.collection)


我已经通过重写集合的create方法解决了这个问题,我仍然不确定这是否是正确的方法

create: function(attributes, options) {
  return Backbone.Collection.prototype.create.call(
    this,
    _.extend({menu: this.name}, attributes),
    options
  );
}

对于新车型的各种可能性:

  • 裸对象或模型实例
  • 通过、、或添加
我发现在undocumented collection函数中的挂钩工作得很好

一般的收藏 此集合可以原样用于替换默认主干集合。它补充道

  • 一个新的要覆盖的
    onNewModel
    函数,用于接收新模型实例和选项
  • 发送相同数据的自定义
    新型号
    事件
您自己的收藏可以是:

var Menu = Collection.extend({
    model: MenuItem,
    initialize: function(models, options) {
        this.name = options.name;
    }
    onNewModel: function(model, options) {
        model.set({ menu: model.get('menu') || this.name });
    },
});
可以保证
model
onNewModel
中的主干
model
实例

概念证明
//将泛型集合放入文件中,并在项目中包含一次。
var Collection=Backbone.Collection.extend({
/**
*钩住本机的prepareModel以提供标准钩子
*将新模型添加到集合中时。
*/
_prepareModel:函数(模型、选项){
model=Collection.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
if(型号){
此.onNewModel(模型、选项);
此.trigger('new-model',model,options);
}
收益模型;
},
//将新模型添加到集合时调用。
onNewModel:uu.noop,
});
//从通用集合扩展到您自己的集合。
var Menu=Collection.extend({
初始化:功能(模型、选项){
this.name=options.name;
},
onNewModel:函数(模型、选项){
模型集({
菜单:model.get('menu')| | this.name
});
console.log(“onNewModel菜单:”,model.get(“菜单”);
},
});
//然后使用它
var菜单=新菜单([
//使用裸对象
{
标题:“第页”,
url:“/页”
},
//或模型实例
新主干。模型({
标题:“其他页面”
})
], {
名称:“页脚”//collection选项
});
//如果需要,请收听自定义事件
主干.listenTo(菜单“新型号”,功能(型号,选项){
log(“'new-model'触发),model.get('title');
});
//或其他收集方法
菜单。添加({
标题:“第页”,
菜单:“不会被覆盖”
});


这是一个有限的答案,因为
attributes
param可能是一个模型实例,而不是一个原始对象。此外,它还缺少使用
set
add
添加的模型。集合应使用
new菜单(null,{name:'footer})
实例化,init应为
initialize:function(models,options)
,以遵循主干的约定。
var Collection = Backbone.Collection.extend({
    /**
     * Hook into the native _prepareModel to offer a standard hook
     * when new models are added to the collection.
     */
    _prepareModel: function(model, options) {
        model = Collection.__super__._prepareModel.apply(this, arguments);
        if (model) {
            // call our new custom callback
            this.onNewModel(model, options);
            // trigger a new custom event
            this.trigger('new-model', model, options);
        }
        return model;
    },

    // Called when adding a new model to the collection.
    onNewModel: _.noop,
});
var Menu = Collection.extend({
    model: MenuItem,
    initialize: function(models, options) {
        this.name = options.name;
    }
    onNewModel: function(model, options) {
        model.set({ menu: model.get('menu') || this.name });
    },
});