简体   繁体   English

如何在Backbone.Model.extend中使用多个扩展?

[英]How to use multiple extensions in Backbone.Model.extend?

I am working on a project that creates models in such a manner. 我正在开发一个以这种方式创建模型的项目。

Backbone.DeepModel.extend({
  ...
});

This deepmodel allows for deeply nested attributes to be refereed via sytax such as 这个深度模型允许通过sytax来引用深层嵌套的属性,例如

containerGrid.get('cols')[0]['content']

Now I have to implement nested models in a one-to-many format. 现在我必须以一对多的格式实现嵌套模型。 Deep model doesn't support this, but a library called backbone-relational.js does. 深度模型不支持此功能,但是名为backbone-relational.js的库可以支持。 The syntax for this library is 这个库的语法是

Backbone.RelationalModel.extend({
  ...
});

The functionality for these libraries does not overlap, and it is not possible for me to rewrite all of the code currently implemented with the Backbone.Deepmodel syntax. 这些库的功能不重叠,我不可能重写当前使用Backbone.Deepmodel语法实现的所有代码。 Is there another library I can use which supports both functionalities or another workaround you might recommend? 是否有另一个我可以使用的库,它支持两种功能或您可能推荐的其他解决方法?

Thank you very much for reading, I really appreciate your time and effort! 非常感谢您的阅读,我非常感谢您的时间和精力!

Just extend from both classes. 只需从两个类扩展。

You can provide any object to .extend , not just an object literal. 您可以向.extend提供任何对象,而不仅仅是对象文字。 Extend your own class from Deepmodel , then extend a new class from RelationalModel , providing the Deepmodel -extended class's prototype: Deepmodel扩展您自己的类,然后从RelationalModel扩展一个新类,提供Deepmodel -extended类的原型:

var MyModel = Backbone.Deepmodel.extend({ ... })

MyModel = Backbone.RelationalModel.extend(MyModel.prototype);

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

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