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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -