I’ve looked for a solution to this but cannot find anything that works.
Note: I am overriding the Backbone.sync method globally.
When I instantiate a Session model I pass a number of attributes to it. These, as you would expect, should be “gettable” via mySessionInstance.get('someAttribute'). The session model calls this.fetch() within the initialize method. My custom Backbone.sync method needs to access some of the session attributes but when it runs I get an error saying the “get” method does not exist for that instance.
It’s as if I was trying to access .get for my session instance before it was fully initialized. Any ideas on how to fix it? I wish there was a “ready” or “initialized” event for models so I could just listen for that and ensure certain things only happen when the model is truly done initializing.
Thanks,
Luis
There are two events that a model
fetchwill trigger:"change"events will be triggered if anything is changed (i.e. if anything came back from the server). Afetchis mostly asetthat is called by an AJAX success handler after all."sync"event will be triggered when the model is synced with the server, syncing includes fetching. Thefetchdocs aren’t explicit on this but you can easily see it in the source and the master events list notes it.The
"change"event is probably more useful in general and should serve to indicate that the model is ready for use.If you just need a one-time notification, you could use use the
successcallback from thefetch: