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.

Problem with declaring callbacks in the data-* attributes

See original GitHub issue

What I like about select2 is the idea to specify all configuration parameters in data-* attributes.

Suppose I have some sort of templating function stored in my namespace:

window.myNamespace = {
    templateResult: function(item) {
        if (item.loading) return item.text;
        return '<div>' + item.text + ' (' + item.price + ')' + '</div>';
    }
}

Now I want to specify this function as a callback in my select tag in data-template-result attribute like that:

<select class="select2" data-template-result="myNamespace.templateResult"></select>

Clean and cool but does not work. The reason is, as we can read from the Jquery docs: “Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null)”. No function in this list. So, now there is no legal way to declare callbacks using data-* attributes.

For now I’m using the following workaround: I’m prefixing all my callback names with ‘_fn::’ like this: “_fn::myNamespace.templateResult”. Then, before activating any select tag with select2(), I’m using the following function to let jQuery know about callbacks declared in data-* attributes:

function(obj) {
    var getFunctionByName = function (functionName, context) {
        context = typeof context === 'undefined' ? window : context;
        var namespaces = functionName.split(".");
        var func = namespaces.pop();
        for(var i = 0; i < namespaces.length; i++) {
            context = context[namespaces[i]];
        }
        return context[func];
    }
    var $obj = $(obj);
    $.each($obj.data(), function(key, el) {
        if (typeof el === 'string' && el.substr(0, 5) == '_fn::') {
            $obj.data(key, getFunctionByName(el.substr(5)));
        }
    });
}

Works just fine, but looks like a hack.

Maybe we can do something with select2 behavior itself to let users legally specify callbacks in data attributes without using such external hacks?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dwasylcommented, Feb 5, 2020

This is still an issue and a simple fix like one of the suggestions would be fantastic in the next release. Is it possible to revisit, there are existing PRs (closed) that would address the issue.

0reactions
stale[bot]commented, Mar 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass a callback function as a html data attribute - Stack Overflow
Try this: <div data-execute="someFunction.abc" id="someId"></div> var x = document.getElementById("someId").getAttribute('data-execute'); ...
Read more >
How to access the correct this inside a callback
We explore the methods for accessing the correct this in a callback by explicitly forcing a context binding to point to our object...
Read more >
JavaScript Passing parameters to a callback function
In this article, we will see how to pass parameters to a callback function. Firstly let us see what is a callback function....
Read more >
Attribute Callback Functions - NI - National Instruments
Attributes that represent values that the driver recalculates upon each query have read callbacks but no write callbacks. The read callback ...
Read more >
JavaScript Callbacks - W3Schools
A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback...
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