I'm looking for some examples for creating a custom constructor on my models. I want the structure the model/data differently then just setting it as attributes.
Can somebody show me some basic example of how to do this?
Thanks!
I'm looking for some examples for creating a custom constructor on my models. I want the structure the model/data differently then just setting it as attributes.
Can somebody show me some basic example of how to do this?
Thanks!
This is how I override the default Backbone.Model constructor:
After that you have to make sure the Backbone.Collection prototype uses the updated Backbone.Model constructor:
The 'advantage' of this approach in my opinion is that you can keep using Backbone.Model as your model constructor, which makes the change more transparent.
If you want to write your model by yourself, like this:
Be careful, I think you need to consult
Backbone.Model
constructor source code. But I think this is not a good idea. Overrideinitialize
method is the right way:If you really want to override the constructor, pass a
constructor
property toBackbone.Model.extend()
, e.g.:If you want to call the built-in constructor from your custom constructor, you can do something like:
Or if you don't want to have to repeat the name of the variable containing the parent class all over the sub class, or you don't want to worry about the value of that variable changing, you can do something like the following:
Or if you prefer the way @Claude suggests, but repeating the sub class variable name within the sub class instead of the parent class var name:
If you want more advice than that, you'll have to be more specific about what you want to accomplish.
Anything that you just want to do after the built-in constructor functionality, you should probably do in
initialize()
.It sounds like you're looking for the
initialize
method. It's called when you create a new model, and you can use if for whatever you need it for:If you're looking to do something more involved, you might override the
constructor
method in Backbone core. That's a much trickier business, though. Much better to work with exposed methods if you can!As I mention in the comment, take care when using
this.constructor.__super__
(orthis.__super__
), lest you end up in an endless loop (Maximum call stack size exceeded
in Chrome).Try the following in the console (at own risk, it will lead to afore mentioned endless loop)
Reason is that when creating
B
,this
in the constructor forA
points to an instance ofB
, sothis.constructor.__super__.constructor
keeps pointing towards the constructor ofA
, which is being called time and time again.If you want "intended behaviour", use one of the following syntaxes:
or directly without
__super__
:Something to be aware of when overriding the Backbone.Model constructor - if you don't call
Backbone.Model.apply
then the Model.cid may not be set.That's bad - If cid is not set across multiple models, collection may consider it to be a duplicate of the first model in your collection - and will not allow it to be added.