Created
November 11, 2011 20:46
-
-
Save gonzedge/1359189 to your computer and use it in GitHub Desktop.
Adding custom method calls to the jQuery Rambling Slider
This file contains 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
//Stop and start are really setters | |
$('#slider').ramblingSlider('stop'); // => [div#slider] for method chaining | |
$('#slider').ramblingSlider('start'); // => [div#slider] for method chaining | |
$('#slider').ramblingSlider('effect'); // => 'random' | |
$('#slider').ramblingSlider('option'); // => Object with all options | |
$('#slider').ramblingSlider('option', 'speed'); // => 400 | |
$('#slider').ramblingSlider('effect', 'boxRain'); // => [div#slider] for method chaining | |
$('#slider').ramblingSlider('option', 'speed', 600); // => [div#slider] for method chaining | |
$('#slider').ramblingSlider('option', 'startSlide'); // => 0 | |
$('#slider').ramblingSlider('option', 'startSlide', 2); // => throws error "Slider already running. Option 'startSlide' cannot be changed." |
This file contains 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($){ | |
var methods = { | |
init : function(options) { /*...*/ }, | |
show : function() { /*...*/ }, | |
hide : function( ) { /*...*/ }, | |
update : function(content) { /*...*/ }, | |
}; | |
$.fn.tooltip = function(method) { | |
// Method calling logic | |
if (methods[method]) { | |
return methods[method].apply(this, Array.prototype.slice.call( arguments, 1 )); | |
} else if (typeof method === 'object' || !method) { | |
return methods.init.apply(this, arguments); | |
} else { | |
$.error('Method ' + method + ' does not exist on jQuery.tooltip'); | |
} | |
}; | |
})(jQuery); |
This file contains 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
(($) -> | |
publicMethods = ['stop', 'start', 'option', 'effect'] | |
$.fn.ramblingSlider = (options, others...) -> | |
methodExists = options in publicMethods | |
optionsIsString = (typeof options) is 'string' | |
ramblingSlider = @data 'rambling:slider' | |
isCallingGetter = (others) -> not others.length or (others.length is 1 and typeof(others[0]) is 'string') | |
if ramblingSlider | |
return if methodExists | |
value = ramblingSlider[options](others...) | |
if isCallingGetter others | |
value | |
else | |
@ | |
else | |
if optionsIsString | |
$.error "Method '#{options}' not found." | |
else | |
$.error "Slider already initialized." if options | |
else | |
return $.error "Tried to call method '#{options}' on element without slider." if methodExists or optionsIsString | |
@each (key, value) -> | |
element = $ @ | |
return if element.data 'rambling:slider' | |
ramblingSlider = new RamblingSlider @, options | |
element.data 'rambling:slider', ramblingSlider | |
ramblingSlider.initialize() | |
ramblingSlider.run() | |
RamblingSlider = (element, options) -> | |
### | |
# Method definitions | |
### | |
)(jQuery) |
This file contains 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
$('#slider').ramblingSlider('stop'); | |
$('#slider').ramblingSlider('start'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment