question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ajax Data Sources

See original GitHub issue

While you provide access to existing DOM data, it appears that there is no way to load the data on demand for it to be copied.

It would be great if you were able to send an Ajax request automatically by using data-clipboard-ajax="http://example.com/" on the target attribute.

This can be further customized by allowing for a custom xhr object to be passed to the constructor, allowing for user-level events to be created without modifying the library itself.

The main reason i’m suggesting this, is because I want to be able to copy a possibly large Base64 encoded string without having to have it in the DOM all of the time, but it seems that no matter what i do (using jQuery, atleast) i cannot get your text callback function to work using ajax based functions.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
janrhansencommented, Dec 15, 2016

@MrCrankHank : I have used a workaround where I hook up to the text event from clipboard.js. When the function is called, I retrieve the clipboard content from the server with an synchronous ajax call, providing relevant parameters. The code below works, but is a bit… well… rude. Errorhandling etc. to come in the very near future, but it should help you move along. However, synchronous ajax requests (in jquery) are “deprecated because of its detrimental effects to the end user’s experience.” - quote Chrome developer console. So you’ll get a warning about that, but I guess it won’t go away anytime soon, at least not without something to replace it.

Hope this helps!

Javascript

var clipboardItems = new Clipboard('.clipboardCopyButton2', {
      text: function (trigger) {
          var customerId = trigger.getAttribute('data-customerid');
          var cpv = GetClipboardData(customerId);
          return cpv;
      }
  });

function GetClipboardData(customerId) {
    var retval = "";

    var month = 12; // get from UI element

    var args = {};
    args.customerId = customerId;
    args.monthNumber = month;

    $j.ajax({
        type: "POST",
        async: false,
        url: "invoicing.aspx/GetInvoiceContent",
        data: JSON.stringify(args),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            retval = msg.d;
        }
    });
    return retval;
}

Server side C#

        [WebMethod]
        public static string GetInvoiceContent(int customerId, int monthNumber)
        {
                return "generated clipboard content";
        }
1reaction
AlexH269commented, Dec 15, 2016

I have the same problem as @janrhansen. The data i want to copy to the clipboard isn’t in the dom by the time the page is displayed. It should only be loaded from the server if the user clicks the button. Any ideas?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ajax data source (objects) - DataTables example
In this example the Ajax source returns an array of objects, which DataTables uses to display the table. The structure of the row's...
Read more >
Ajax (remote data) - The jQuery replacement for select boxes
Ajax (remote data). Select2 comes with AJAX support built in, using jQuery's AJAX methods. In this example, we can search for repositories using...
Read more >
DataTables example - Ajax data source (objects)
In this example the Ajax source returns an array of objects, which DataTables uses to display the table. The structure of the row's...
Read more >
DataTables example - Ajax data source (arrays)
DataTables has the ability to read data from virtually any JSON data source that can be obtained by Ajax. This can be done,...
Read more >
DataTables example - Ajax data source (arrays) - Otisa
DataTables has the ability to read data from virtually any JSON data source that can be obtained by Ajax. This can be done,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found