扩展 Backbone.js 中模型超类的默认值
我想向 这个答案,但我似乎无法这样做,我很抱歉。
扩展子类的默认值反映在超类中。这似乎违背了目的,我更倾向于在子类中显式列出超类的默认值,以获得我正在寻找的结构。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
问题在于
Inventory.prototype.defaults
和Extended.prototype.defaults
具有相同的引用,因为您尚未覆盖该引用。所以你可以通过两种方式做到这一点,也许更多,但我只找到了这两种:
编辑: 第一个例子是不正确的(见评论);请参考第二条。
或者
我认为第一个看起来更干净。
The problem is that
Inventory.prototype.defaults
andExtended.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.
or
I think the first looks cleaner.
我认为解决这个问题的最好方法是使用 underscore.js 的 _.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:
See this example:
http://jsfiddle.net/mattfreer/xLK5D/
作为 JCorcuera 答案的扩展,如果您的基类使用(或可能使用)一个函数来定义默认值,那么这将很好地工作:
关键位是在子默认方法和 _.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:
the key bit being the use of a funciton in the child defaults method and
_.result()
docs我认为你是对的,你想确保 Inventory.prototype.defaults 不会因为将兔子添加到 ExtendedInventory.defaults 而改变。我的原型继承不足以清楚地解释为什么下面的代码有效,但我认为这可以实现您想要的功能。
关于 _.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.
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).
我认为 underscore.js 没有深入扩展价值观。如果你有一些数组,你应该使用 Jquery $.extend 。您可以在此处尝试一下。
另外,您应该给第一个参数“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
Also you should give first parameter "false" to do your work correctly. when it is true, just interlace each other.
另一种方法是使用下划线的
_.extend
函数来完成此操作:Yet another way is to use underscore's
_.extend
function to accomplish this: