我写了一个RactiveJS适配器,它将对象与json-api同步。我想从ractive中回复已解决的Promise,但ractive.set-method会在设置属性后立即解析,并且不会等待我的adaptor.set-method解析。
适配器类:
JsonApiWrapper.prototype = {
/**
* Update object if model-id changes
* @param prop
* @param value
*/
set: function (prop, value) {
if (prop === '_id' && !this.updatelock) {
this.updatelock = true;
return this.jsonapi.find(value)
.then(function (data) {
return this.ractive.set(this.prefixer(data));
}.bind(this))
.then(function(){
/**
* release updatelock
* @type {boolean}
*/
this.updatelock = false;
return this.object;
}.bind(this))
} else {
this.object[prop] = value;
return Promise.resolve(this.object);
}
},
来自ractive的电话:
this.set('poll._id', id).then(function(){
/**
* Check if poll is voted
*/
if (typeof Storage !== "undefined") {
if (localStorage.getItem('polls-'+this.get('poll._id'))) {
this.set('poll.status', 'inactive');
}
}
});
第二个代码中的“then”会在设置属性后立即解析,而不是在解析获取后解析。
这是一个错误,一个功能还是我做错了?