0

如本指南中所述,我无法理解如何创建可用于扩展选项的回调函数。这是我想用于回调的代码摘录;

var chart       =   {};
chart.data      =   $('.liselected').attr("data"); 
chart.command   =   $('.liselected').attr("cmd");
chart.option    =   "option"; // Category of event request
chart.sessionid =   docCookies.getItem("sessionid");
chart.ageType   =   selectedAgeType;
chart.showData  =   showUnderlyingData;

var action  =   function(result, status) {

    $('#thumbnails .error').remove();
    var chart_list  =   "";

    $.each(result, function(i, val){
        chart_list += //Custom HTML Output
    });

    $('#chart_view').html(chart_list);
};

$.post("jsoncommand", JSON.stringify(chart), action);

为了可以调用 using $("a").on("click", postcommand(eventrequest)),我尝试创建这样的函数;

$.fn.postcommand = function(){
    var settings = $.extend({
        item        :   {},
        data        :   $('.liselected').attr("data"),
        command     :   $('.liselected').attr("cmd"),
        option      :   "specify query",
        sessionid  :    docCookies.getItem("sessionid"),
        ageType     :   selectedAgeType,
        showData    :   showUnderlyingData,
    }, options );

    return //How do I make the output of HTML result is customizable?
};

但是,当然,我的尝试是失败的。勺子喂养很好,但你总是可以给我一个提示,我会尝试自己探索。谢谢!

4

1 回答 1

1

It might be a good idea to check out the jQuery plugin section: http://learn.jquery.com/plugins/advanced-plugin-concepts/. You could do something like this:

$.fn.postcommand = function (options) {

    // define some default values for your plugin
    var default = {
        callback: function () {}
    }

    // merge default settings,  with the ones given  
    var settings = $.extend( {}, defaults, options );

    return this.each(function() {
        var $this = $(this);
        $this.on('click', function(event) {
          settings.callback();
          event.preventDefault();
        });
    }
});

And then use your plugin on some links:

$('a.useCallback').postcommand();
于 2013-11-12T20:27:55.903 回答