定义创建新服务对象的承诺

时间:2014-03-20 10:49:44

标签: javascript angularjs asynchronous service promise

我对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调用,该服务从数据库加载客户的属性。这是一个异步任务。如何在这种情况下创造承诺?我使用这样的客户服务:

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(){...},
)

有没有办法打电话给“新客户”并收到承诺?提前谢谢!

1 个答案:

答案 0 :(得分:2)

就像我在评论中所说,我建议不要采用这种方法。将复杂的异步逻辑放在构造函数中通常会让人感到困惑,并且无法形成非常好的或清晰的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.
    }