Skip to content

ryanflorence/jQuery.addObject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jQuery.addObject

This method allows you to add a JavaScript constructor to the jQuery, and then manage the state of an instance with the jQuery API. For example:

var MyObject = function(selector, options){

	this.jQuery = $(selector);
	this.options = $.extend(this.defaults, options);

}; MyObject.prototype = {

  defaults: {
    one: 1,
    two: 2
  },

  foo: 'bar',

  addArgs: function(arg, arg2, arg3){
    return arg + arg2 + arg3;
  },

  returnThis: function(){
    return this;
  }

};

jQuery.addObject('myNameSpace', MyObject);

Now you can use your object like this:

Create an instance

// Create an instance stored on #element
$('#element').myNameSpace({
  one: 'one',
  two: 'two'
});

Get properties

$('#element').myNameSpace('foo'); // => 'bar'

Set properties

$('#element').myNameSpace('foo', 'baz');
$('#element').myNameSpace('foo'); // => 'baz'

Call methods

$('#element').addArgs(1,2,3); // => 6

Chain

If the method returns this, then through the jQuery API it will return the jQuery.

$('#element').myNameSpace('returnThis').css().myNameSpace('foo', 'bar').fadeOut();

Get the object out of the jQuery API

var instance = $('#element').myNameSpace(); // called with no args, after already instantiated, returns the instance
// or just get it from the data
var instance = $('#element').data('myNameSpace');
instance.addArgs(1,1,1); // => 3
instance.returnThis().foo; // => 'baz'

Running the tests

QUnit is a submodule of this repo. Run it as follows:

git submodule init
git submodule update

Then pop open Test/index.html


This chunk of code is moo4q's twin sister. They both do the same thing, interact with JavaScript objects through jQuery's simple API; moo4q just gets 14k of the rockin' MooTools inheritance API and language extensions as well.

About

This method allows you to add a JavaScript constructor to the jQuery, and then manage the state of an instance with the jQuery API

Resources

Stars

Watchers

Forks

Packages

No packages published