开发者

Using zClip on click event of a jQuery UI Dialog button

I want to use a jQuery zClip plugin in jQuery UI dialog button, but I don't know how to adapt in this case. Anyone can h开发者_开发技巧elp me?

Thank you in advance!

    $.ajax({
        url: '/music/lyrics/' + hash,
        success: function (data) {
            data = jQuery.parseJSON(data);
            $('#dialog-modal').html(data.lyrics);
            $('#dialog:ui-dialog').dialog('destroy');
            $('#dialog-modal').dialog({
                modal: true,
                resizable: false,
                title: 'Lyric: ' + data.song,
                width: 500,
                height: 400,
                buttons: {
                    'Copy' : function () {
                         // use zClip to copy $('#dialog-modal').text() here
                     }
                }
            });
        },
        error: function (msg) {
            alert(msg);
        }
    });


I would ignore the normal way dialog buttons handle actions, and separately use the way zClip handles actions. Something like this:

$.ajax({
        url: '/music/lyrics/' + hash,
        success: function (data) {
            data = jQuery.parseJSON(data);
            $('#dialog-modal').html(data.lyrics);
            $('#dialog:ui-dialog').dialog('destroy');
            $('#dialog-modal').dialog({
                modal: true,
                resizable: false,
                title: 'Lyric: ' + data.song,
                width: 500,
                height: 400,
                buttons: {
                    'Copy' : function () { return true; }
                }
            });
            $('#dialog-modal ui-button:contains(Copy)').zclip({
                path:'../whatever/ZeroClipboard.swf',
                copy:$('#dialog-modal').text()
            });
        },
        error: function (msg) {
            alert(msg);
        }
    });


Assuming you are using jQuery 1.8+, you can specifiy your buttons in a different way to add IDs to them:

$("#mydialog").dialog({
  ...
  buttons : [{
    text: "Close",
    click: function() {
      $(this).dialog("close");
    }
  },{
    text: "Copy to clipboard",
    id: "copyButton", // here is your ID
    click : function() {
      alert("Sorry, copy not supported in your browser, please copy manually.");
    }
  }]
  ...
});

//after .dialog("open");
$("#copyButton").zclip({
  ...
  clickAfter: false // dont propagate click: will suppress unsupported warning
  ...
});

The only issue I have is that it seem you can only mount zclip on visible buttons, so I do the zclip() call inside the handler for the button that opens the dialog

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜