javascript - Define a promise creating a new Service Object -
i have question promise system in angularjs , creation of services. have service called 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 customer this.addnewoffice = function(){ self.offices.addnewoffice(); }; //remove office customer this.removeoffice = function(officeindex){ self.offices.removeoffice(officeindex); }; //show offices this.toggleofficevisibility = function(officeindex){ self.offices.toggleofficevisibility(officeindex); }; }]);
in "constructor" part of service there ajax call service loads attributes of customer database. async task. how can create promise in situation? use customer service this:
var customer = new customer(id);
and like
var customer = new customer(id).then( function(){...}, //success function(){...} //error );
to need promise. have program method create()
within customer service?
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; } } ... ... ... }]);
and use stuff this?
var customer = new customer(); customer.create(id).then( function(){...}, function(){...}, )
isn't there way call "new customer" , receive promise? thank in advance!
like said in comment recommend against approach. putting complex asynchronous logic in constructor confusing , not make or clear api.
that said, don't need .create
method.
the trick is: if function called constructor returns object in javascript - returned instead of this
value.
sparing whole angular around it:
function(customerdbservices, officeslist, $q){ return function(customerid){ var p = $q.defer(); var = p.promise; // our 'that' 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 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; // return promise here. }
Comments
Post a Comment