Created
May 1, 2010 19:04
-
-
Save MarkBorcherding/386578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#foo1').foo(); | |
$('#foo2').foo({bar:'baz'}); | |
// later on, maybe in a event handler of something else. | |
// These calls need the options set up above, but the way they are defined, | |
// they are not in the closure of $.fn.foo function block | |
$('#foo1').foo('doFoo'); | |
$('#foo2').foo('doFoo'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
$.fn.foo = function(options){ | |
// Handle API methods | |
if(typeof arguments[0]=='string'){ | |
// Perform API methods on individual elements | |
if(this.length>1){ | |
var args = arguments; | |
return this.each(function(){ | |
$.fn.foo.apply($(this), args); | |
}); | |
}; | |
// Invoke API method handler | |
$.fn.foo[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []); | |
return this; | |
} | |
var options = $.extend($.fn.foo.options, options); | |
// Is this OK? | |
// stash the options on the items so we can use them in the API | |
$(this).data('foo-options',options); | |
console.debug('options.bar = ',options.bar); | |
}; | |
$.extend($.fn.foo, { doFoo: function(){ | |
console.debug('in doFoo bar = ', $(this).data('foo-options').bar); | |
} | |
}); | |
$.fn.foo.options = { bar:'bar'}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need to use the options in the API extensions, but not sure the best way to pass them down there.