-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider supporting Promises in addition to success/error callbacks #118
Comments
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:
|
But the most common pattern we're using is the following line in View.initialize:
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. |
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.
The text was updated successfully, but these errors were encountered: