javascript - Push function to front of object literal -
javascript - Push function to front of object literal -
i have html popup jquery ui modal:
<a href="my/url" class="popup">click me</a> with javascript code:
$('.popup').click(function() { var = this; var dialog = $('<div>').load($(a).attr('href'), function() { var form = dialog.find('form'); dialog.dialog({ modal: true, title: $(a).attr('title'), buttons: { add together : function () { $.post($(form).attr('action'), $(form).serialize()); dialog.dialog('close'); }, cancel: function () { dialog.dialog('close'); } } }); if ($(a).attr('data-third')) { // add together here button } }); homecoming false; }); the thought resource in modal contains form, on submit of modal form submitted. when <a href="my/url" class="popup" data-third="add & new">click me</a> exists, means 3rd button placed in modal in order: cancel | add | add & new.
i tried code:
if($(a).attr('data-third')) { var buttons = dialog.dialog('option', 'buttons'); buttons[$(a).attr('data-third')] = function(){ // callback here } dialog.dialog('option', 'buttons', buttons); } i got new button, order add & new | cancel | add. how can create sure order cancel | add | add & new?
with code should cancel | add together | add together & new? (from left right.
$('.popup').click(function() { var = this; var dialog = $('<div>').load($(a).attr('href'), function() { var form = dialog.find('form'); dialog.dialog({ modal: true, title: $(a).attr('title'), buttons: { cancel: function() { dialog.dialog('close'); }, add: function() { $.post($(form).attr('action'), $(form).serialize()); dialog.dialog('close'); } } }); if ($(a).data('third')) { var buttons = dialog.dialog('option', 'buttons'); console.log(buttons); buttons[$(a).data('third')] = function() { // callback here } dialog.dialog('option', 'buttons', buttons); } }); homecoming false; }); fiddle here: http://jsfiddle.net/rqq2p/1/
remember utilize data() , not attrib() attribute link!
edit - "buttons" object, , each property 1 of buttons. think order in buttons appear order in properties added. in illustration cancel leftmost button because first added property , add together & new it's rightmost because latest. if need fiddle order create new object , add together properties in order want:
if ($(a).data('third')) { var buttons = dialog.dialog('option', 'buttons'); var newbuttons = {}; newbuttons[$(a).data('third')] = function() { // callback here } newbuttons['cancel'] = buttons['cancel']; newbuttons['add'] = buttons['add']; dialog.dialog('option', 'buttons', newbuttons);//now add together & new should leftmost } javascript jquery object-literal
Comments
Post a Comment