简体   繁体   English

使Ember函数从回调返回承诺

[英]Make Ember function return a promise from callback

I want to have a function return the promise from model.save() , but the call to model.save() is in a callback, and so doesn't make it up the chain. 我想让一个函数返回来自model.save()的promise,但是对model.save()的调用在回调函数中,因此没有构成它的链。 Here's what I mean: 这就是我的意思:

function saveOn (target, attribute) {
    target.addObserver(attribute, function () {
        if (target.get(attribute)) {
            target.removeObserver(attribute);
            Ember.run.once(target, function() {
                target.save();
            });
        }
    });
};

(this function was necessary to solve this problem I previously posted ) (此功能是解决我先前发布的问题所必需的)

I want the target.save(); 我想要target.save(); line to instead be: return target.save(); 改为: return target.save();

Then I could do this: 然后我可以这样做:

saveOn().then();

which as it stands doesn't work, the error message simply being that then doesn't exist on that object. 其中,因为它代表不工作,该错误消息简单地在于then不该对象上存在。

Update 更新

teacherSignUp: function() {
    var model = this.get('model');
    var teacher = this.get('store').createRecord('teacher', {
        description: 'Why hello sir',
        user: model
    });
    model.set('teacher', teacher);
    saveOn(teacher, 'user.isFulfilled').then(function() {
        console.log("Model");
        saveOn(model, 'teacher.isFulfilled');
    });
}

The console.log("Model"); console.log("Model"); is successfully called, but the model is never persisted at all. 已成功调用,但模型从未永久存在。 What's happening here? 这里发生了什么事?

Update Solved 更新已解决

It the second observer on the model was never firing, since after the teacher had completed saveOn the model was already done. 模型上的第二个观察者从未解雇,因为在老师完成saveOn ,模型已经完成。 I just changed the saveOn(model, 'teacher.isFulfilled'); 我只是更改了saveOn(model, 'teacher.isFulfilled'); to model.save(); 进行model.save(); and it works great. 而且效果很好。

pass another parameter. 传递另一个参数。

function saveOn (target, attribute, then) {
    target.addObserver(attribute, function () {
        if (target.get(attribute)) {
            target.removeObserver(attribute);
            Ember.run.once(target, function() {
                var promise = target.save();
                if(then){ 
                  promise.then(function(){ then(); });
          });
        }
    });
};

or create another promise and resolve in the then 或在那时创造另一个承诺并解决

function saveOn (target, attribute) {
    return new Ember.RSVP.Promise(function(resolve, reject){

      target.addObserver(attribute, function () {
        if (target.get(attribute)) {
            target.removeObserver(attribute);
            Ember.run.once(target, function() {
                target.save().then(function(record){ resolve(record); },
                      function(error){ reject(error); });
          });
        }
      });
    });
};

teacher.isFulfilled is probably already changed, so the observer isn't firing, cause it isn't changing, try checking before creating the observer and skipping that portion if already true/exists etc. Teacher.isFulfilled可能已经更改,因此观察者没有触发,因为它没有改变,请在创建观察者之前尝试检查,如果已经存在/存在则跳过该部分。

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

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