定义一个承诺,创建一个新的Service Object
Define a promise creating a new Service Object
我有一个关于AngularJS中的承诺系统和服务创建的问题。我有一个服务叫做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调用,用于从数据库加载客户的属性。这是一个异步任务。在这种情况下,我怎样才能做出承诺呢?我是这样使用客户服务的:
var customer = new Customer(ID);
,我想做一些像
var customer = new Customer(ID).then(
function(){...}, //success
function(){...} //error
);
要做到这一点,我需要一个承诺。我必须在客户服务中编写方法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(){...},
)
有没有办法打电话给"新客户"并接受承诺?提前感谢!
正如我在评论中所说的,我不建议使用这种方法。将复杂的异步逻辑放在构造函数中通常会令人困惑,并且不会产生非常好的或清晰的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函数打开一个新的弹出窗口
- jQuery:循环一个具有不同超时值的循环
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- AngularJS angular-modal-service给了我一个错误
- 来自另一个模块的 Angular JS Service DI
- 在Angular Service中获取数据并将其传递给另一个控制器
- Angular Service可以从一个控制器中设置数据,但不能从另一个控制器中设置
- 如何使用service worker存储一个用户id
- 如何在AngularJS上使用service函数检查一个包db文档是否存在
- 注册一个service worker,得到一个错误
- Angular Modal Service如何在Modal popup中编写函数.如何在模态弹出框中传递一个新的控制器函
- 我该如何使用Service Worker为我所有的API请求添加一个授权头?
- 在Angular Service中基于另一个JSON检索JSON
- 如何用一个Javascript标签同时安装service worker和manifest ?
- 在另一个文件夹中注册Service Worker
- 如何将service worker注册到一个重定向的脚本URL
- 用service worker缓存到另一个页面的链接
- Angularjs的service typeror undefined不是一个函数
- Angular Service被注入到控制器中,但它显示为一个空对象
- 定义一个承诺,创建一个新的Service Object