本文介绍了如何在 Ember.js 中为 Ember-data 模型创建承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Ember-Data 模型.我想在 .then 承诺加载后进行一些处理,然后返回与承诺相同的模型.这就是我现在所拥有的.我如何将返回对象包装为一个 Promise 以便可以链接其他 Promise?

I have an Ember-Data model. I would like to do some processing in the .then promise once it has loaded and then return the same model as a promise. This is what I have right now. How do I wrap up the return object as a promise so that other promises can be chained?

App.Member.find(1).then(function(member){

  //do some processing here
  return member; // Does this need to be wrapped as a promise?

}

推荐答案

基本上你可以像这样创建一个 promise:

Basically you can create a promise like this:

var promise = new Ember.RSVP.Promise(function(resolve, reject){
  // succeed
  resolve(value);
  // or reject
  reject(error);
});

然后你可以使用 then 属性来进一步链接它:

and then you can use the then property to chain it further:

promise.then(function(value) {
  // success
}, function(value) {
  // failure
});

您也可以看看这个 jsbin,它展示了它们是如何实现的.而这个也很有帮助.

You can aslo have a look at this jsbin which shows how they could be implemented. And this is also very helpful.

希望有帮助.

这篇关于如何在 Ember.js 中为 Ember-data 模型创建承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:53