backbone.js models pointing to same instance of nested model

后端 未结 2 1393
情书的邮戳
情书的邮戳 2020-12-29 14:24

Using backbone.js, here is a quick test to demonstrate the problem I am facing with nested models.

Preface I have a Obj Model that

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 15:07

    With:

    var Obj = Backbone.Model.extend({
        defaults: {
            obj1    : new Obj1(),
            obj2    : new Obj2()
        }
    })
    

    You are saying that you want all objects created with "Obj" to have the same "obj1" and "obj2" values, use:

    var Obj = Backbone.Model.extend({
        initialize: function() {
            this.obj1 = new Obj1();
            this.obj2 = new Obj2();
        }
    });
    

    To achieve what you seen to want. http://documentcloud.github.com/backbone/#Model-constructor

提交回复
热议问题