0

I have problem understanding how to create a callback function which I can use to extend the options, as stated in this guide. Here's the excerpt of code that I want to use for callback;

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);

So that I can call using $("a").on("click", postcommand(eventrequest)), I tried creating a function like this;

$.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?
};

But of course, my attempt is a failure. Spoon feeding is good, but you can always give me a hint and I'll try to explore on my own. Thanks!

5
  • 3
    $("a").on("click", postcommand(eventrequest)) doesn't make sense unless postcommand(eventrequest) returns a function. Commented Nov 12, 2013 at 20:09
  • Of course. That's my question. I'm trying to create a function for that. Commented Nov 12, 2013 at 20:10
  • Where is your "attempt"? I see where you started to make a jquery plugin, but it's being assigned to the jquery.prototype rather than directly to jquery, and you aren't even using it in your code. And best of all, you didn't even attempt to build a function to return! Commented Nov 12, 2013 at 20:11
  • 1
    Also, your syntax for the object inside extend is incorrect; replace the = with :, and ; with ,. Commented Nov 12, 2013 at 20:11
  • @KevinB Thats why I'm here. I need a hint and your expert opinion. My attempt is half cooked. Maybe my question is wrong also. Forgive me for being so bad, but I'm still new to this. Commented Nov 12, 2013 at 20:20

1 Answer 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();
Sign up to request clarification or add additional context in comments.

Comments

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.