What's a clean way to run some code during initialize() if a Model is constructed with all its data, but otherwise run that code only after a fetch? The best I have is something like the following, but I'm hoping there's a more streamlined way of doing it:
if (this.hasData()) {
this.setupListeners();
} else {
this.fetch().done(this.setupListeners);
}
Or maybe the following, though the intent isn't super obvious when reading:
var whenLoaded = (this.hasData()) ? $.Deferred().resolve() : this.fetch();
whenLoaded.done(this.setupListeners);
Backstory: the model does some listenTo calls on itself so it can save itself when certain changes occur. But if the model needs fetching, the listenTo()s shouldn't be made until the fetch finishes, otherwise once the data is fetched the subsequent set will trigger the change events and thus an unnecessary save to the server.
Is there a cleaner way to do this? Or maybe this is as good as it gets and I'm being picky. Thanks for your thoughts.