【问题标题】:Define a promise creating a new Service Object定义一个创建新服务对象的承诺
【发布时间】:2023-03-03 04:00:01
【问题描述】:

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

难道没有办法调用“新客户”并获得承诺吗?提前谢谢!

【问题讨论】:

  • 我强烈建议不要这样做。不要将复杂的异步逻辑放在构造函数中,这会很混乱。
  • 感谢您的建议。那么您是否建议在女巫中创建一个“retrieveCustomer”方法来放置异步创建的东西?
  • 是的,我还将添加一个关于您如何可以 完成此任务的答案以及您可能不应该这样做的说明。如果我是你,我也会承诺CustomerDBServices 服务。

标签: javascript angularjs asynchronous service promise


【解决方案1】:

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

【讨论】:

  • 好的,但我刚刚意识到这样做调用“new Customer()”会返回一个承诺(当然,这是我想要的!),那么我如何才能访问客户? :-/
  • 操作,对不起!我还没有看到您已将属性直接分配给“that”,例如“that.ID”或“that.name”。好的,很清楚:)
猜你喜欢
  • 2013-09-15
  • 2020-12-05
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多