0

I have been trying to create my own base Models and Collections for extending in my apps. By this I mean that instead of calling:

var MyModel = Backbone.Collection.extend({
    ...
});

I want to use

var MyModel = App.Model.extend({
    ...
});

And similarly for Controllers. In most OOP models, there is an easy way to do this, for example in Mootools:

var MyModel = new Class({
    Extend: App.Model,
    ...
});

This is really the pattern I wish to mimic. The reason for this is that there are certain attributes and methods I want available in all my models and collections, without having to re-implement it each time.

I notice that there are a few libraries (e.g. backbone-relational) that manage this, but the code seems a little dirty (maybe just because the pattern is so unfamiliar), and I am not quite sure I understand it.

So my question is: how do I go about extending Backbone's core classes in a normal OOP manner? (yes I know that Javascript doesn't use class inheritance but prototype inheritance &c..)

GTF
  • 8,031
  • 5
  • 36
  • 59

1 Answers1

0
App.Model = Backbone.Model.extend({
  //do whatever you want to be present in all of your models
});

Then anywhere your App is available

var MyModel = App.Model.extend({
  // Do whatever you want to do for this particular type of model
});

So you just create yourself a new base Model that has all the certain attributes and methods you want by extending upon Backbone and then you use it like the Backbone one.

jakee
  • 18,486
  • 3
  • 37
  • 42
  • This is what I thought initially, but it wasn't working. I've tried again, and now it is working. I must have introduced another sort of bug somewhere down the line. Thanks a lot :) – GTF Aug 23 '12 at 12:46