javascript - Custom JS Confirm Modals using jQuery.Deferred and issues with return values based on buttons -


i'm java developer has entered javascript world, forgive me if make silly mistakes or assumptions. attempting translate our native browser/js "confirm" dialog boxes custom modals have been developed using backbone framework. currently, these modals not support "confirm" type functionality. think primary issue modals not working asynchronously.

i have method calling opens modal, , hoping receive "true" or "false" value call depends on button user clicked. however, method returns modal displayed, , not wait button click. modal system supports seperate callbacks each button, can defined when calling "openportletmodal" method, demonstrate below. however, "confirmdiscard" method returns undefined value, without ever waiting of button calls. question is, possible pause execution of method until callback made?

while researching, did find out bit jquery.deferred,but haven't found clear way of utilizing functionality want, , no examples see seem include different return values based on callbacks.

here's sample code:

confirmdiscard: function(context) {     var deferred = new $.deferred();      mymodal.openportletmodal(context, null, {     views: [{         name: "auth",         title: "confirmation",            renderresponsedata: "this test",  //message modal renders         buttons: [{         type: "finish",         label: "ok",         click: function() {             console.log("clicked ok");             deferred.resolve("true");  //this i'd return confirmdialog() method             return false; //modal system expects "false" value callback if there no ajax call made.         },         }, {         type: "cancel",         click: function() {             console.log("clicked cancel");             deferred.resolve("false");               return false;         },         }]     }]     });      deferred.done(function(value) {                  alert(value);  //this called on button click, confirmdiscard method has returned.     return value;     })      return deferred.promise(); }, 

given pattern, there way in javascript have method wait while modal responded to?

i advise way yours, little different. hope you.

cutomconfirm: function(text) {     var d, rendered, template,     _this = this;      template = $('#custom_confirm_temp');     rendered = $.tmpl($(template).template(), {         text: text || ''     });      $.blockui({         message: rendered,         showoverlay: true,         onunblock: function() {             return $(document).unbind('click');         },         css: {           width: '600px',           height: '150px',           opacity: '1',           border: '0',           backgroundcolor: 'none'         }     });      d = $.deferred();      rendered.find('#ok').click(function() {         $.unblockui();         return d.resolve();     });      rendered.find('#cancel').click(function() {         $.unblockui();         return d.reject();     });      return d; } 

you need create template(with id ="custom_confirm_temp" ) , call when need.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -