2
나는 나에게 간단한 약속RSVP 약속을 연결하고 원래 거부/성공 함수를 반환하는 방법은 무엇입니까?
var PromiseMixin = Ember.Object.create({
promise: function(url, type, hash) {
return new Ember.RSVP.Promise(function(resolve, reject) {
hash.success = function(json) {
return Ember.run(null, resolve, json);
};
hash.error = function(json) {
if (json && json.then) {
json.then = null;
}
return Ember.run(null, reject, json);
};
$.ajax(hash);
});
}
});
이 잘 작동하고 당신이 기대하는 것처럼 다음 - 수와 같은 Ajax 호출을 래핑 할 수있는 간단한 RSVP 도우미가 있습니다. 문제는이 낮은 레벨을 먼저 감싸는 또 다른 약속이 필요한 코드가있을 때입니다.
예를 들어 내 엠버 컨트롤러에서
나는 내 약속 모델에서이
Appointment.remove(this.store, appointment).then(function() {
router.transitionTo('appointments');
}, function() {
self.set('formErrors', 'The appointment could not be deleted');
});
을 할 수있는 나는
가remove: function(store, appointment) {
return this.xhr('/api/appointments/99/', 'DELETE').then(function() {
store.remove(appointment);
//but how I do return as a promise?
}, function() {
//and how can I return/bubble up the failure from the xhr I just sent over?
});
},
xhr: function(url, type, hash) {
hash = hash || {};
hash.url = url;
hash.type = type;
hash.dataType = "json";
return PromiseMixin.promise(url, type, hash);
}
현재 내 컨트롤러는 항상 떨어진다 "제거"이 일을 해요 "ajax 메서드가 204를 반환하고 성공한 경우에도" "실패"상태가됩니다. 내 모델에서이 제거 메소드에서 "체인 된 약속"리턴을 수행하여 컨트롤러가 위와 같이 "선택 가능"으로 호출 할 수있게하려면 어떻게해야합니까?
붐 당신은 그것을 내 친구에게 맡겼습니다! 이전 (잘못된 설명)에 대해 사과드립니다. 당신이 진술 한대로 이것은 100 % 일했습니다! 다시 한 번 감사드립니다! –