简体   繁体   English

ribs.js嵌套模型的对象不是实例唯一的

[英]backbone.js nested model's object not unique to instance

I have a parent Model that has by default and nested Model. 我有一个默认包含嵌套模型的父模型。 The nested Model itself has propertyA, which is an object with defaultValue and userValue. 嵌套的模型本身具有propertyA,这是具有defaultValue和userValue的对象。 The idea is that every instance of parent Model comes with a nested Model that has a userValue of null, and a static default value. 这个想法是,父模型的每个实例都带有一个嵌套的模型,该模型的userValue为null,并且为静态默认值。

The problem is, when I update the userValue for one instance, it ends up changing for all instances going forward. 问题是,当我为一个实例更新userValue时,它将最终更改所有实例。 Instead of updating the userValue for a particular instance, I'm doing something wrong and updating the nested Model "prototype". 我没有为特定实例更新userValue,而是在做错误的事情并更新了嵌套的Model“原型”。

Below code available at http://jsfiddle.net/GVkQp/4/ . 以下代码可从http://jsfiddle.net/GVkQp/4/获得 Thanks for help. 感谢帮助。

var ParentModel = Backbone.Model.extend({
    initialize: function(){
        var defaultObjs = {
            nestedModel : new NestedModel()
        };
        $().extend(this.attributes, defaultObjs);
    }
});


var NestedModel = Backbone.Model.extend({
    defaults: {
        "propertyA" : {
                        "defaultValue" : "ABC",
                        "userValue": null
                      }
    }
});

var ParentView = Backbone.View.extend({
    initialize: function(){
        var self = this;
        var defaultValue = self.model.get("nestedModel").get("propertyA")["defaultValue"];
        var userValue = self.model.get("nestedModel").get("propertyA")["userValue"];

        var div = $("<div class='box'>defaultValue = <span id='defaultValue'>" +
                    defaultValue+ "</span>, userValue = <span id='userValue'>" +
                    userValue + "</span></div>");
        var divButton = $('<input type="button" value="Change my userValue to DEF">')
            .click(function(){
            temp = self.model.get("nestedModel").get("propertyA");
            temp.userValue = "DEF";
            //wherewas my intention is just to set the userValue for a particular instance,
            //generating another instance of ParentView (by clicking button) reveals that
            //the userValue is set even for new instances.
            //How can I change it such that I only change the userValue of the particular
            //instance?

            //set value
            self.model.get("nestedModel").set({"propertyA": temp});

            //update userValue in View
            userValue = self.model.get("nestedModel").get("propertyA")["userValue"];
            $(this).parent().find("span#userValue").text(userValue);    
        });    

        //append divButtont to div
        div.append(divButton)

        //append div to body
        $('body').append(div)
    },
});

$("#add").click(function(){
    var newBoxView = new ParentView({
        model: new ParentModel()
    });
});

Thanks for the jsFiddle. 感谢jsFiddle。 It illustrates your problem perfectly. 它完美地说明了您的问题。

You see, when you set your "propertyA" in your defaults, the object you create is the default value being copied along with every other nested class you create. 您会看到,将“ propertyA”设置为默认值时,创建的对象就是默认值以及创建的所有其他嵌套类都将被复制。 For this reason, when you define your defaults , you have the option to define it as a function in order to create the default value new every time. 因此,在定义defaults ,可以选择将其定义为一个函数,以便每次创建新的default值。 If you do this, you solve your problem: 如果这样做,则可以解决您的问题:

var ParentModel = Backbone.Model.extend({
    defaults: function() {
        return {nestedModel: new NestedModel()};
    }
});


var NestedModel = Backbone.Model.extend({
    defaults: function() {
        return {
        "propertyA" : {
                        "defaultValue" : "ABC",
                        "userValue": null
                      }
        };
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM