Skip to content
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

Promise as argument of Collection add, remove, reset, set methods #4096

Closed
redexp opened this issue Nov 9, 2016 · 1 comment
Closed

Promise as argument of Collection add, remove, reset, set methods #4096

redexp opened this issue Nov 9, 2016 · 1 comment

Comments

@redexp
Copy link

redexp commented Nov 9, 2016

What would you say if add ability to pass to Collection methods not only plain objects, arrays and models, but also promise. Just add code like this to each method

add: function(models, options) {
  //...
  if (typeof models.then === 'function') {
    var self = this;
    return models.then(function (models) {
      return self.set(models, options);
    });
  }
  //..
}

and so we can do

users.add($.get('/users'));

very useful if you decided to change some of your methods from sync to async like

User = Model.extend({
  getProducts: function () {
    // was
    return products.where({user_id: this.get('id')});
    // become
    return $.get('/products', {user_id: this.get('id')});
  }
});

// but code same for sync and async
bag.add(user.getProducts());
@akre54
Copy link
Collaborator

akre54 commented Nov 14, 2016

How about user.getProducts().then(products => bag.add(products))?

I think if we're going to add Promises to Backbone we should do so holistically (like #3729, #3651), not in fits and starts.

@akre54 akre54 closed this as completed Nov 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants