定义一个承诺,创建一个新的Service Object

Define a promise creating a new Service Object

本文关键字:一个 Service Object 一个承诺 创建 定义      更新时间:2023-09-26

我有一个关于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.
    }