gpt4 book ai didi

javascript - 定义创建新服务对象的 promise

转载 作者:行者123 更新时间:2023-11-29 22:03:05 25 4
gpt4 key购买 nike

我有一个关于 AngularJS 中的 promise 系统和服务创建的问题。我有一个名为 Customer 的服务:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com