jashkenas / backbone

Give your JS App some Backbone with Models, Views, Collections, and Events
http://backbonejs.org
MIT License
28.09k stars 5.39k forks source link

Where is the initialize function for Backbone.Model documented? #4206

Open komali2 opened 5 years ago

komali2 commented 5 years ago

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?

jashkenas commented 5 years ago

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.

komali2 commented 5 years ago

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.