我有一个关于 AngularJS 中的 Promise 系统和服务创建的问题。我有一项名为“客户”的服务:
angular.module("app").factory("Customer", ["CustomerDBServices", "OfficesList", "$q",
function(CustomerDBServices, OfficesList, $q){
return function(customerID){
var self = this;
//attributes
this.name = null;
this.ID = null;
this.code = null;
this.isVisible = 1;
this.showOffices = true;
this.offices = new OfficesList();
//constructor
if(typeof customerID !== "undefined"){
var metacustomer = CustomerDBServices.find({ID:customerID}, function(){
self.name = metacustomer.results.customer_name;
self.ID = metacustomer.results.customer_ID;
self.code = metacustomer.results.customer_internal_code;
self.isVisible = metacustomer.results.customer_is_visible;
self.getOffices();
});
}
//add office to customer
this.addNewOffice = function(){
self.offices.addNewOffice();
};
//remove office from customer
this.removeOffice = function(officeIndex){
self.offices.removeOffice(officeIndex);
};
//show offices
this.toggleOfficeVisibility = function(officeIndex){
self.offices.toggleOfficeVisibility(officeIndex);
};
}]);
在此服务的“构造函数”部分中,有一个对服务的 AJAX 调用,该服务从数据库加载客户的属性。这是一个异步任务。在这种情况下我该如何创建一个 promise ?我这样使用客户服务:
var customer = new Customer(ID);
我想做一些类似的事情
var customer = new Customer(ID).then(
function(){...}, //success
function(){...} //error
);
为此我需要一个 promise 。我是否必须在客户服务中编写一个方法create()
?
angular.module("app").factory("Customer", ["CustomerDBServices", "OfficesList", "$q",
function(CustomerDBServices, OfficesList, $q){
return function(customerID){
var self = this;
//attributes
this.name = null;
this.ID = null;
this.code = null;
this.isVisible = 1;
this.showOffices = true;
this.offices = new OfficesList();
//costructor
this.create = function(){
if(typeof customerID !== "undefined"){
var rest = $q.defer();
var metacustomer = CustomerDBServices.find({ID:customerID}, function(){
self.name = metacustomer.results.customer_name;
self.ID = metacustomer.results.customer_ID;
self.code = metacustomer.results.customer_internal_code;
self.isVisible = metacustomer.results.customer_is_visible;
self.getOffices();
rest.resolve("ok!");
});
return rest.promise;
}
}
...
...
...
}]);
然后像这样使用那些东西?
var customer = new Customer();
customer.create(ID).then(
function(){...},
function(){...},
)
是否有办法调用“新客户”并获得 promise ?预先感谢您!
最佳答案
就像我在评论中所说的那样,我建议反对这种方法。将复杂的异步逻辑放入构造函数中通常会令人困惑,并且不会形成非常好的或清晰的 API。
也就是说,您不需要 .create
方法。
技巧是:如果作为构造函数调用的函数在 JavaScript 中返回一个对象,则返回该对象而不是 this
值。
为您节省周围的整个 Angular:
function(CustomerDBServices, OfficesList, $q){
return function(customerID){
var p = $q.defer();
var that = p.promise; // our 'that' is now a promise
//attributes
that.name = null;
that.ID = null;
that.code = null;
that.isVisible = 1;
that.showOffices = true;
that.offices = new OfficesList();
// use `that` instead of this in additional code
if(typeof customerID !== "undefined"){
var metacustomer = CustomerDBServices.find({ID:customerID}, function(){
self.name = metacustomer.results.customer_name;
self.ID = metacustomer.results.customer_ID;
self.code = metacustomer.results.customer_internal_code;
self.isVisible = metacustomer.results.customer_is_visible;
self.getOffices();
that.resolve("ok!");
});
}
return that; // we return the promise here.
}
关于javascript - 定义创建新服务对象的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22530589/