什么是设置在骨干parent属性的正确方法? [英] What's the proper way to set a parent property in Backbone?

查看:118
本文介绍了什么是设置在骨干parent属性的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,该文件将创建我的 ParentModel 以及填充子记录集合。

I have a JSON file that will create my ParentModel as well as populate the child Records collection.

ParentModel : Backbone.Model.extend({
  initialize: function() {
    this.set({ records: new Records(this.get("records")) });
  }
});

和的记录集只是一个基本的骨干集合映射到一个记录模式。

And the Records collection is just a basic Backbone collection that maps to a Record model.

问题是我需要的孩子了解父母,所以每个录制模型对它有一个parent属性。所以现在我刚刚加入这初始化方法的底部:

The problem is that I need the child to know about the parent, so each Record model has to have a parent property on it. So for now I've just been adding this to the bottom of the initialize method:

var self = this;
this.get("records").each(function(record) {
  record.set("parent", self);
});

这工作得很好,但是当我创建一个新的记录我宁愿不记得要包括4行。

This works fine, but when I'm creating a new record I'd rather not have to remember to include those 4 lines.

这个答案说,我可以重写initialize方法采取额外的参数,但我不太清楚如何我会得到骨干在ParentModel自动传递到重写initialize方法。任何人都可以提供关于如何做到这一点的例子吗?

This answer says I can override the initialize method to take in additional parameters, but I'm not quite sure how I would get Backbone to automatically pass in the ParentModel to the overridden initialize method. Can anyone provide an example on how to do that?

我听说的骨干关系这可能有助于做我想做的,但是这是另一个23KB包括。如果是更好的方式去,我会看看如何实现它,但除此之外,我想preFER一个简单的解决方案(如果可用)。

I've heard of Backbone-relational which might help do what I want, but that's another 23kb to include. If that's the better way to go I'll look at implementing it, but otherwise I'd prefer a simpler solution if one is available.

这需要工作,我是否创建一个新的 ParentModel 通过code的记录,或者如果它是由一个JSON提要被自动创建。

This needs to work whether I create a new ParentModel record through code, or if it's being automatically created by a JSON feed.

推荐答案

我通常会发现,移动结构元素出的属性是清洁的,所以我的记录和父属性是对象,而不是属性上。这就是说,你可以采取的收集和父对象的不同事件的优点是:

I usually find that moving the structural elements out of the attributes is cleaner so my records and parent properties are on the object, not the attributes. That said, you could take advantage of the different events on the collection and the parent object:

var ParentModel = Backbone.Model.extend({
    initialize: function () {
        _.bindAll(this, 'adoptOne', 'adoptAll');
        this.records = new Records();

        this.on('change:records', function () {
             this.records.reset(this.get('records'));
        });
        this.records.on('reset', this.adoptAll);
        this.records.on('add', this.adoptOne);

        this.records.reset(this.get('records'));
    },

    adoptAll: function () {
       this.records.each(this.adoptOne);
    },
    adoptOne: function (model) {
        model.parent = this;
    }
});

几个测试:

var p = new ParentModel({
    name: "I am the parent",
    records: [{id: 1}, {id: 2}]
});

p.records.add({id: 3});

p.records.each(function (child) {
    console.log(child.get('id')+' '+child.parent.get('name'));
});

p.set({records: [{id: 4}]});

p.records.each(function (child) {
    console.log(child.get('id')+' '+child.parent.get('name'));
});

和小提琴 http://jsfiddle.net/sPXaZ/

这篇关于什么是设置在骨干parent属性的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆