backbone icon indicating copy to clipboard operation
backbone copied to clipboard

Where is the initialize function for Backbone.Model documented?

Open komali2 opened this issue 5 years ago • 2 comments

I'm looking up how to instantiate a Backbone.Model. Under the docs for constructor / initialize, it says:

When creating an instance of a model, you can pass in the initial values of the attributes, which will be set on the model. If you define an initialize function, it will be invoked when the model is created.

What it doesn't say is what arguments that initialize function takes, which if I remember correctly includes the options object passed in on instantiation.

Where is this documented?

komali2 avatar Oct 31 '18 18:10 komali2

initialize is called at the end of the constructor function, and is passed the same arguments that you've passed to the constructor. The default implementation is to do nothing.

http://backbonejs.org/docs/backbone.html#section-51

This should, indeed, be documented slightly better.

jashkenas avatar Oct 31 '18 19:10 jashkenas

Oh, good to know. This also clarifies a second question I had - whether attributes passed in in the constructor were available in the initialize function. It appears they are. As in, get I do this.get('some_attribute') inside the initialize function.

komali2 avatar Oct 31 '18 21:10 komali2