扩展 Backbone.js 中模型超类的默认值

发布于 2024-11-17 20:32:22 字数 942 浏览 4 评论 0原文

我想向 这个答案,但我似乎无法这样做,我很抱歉。

扩展子类的默认值反映在超类中。这似乎违背了目的,我更倾向于在子类中显式列出超类的默认值,以获得我正在寻找的结构。

var Inventory = Backbone.Model.extend({
    defaults: {
        cat: 3,
        dog: 5
    }
});

var ExtendedInventory = Inventory.extend({
});

_.extend(ExtendedInventory.prototype.defaults, {rabbit: 25});

var i = new Inventory();
var ei = new ExtendedInventory();
console.log(i.attributes);
console.log(ei.attributes);

输出:

{cat: 3, dog: 5, rabbit: 25}
{cat: 3, dog: 5, rabbit: 25}

不是我的(我也假设 op)想要:

{cat: 3, dog: 5}
{cat: 3, dog: 5, rabbit: 25}

I would like to pose this as a question to this answer but I can't seem to do so, I apologize.

Extending the defaults for the subclass are reflected in the superclass. This seems to defeat the purpose and I'm more apt to explicitly list the superclass' defaults in the subclass to get the structure I'm looking for.

var Inventory = Backbone.Model.extend({
    defaults: {
        cat: 3,
        dog: 5
    }
});

var ExtendedInventory = Inventory.extend({
});

_.extend(ExtendedInventory.prototype.defaults, {rabbit: 25});

var i = new Inventory();
var ei = new ExtendedInventory();
console.log(i.attributes);
console.log(ei.attributes);

This outputs:

{cat: 3, dog: 5, rabbit: 25}
{cat: 3, dog: 5, rabbit: 25}

Not what I (nor, I assume, the op) want:

{cat: 3, dog: 5}
{cat: 3, dog: 5, rabbit: 25}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尝蛊 2024-11-24 20:32:22

问题在于 Inventory.prototype.defaultsExtended.prototype.defaults 具有相同的引用,因为您尚未覆盖该引用。

所以你可以通过两种方式做到这一点,也许更多,但我只找到了这两种:

编辑: 第一个例子是不正确的(见评论);请参考第二条。

var ExtendedInventory = Inventory.extend({
    defaults: {
        rabit:25
    }
});

_.extend(ExtendedInventory.prototype.defaults, Inventory.prototype.defaults);

或者

var ExtendedInventory = Inventory.extend({
    defaults: _.extend({},Inventory.prototype.defaults,
         {rabit:25}
    )
});

我认为第一个看起来更干净。

The problem is that Inventory.prototype.defaults and Extended.prototype.defaults has the same reference, because you have not override the reference.

So you can do this in 2 ways, maybe more but i only found this 2:

Edit: The first example is incorrect (see comments); please refer to the second.

var ExtendedInventory = Inventory.extend({
    defaults: {
        rabit:25
    }
});

_.extend(ExtendedInventory.prototype.defaults, Inventory.prototype.defaults);

or

var ExtendedInventory = Inventory.extend({
    defaults: _.extend({},Inventory.prototype.defaults,
         {rabit:25}
    )
});

I think the first looks cleaner.

如梦初醒的夏天 2024-11-24 20:32:22

我认为解决这个问题的最好方法是使用 underscore.js 的 _.defaults 方法。这将允许您覆盖模型子类中的默认值:

_.defaults(ExtendedInventory.prototype.defaults, 
           Inventory.prototype.defaults);

请参阅此示例:

http://jsfiddle.net/mattfreer/xLK5D/

I think the best way to solve it is to use underscore.js's _.defaults method. This will allow you to override default values in your Model subclass:

_.defaults(ExtendedInventory.prototype.defaults, 
           Inventory.prototype.defaults);

See this example:

http://jsfiddle.net/mattfreer/xLK5D/

一梦等七年七年为一梦 2024-11-24 20:32:22

作为 JCorcuera 答案的扩展,如果您的基类使用(或可能使用)一个函数来定义默认值,那么这将很好地工作:

   defaults: function() {                                     
     return _.extend( _.result(Slot.prototype, 'defaults'),{  
       kind_id: 6,                                  
       otherthing: 'yello'  
       // add in your extended defaults here                                
   })}                                                      

关键位是在子默认方法和 _.result( ) 文档

As an extension of JCorcuera's answer, if your base class uses (or may use) a function to define defaults then this will work nicely:

   defaults: function() {                                     
     return _.extend( _.result(Slot.prototype, 'defaults'),{  
       kind_id: 6,                                  
       otherthing: 'yello'  
       // add in your extended defaults here                                
   })}                                                      

the key bit being the use of a funciton in the child defaults method and _.result() docs

荭秂 2024-11-24 20:32:22

我认为你是对的,你想确保 Inventory.prototype.defaults 不会因为将兔子添加到 ExtendedInventory.defaults 而改变。我的原型继承不足以清楚地解释为什么下面的代码有效,但我认为这可以实现您想要的功能。

ExtendedInventory.defaults = {}
_.extend(ExtendedInventory.defaults, ExtendedInventory.prototype.defaults, {rabbit: 25});

关于 _.extend 方法需要记住的重要一点是第一个参数是destination。它从第一个参数之后的参数中获取所有属性,并将它们放入目标参数中。

另一点是 ExtendedInventory.prototype.defaults === Inventory.prototype.defaults 结果为 true,这意味着它们是同一个对象,因此如果您更改 ExtendedInventory 的原型,您就会更改 Inventory 的原型(我不确定为什么尽管它们首先是平等的)。

I think you're right that you'd want to make sure that Inventory.prototype.defaults doesn't change as a result of adding rabbit to the ExtendedInventory.defaults. My protoypical inheritance isn't good enough to clearly explain why the below works, but I think this does what you want it to do.

ExtendedInventory.defaults = {}
_.extend(ExtendedInventory.defaults, ExtendedInventory.prototype.defaults, {rabbit: 25});

An important point to remember about the _.extend method is that the first argument is destination. It takes all the attributes from the arguments after the first argument and places them into the destination argument.

Another point is that ExtendedInventory.prototype.defaults === Inventory.prototype.defaults results in true, meaning they're the same object, so if you change the prototype of ExtendedInventory you change the prototype of Inventory (I'm not sure why they're equal though in the first place).

千里故人稀 2024-11-24 20:32:22
var MoveToolModel = ToolModel.extend({
    extendDefaults: {
        cursor: 'move'
    },
    initialize: function() {
        ToolModel.prototype.initialize.apply(this, arguments);
        this.defaults = _.extend({}, this.defaults, this.extendDefaults);
    },
    draw: function(canvasContext, data) {
        //drag
    }
});
var MoveToolModel = ToolModel.extend({
    extendDefaults: {
        cursor: 'move'
    },
    initialize: function() {
        ToolModel.prototype.initialize.apply(this, arguments);
        this.defaults = _.extend({}, this.defaults, this.extendDefaults);
    },
    draw: function(canvasContext, data) {
        //drag
    }
});
方觉久 2024-11-24 20:32:22

我认为 underscore.js 没有深入扩展价值观。如果你有一些数组,你应该使用 Jquery $.extend 。您可以在此处尝试一下。

var basemodel = Backbone.Model.extend({
  defaults:{a:{"1":"1","2":4,"4":5},b:2}
}
);

var model1 = basemodel.extend({
    defaults: $.extend(false,basemodel.prototype.defaults,{a:{"1":1,"2":2},b:{"xx":13}})
});


var model2 = basemodel.extend({
    defaults: $.extend(false,basemodel.prototype.defaults,{a:{"1":1,"2":2},z:13})
});



var m1 = new model1();
var m2 = new model2();


alert(JSON.stringify(m1.toJSON()));
alert(JSON.stringify(m2.toJSON()));

另外,您应该给第一个参数“false”才能正确完成工作。真实时,只是相互交错。

I think underscore.js don't extend values deeply. You should use Jquery $.extend if you have some array. You can try it here

var basemodel = Backbone.Model.extend({
  defaults:{a:{"1":"1","2":4,"4":5},b:2}
}
);

var model1 = basemodel.extend({
    defaults: $.extend(false,basemodel.prototype.defaults,{a:{"1":1,"2":2},b:{"xx":13}})
});


var model2 = basemodel.extend({
    defaults: $.extend(false,basemodel.prototype.defaults,{a:{"1":1,"2":2},z:13})
});



var m1 = new model1();
var m2 = new model2();


alert(JSON.stringify(m1.toJSON()));
alert(JSON.stringify(m2.toJSON()));

Also you should give first parameter "false" to do your work correctly. when it is true, just interlace each other.

感性 2024-11-24 20:32:22

另一种方法是使用下划线的 _.extend 函数来完成此操作:

var SuperClass = Backbone.Model.extend({
  baseDefaults: {
    baseProp1: val,
    baseProp2: val2
  }
});

var SubClass = SuperClass.extend({
  defaults: _.extend({
    prop1: val,
    prop2: val2
  }, SuperClass.prototype.baseDefaults)
})

Yet another way is to use underscore's _.extend function to accomplish this:

var SuperClass = Backbone.Model.extend({
  baseDefaults: {
    baseProp1: val,
    baseProp2: val2
  }
});

var SubClass = SuperClass.extend({
  defaults: _.extend({
    prop1: val,
    prop2: val2
  }, SuperClass.prototype.baseDefaults)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文