Skip to content

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

@redexp

Description

@redexp

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());

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions