-
-
Save appden/376133 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
Class.Mutators.jQuery = function(name){ | |
var self = this; | |
jQuery.fn[name] = function(arg){ | |
var instance = this.data(name); | |
if ($type(arg) == 'string'){ | |
var prop = instance[arg]; | |
if ($type(prop) == 'function'){ | |
var returns = prop.apply(instance, Array.slice(arguments, 1)); | |
return (returns == instance) ? this : returns; | |
} else if (arguments.length == 1){ | |
return prop; | |
} | |
instance[arg] = arguments[1]; | |
} else { | |
if (instance) return instance; | |
this.data(name, new self(this.selector, arg)); | |
} | |
return this; | |
}; | |
}; |
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
var Person = new Class({ | |
Implements: Options, | |
options: { | |
height: 'tall', | |
weight: 'fat' | |
}, | |
jQuery: 'person', // must be after options definition | |
initialize: function(selector, options){ | |
this.setOptions(options); | |
this.jqueryObject = jQuery(selector); | |
}, | |
dance: function(whichDance){ | |
// dance the whichDance | |
}, | |
combust: function(){ | |
// combust | |
} | |
}); |
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
// instantiate the class | |
var instance = new Person('#dude',{ height: 'short' }); | |
jQuery('#dude').person({ height: 'short' }); | |
// call methods | |
instance.dance('salsa'); | |
jQuery('#dude').person('dance','salsa').person('otherMethod'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment