2

I would like to call from within my plugin an external function and I wish the external function could read the value date-id of the button that was invoked.

plugin.js

;(function ($, window, document, undefined) {

// Function-level strict mode syntax
'use strict';

 $.fn.Plugin = function(options) {
    var options = $.extend({}, $.fn.Plugin.defaults, options);
    return this.each(function() {
        var $this = $(this);
        $.each(options.commands, function(el, fc) {
            $this.on('click', '.' + el, function(e) {
                //options.callback.call(fc);
            });
        });
    });
};

$.fn.Plugin.defaults = {
    commands: {
        add: "addFunc",
        edit: "editFunc",
        del: "deleteFunc",
        show: "showFunc"
    },
    callback: function() {}
};

})(jQuery, window, document);

index.php

<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="js/plugin.js"></script>
<script>
    $(function() {
        $('#grid').Plugin();
    });
    function editFunc() {
        alert($(this).attr('data-id'));
    }
</script>
</head>

<html>
<body>
dinamic table
<table>
    <tr>
        <td>001</td>
        <td>
            <button type="button" class="btn btn-warning btn-xs edit" data-id="001"><span class="glyphicon glyphicon-edit"></span></button>
            <button type="button" class="btn btn-danger btn-xs delete" data-id="001"><span class="glyphicon glyphicon-trash"></span></button>
        </td>
    </tr>
    <tr>
        <td>002</td>
        <td>
            <button type="button" class="btn btn-warning btn-xs edit" data-id="002"><span class="glyphicon glyphicon-edit"></span></button>
            <button type="button" class="btn btn-danger btn-xs delete" data-id="002"><span class="glyphicon glyphicon-trash"></span></button>
        </td>
    </tr>
    ...
</table>   

How could I do that? Thank you

1 Answer 1

1

editFunc() should be set on global scope (or plugin scope), not wrapped in document pseudo ready handler and your options should be function references, not strings.

So:

$.fn.Plugin = function (options) {
    var options = $.extend({}, $.fn.Plugin.defaults, options);
    return this.each(function () {
        var $this = $(this);
        $.each(options.commands, function (el, fc) {
            $this.on('click', '.' + el, function (e) {
                fc.call(this);
            });
        });
    });
};

$.fn.Plugin.defaults = {
    commands: {
        add: addFunc,
        edit: editFunc,
        "delete": deleteFunc, // 'delete' is a reserved word
        show: showFunc
    },
    callback: function () {}
};

$(function () {
    $('table').Plugin();
});



function editFunc() {
    alert('call editFunc');
}
Sign up to request clarification or add additional context in comments.

8 Comments

I tried as you suggested but I get this error: TypeError: fc.call is not a function.
Otherwise you can do this by creating dynamic callback taking them from options.commands?
@PaoloRossi Works here: jsfiddle.net/aho6mtd9 Now check what is fc in your code, i told it shouldn't be string BUT function reference, e.g edit: editFunc, not edit: "editFunc",. If for some reasons, you want to use string (but why should you?), then you could use: window[fc].call(this);
@PaoloRossi Otherwise you can do this by creating dynamic callback taking them from options.commands? Not sure to understand what you mean?!
Create callback editFunc, addFunc, etc from commands so I can call when I initialize the plugin. $('table').Plugin( { editFunc: function() { // some action }); . Thanks a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.