Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. 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. 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!

like image 256
Alek Hurst Avatar asked Dec 09 '25 20:12

Alek Hurst


1 Answers

Just extend from both classes.

You can provide any object to .extend, not just an object literal. Extend your own class from Deepmodel, then extend a new class from RelationalModel, providing the Deepmodel-extended class's prototype:

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

MyModel = Backbone.RelationalModel.extend(MyModel.prototype);
like image 149
meagar Avatar answered Dec 11 '25 09:12

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!