Consider supporting Promises in addition to success/error callbacks
In the various ajax methods (save, fetch, etc) Backbone uses success and error callbacks passed as functions in the options object. It would be nice if we could use Promises instead but in a way that's backward-compatible with existing code.
collection.fetch, and all model I/O methods (save, fetch, destroy), are currently returning promises (this feature is standard for backbone). So, you could do right now:
collection.fetch().done( () => this.render() );
But the most common pattern we're using is the following line in View.initialize:
this.listenTo( this.model, 'change', this.render )
And view will be rendered for every change no matter what the reason is. Deep changes in model attributes of Model/Collection types will also be detected, merged, and bubbled up, thus you will receive just single 'change' event after the fetching of deeply nested structure.
If you need to load multiple resources (models/collections) in a bulk, extend LazyStore model.
var ViewModel = Nested.LazyStore.defaults({
c : Some.Collection,
d : Other.Collection
});
...
initialize : function(){
this.model = new ViewModel();
this.model.fetch(); // load everything, returns combined promise
this.listenTo( this.model, 'change', this.render ); // fired on every change of c and d, two times here
Funny part is that this line with 'fetch' is not actually required. Collections will be fetched automatically on first c and d attributes read attempt.
You can do all that stuff with React views, and it looks and works nicer, but that's a bit different story. I will release updated version of React bindings soon.
But I guess, with knockout typical scenario might look somewhat different.
As I've mentioned in another issue, we are still working out the best way to use the sync() functionality or even whether we want to use it or replace it. But currently Backbone provides error/success callbacks via the options array or direct access to the xhr via the return. By default this is jQuery which doesn't behave quite like a real Promise.
LazyStore is not something we've looked into yet. We will have to take a look as part of our experimentation into the best way to use or replace the sync() functionality in NestedTypes :)
Models takes sync from the store they belong to (or from the global one), so you could just override sync in LazyStore.
About promises - am I getting you right that you're asking to replace jQuery promises with standard ES6 ones? I could do it in future, rewriting sync in the way that it won't depend on jQuery. Meanwhile, you can just replace Backbone.ajax method to the one returning the real Promise, and assigning it to Nested.ajax. Just make sure you've updated to 1.1.7 for this to work.
Implemented in Type-R f/promise-io branch.
Soon it will be there.