Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Source
Octocat-spinner-32 Tests
Octocat-spinner-32 qunit @ 2b3e049
Octocat-spinner-32 .gitmodules
Octocat-spinner-32 README.md
README.md

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.

Something went wrong with that request. Please try again.