why sometimes you need to have a dummy object for event storage (JavaScript) -


i don't understand why need have dummy object in javascript store evetns, following code.

metal.gold = function() {    var temp = $("<div>"); //dummy object    this.submit = function(url1, method, data){       temp.trigger("submit", data); //invoke trigger dummy object??       $.ajax({          url:url1,          data:json.stringify(data),          contenttype: "application/json; charset=utf-8",          datatype:"json",          success: function(data){             temp.trigger("submittsuccess", data); //????             //dosomething          },          error: function(error){            temp.trigger("submitfailure", error);// ???          }       });    } } 

why dummy element has been deklared , trigger has been invoked 3 times? thanks.

updated

here invokes submit method:

mygold.submit(.... function(data){    temp.trigger("submitquantitysuccess", data); }, ....  this.onsubmitquantitysuccess = function (handler) {temp.on("submitquantitysuccess", handler); } 

i have search files cannot find method name submitquantitysuccess. don't know , happend data or error when comes back.

that's not best way it. i've never seen guess work...

you want return $.ajax call in submit function, return jqxhr object (a deferred object).

metal.gold = function() {     this.submit = function(url1, method, data){         return $.ajax({             url:url1,             data:json.stringify(data),             contenttype: "application/json; charset=utf-8",             datatype:"json"         });     } } 

so can do:

var mygold = new metal.gold();  mygold.submit(arg1, arg2, arg3)     .done(function () { ... })     .fail(function () { ... }); 

more information on jquery's deferred object here

and jquery's jqxhr object here


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -