简体   繁体   English

当使用余烬数据时如何使用jquery.promise

[英]How to use jquery.when with ember data promises

I'm trying to fire a callback when multiple ember models have finished their save methods. 我正在尝试在多个余烬模型完成其保存方法时触发回调。

var promises = [];
modellist.forEach(function(mymodel){
    promises.push(mymodel.save())
}
$.when.apply(null, promises).done(function () {
    console.log('finished promises');
}
console.log('finished method');

The apply function is executed immediately. apply函数将立即执行。

Question: Which of the following is true? 问题:以下哪一项是正确的?

  • promise != ember-promise 许诺!=余烬无极
  • promise != deferred 答应!=推迟
  • ember-promise != deferred 余烬承诺!=推迟

I'm a big fan of RSVP.hash as it allows an object, while RSVP.all uses an array. 我非常支持RSVP.hash因为它允许对象,而RSVP.all使用数组。

If you want to use it in regular code to save multiple models. 如果要在常规代码中使用它来保存多个模型。

actions: {
   doSomething() {
     Ember.RSVP.hash({
       model1Saved: this.get('model1').save(),
       model2Saved: this.get('model2').save(),
    }).then((hash) => { 
      /* hash.model1Saved and hash.model2Saved are available here */ 
    }, (error) => { 
     /* Deal with error */ 
    });
   }
} 

Which is nice, because you don't have to mess with array indexes. 很好,因为您不必弄乱数组索引。

Also when returning a model for the route 同样在返回路线模型时

model() {
return Ember.RSVP.hash({
  posts: this.store.findAll('post'),
  tags: this.store.findAll('tag'),
  categories: this.store.findAll('category'),
});
},

And then in the setupController method you can: 然后在setupController方法中,您可以:

setupController(controller, model) {
  this._super(controller, model.posts);
  controller.set('tags', model.tags);
  controller.set('category', model.categories);
}

I've found a solution: 我找到了一个解决方案:

  • Ember.RSVP.all(promises).then(function(){...}) Ember.RSVP.all(承诺)。然后(函数(){...})

This was not easy to find in the docs :-( 这在文档中不容易找到:-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM