Thursday, October 6, 2011

backbone.js: Model.initialize() is called at the end

When is Model.initialize() called in backbone.js?

Just a quick post to indicate that in backbone.js, Model.initialize(attrs) is called after everything else is done, like setting the attributes if you passed them into the constructor. I'm only writing this down because it took me a few minutes to find the answer. I had to look at the annotated source.

The docs, and some blog posts, only say things to the effect of "you can provide an initialize function, which will be called at the time of construction" which is ambiguous--is it before or after the attributes are set on the object?

But now that I know the answer, I realize it doesn't matter--I don't want to be designing initialization code that is so fine-grained that it matters whether I set the attributes once or twice.

No comments:

Post a Comment