javascript - How can I stop an object method call before the ajax has completed -
i have following java script object
function eventtypeobj() { alleventtypes = []; // when object created go , event types can included in journey or clusters. $.ajax({ url: "/atomwebservice.svc/getdisplayeventtypes", datatype: "json", success: function(result) { alleventtypes = eval("(" + result.d + ")"); } }); // returns list of event type ids. this.geteventtypeids = function() { var eventtypeids = []; (var = 0; < alleventtypes.length; i++) { eventtypeids.push(alleventtypes[i].id); } return eventtypeids; }; }
i wondering if there way stop 1 calling eventtypeobj.geteventtypeids(); before ajax call in constructor has succeeded, , there no data in alleventtypes array?
something way better (im not guaranteeing 100% working, concept sound):
function eventtypeobj() { this.alleventtypes = []; this.hasloadedeventtypes = false; var loadeventtypes = function(cb) { $.ajax({ url: "/atomwebservice.svc/getdisplayeventtypes", datatype: "json", success: function(result) { this.alleventtypes = eval("(" + result.d + ")"); this.hasloadedeventtypes = true; cb(); } }); }; this.geteventtypeids = function(updateeventtypes, callback) { var _geteventtypeids = function() { var eventtypeids = []; (var = 0; < this.alleventtypes.length; i++) { eventtypeids.push(this.alleventtypes[i].id); } return eventtypeids; }; if (!this.hasloadedeventtypes || updateeventtypes) { loadeventtypes(function(){ callback(_geteventtypeids()); }); } else callback(_geteventtypeids()); }; }
example usage:
var eto = new eventtypeobj(); eto.geteventtypeids(false, function(eventtypeidarray) { // stuff id array }); /* somewhere later on want updated eventtypeid array in case event types have changed. */ eto.geteventtypeids(true, function(eventtypeidarray) { // stuff updated ids });
Comments
Post a Comment