Created
February 21, 2010 06:49
-
-
Save ryanflorence/310172 to your computer and use it in GitHub Desktop.
With MooTools Native and Class modules you can write MooTools classes with 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
(function(){ | |
Class.Mutators.jQuery = function(name){ | |
var self = this; | |
jQuery.fn[name] = function(arg){ | |
var args = Array.prototype.slice.call(arguments, 1); | |
if ($type(arg) == 'string'){ | |
var instance = jQuery(this).data(name); | |
if (instance) instance[arg].apply(instance, args); | |
} else { | |
jQuery(this).data(name, new self(this.selector, jQuery.extend(self.prototype.options, arg))); | |
} | |
}; | |
}; | |
})(); | |
/* | |
// Extend the jQuery object with your class | |
var MyClass = new Class({ | |
Implements: [Options, Events], | |
options: { | |
foo: 'bar' | |
}, | |
jQuery: 'MyClass' | |
}); | |
// instantiate the class | |
jQuery('#element').Class(options); | |
// call methods | |
jQuery('#element').Class('method','arg','arg', ...); | |
*/ |
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
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 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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment