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

标签 javascript angularjs asynchronous service promise

我有一个关于 AngularJS 中的 Promise 系统和服务创建的问题。我有一项名为“客户”的服务:

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/

相关文章:

javascript - 如何禁用网页中Office Web Component图表控件的右键上下文菜单?

javascript - 根据 html 中选择的下拉选择选项打开选项卡内容?

javascript - 如何从 javascript 访问 knockout View 模型

javascript - NodeJS 服务器错误

javascript - 我可以只使用异步 "promisify"函数吗?

javascript - Node.js:异步代码 + js 闭包的问题

javascript - 事件导航项

javascript - 如何根据 Angularjs 接收到的数据在我的 JSON 对象中创建动态键?

javascript - Angular.js 验证消息未显示

asynchronous - 当任务已经在 python3.7 的 asyncio 库中运行时,如何将任务添加到事件循环?