Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.extend() with functions

I'm writing a jQuery plugin that will need callback functions that are executed, for example, when an option is chosen from the <select> the plugin is applied to.

I've seen plugins that allow you to define functions in the options you pass it, like this:

$('div.foo').pluginBar({ 
    option1: 'red',
    someEvent: function() {
        // Do stuff
    }
});

I want to be able to define a default someEvent function in my plugin code, but if the user defines that function in the options they pass, it should overwrite the default function. Is this possible with $.extend(), or am I look in the wrong places?

I've had a look at the plugin authoring tutorial, but I don't think it covers extending the default behaviour of functions anywhere. I've read things about using an init: function, but I'd rather just be able to define a function (inside the plugin namespace) and change it with the options passed to the plugin.

like image 258
Bojangles Avatar asked Dec 04 '11 12:12

Bojangles


People also ask

What does the extend function do in SQL?

The EXTEND function adjusts the precision of a DATETIME or DATE value. The DATETIME or DATE expression that is its first argument cannot be a quoted string representation of a DATE value. If you do not specify first and last qualifiers, the default qualifiers are YEAR TO FRACTION (3).

How do I use the extend function to perform addition or subtraction?

You can use the EXTEND function to perform addition or subtraction with a DATETIME value and an INTERVAL value that do not have the same time unit qualifiers. The next expression expands a literal DATETIME YEAR TO DAY value to a precision of YEAR TO MINUTE so that an interval YEAR TO MINUTE value can be subtracted from it:

What does extend () do in jQuery?

If you pass just one object to jQuery.extend (), then jQuery assumes that the jQuery object itself is the "first" parameter (ie: the one to be modified), and your object is the "second" (ie: the one to add to the first). So: Show activity on this post. It merges the content of one object to another.

What is an extension function in Java?

Basically, an extension function is a member function of a class that is defined outside the class. For example, you need to use a method to the String class that returns a new string with first and last character removed; this method is not already available in String class. You can use extension function to accomplish this task.


2 Answers

Here's an example:

(function( $ ) {
  $.fn.pluginBar = function(options) {
      var settings = $.extend( {
          someEvent: function() {
              alert('default');
          }
      }, options);

      return this.each(function() {
          settings.someEvent();
      });
  };
})( jQuery );

$('div.foo').pluginBar({ 
    option1: 'red',
    someEvent: function() {
        alert('custom');
    }
});

If you don't specify someEvent when wiring up the plugin, the default one will be used.

like image 85
Darin Dimitrov Avatar answered Sep 21 '22 16:09

Darin Dimitrov


In javascript, functions are first-class objects. Meaning, you can use functions like you would any other variable:

//making an anonymous function, and assigning it to a variable
var meep = function () {
    return 'meep';
};

//passing a function as a parameter
[ 'moop' ].map( meep ); //['meep']

//assigning it in an object literal
var weirdNoises = {
    'meep' : meep,

    'slarg' : function () {
        return 'slarg';
    }
};

weirdNoises.meep(); //'meep'
weirdNoises.slarg(); //'slarg'

//you can also return functions
function meeper () {
    return meep;
}
function slarger () {
    return function () {
        return 'slarg';
    };
}

//meeper returns a function, so the second () means "execute the function
// that was returned"
meeper()(); //'meep'
slarger()(); //'slarg'

As you can see, functions are just like any other value. So, you can define a default option that'll be a function, and override it like anything else.

$.fn.weirdNoise = function ( options ) {
    var defaults = {
        makeNoise : function () {
            return 'Rabadabadaba';
        },
        isSilly : true
    };

    return $.extend( defaults, options );
};

var raba = $( 'foobar' ).weirdNoise().makeNoise();
raba.makeNoise(); //'Rabadabadaba'
raba.isSilly; //true

var shaba = $( 'foobar' ).wierdNoise({
    makeNoise : function () {
        return 'Shabadabadoo';
    }
});
shaba.makeNoise(); //'Shabadabadoo'
shaba.isSilly; //true

A contrived example, but I think it illustrates the point.

like image 31
Zirak Avatar answered Sep 19 '22 16:09

Zirak