Skip to content Skip to sidebar Skip to footer

Oo Jquery And Classes

I'm working on a site and using JQuery for essentially the first time. I've mostly used MooTools for previous projects, and I have a few widget classes I've written using the MooT

Solution 1:

You might find this approach useful to the task at stake: building an object-oriented jquery plugin.

And this article on Ajaxian "a real OO class system with jquery".

Solution 2:

Hm... interesting. We have jQuery which imho is a great tool to interact with the DOM. It's a selection tool where you can write your own code (plugins) to modify selected items. You can interact here with your own (object oriented) code in a plugin to do something really cool.

So why would you need extra OO capabilities in jQuery unless you want to be able to inherit from other jQuery plugins?

Because you might have a plugin that allows you to do the following:

$(".spiffyness").yourSpiffyficationPlugin1();

And, while doing some really cool awesomeness already, you need more spiffyness on top of this.

Therefore you want to inherit from your first plugin which results in:

$(".spiffyness").yourSpiffyficationPlugin2(); //plugin inherited from 1

But... wouldn't you get there too by doing this:

$(".spiffyness").yourSpiffyficationPlugin1().yourSpiffyficationPlugin2();

Where the second plugin just does this tiny (but awesome ;)) thing extra on top of the first one?

In other words: is what you want, worth the effort for the sake of OO purism? Or is the jQuery pipe mechanism good enough and maybe everything you need?

I would say that separation of responsibilities and/or your mootools mindset might be the real issue here. Let jQuery do what it is good at, use it's powerfull pipe mechanism, pipe your own plugins (which may contain tons of fancy OO stuff)... and you can have great results while your code is still clean.

If you do think I am thinking too simple here, a good example of the essence of your point would be welcome! :-)

And to be ahead of that, if you are doing something really advanced and you do not get away with simply doing something on top of something else, you're plugin could also delegate to one of your OO classes. E.g. like:

$.fn.totalAwesomeness = function(options) {
  var defaults = {
    mode: 1,
    title: 'Awesome'
  };
  var opts = $.extend(defaults, options);
  returnthis.each(function() {
    var $this = $(this);
    var handler = null;
    if(defaults.mode == 1)
       handler = new com.myStuff.class1($this);
    elseif(defaults.mode == 2)
   handler = new com.myStuff.class2($this); //class2 inherits from class1 
    handler.doMagic();
  });
};

Solution 3:

You can always also use moo4q - http://moo4q.com/ . It adds MooTools class support to jQuery.

Solution 4:

I wrote an article some time ago about jQuery object oriented plugins, hope it will be helpful

http://ajax911.com/jquery-object-oriented-plugins/

Solution 5:

There are third party javascript class implementations that provide very powerful introspection capabilites. I would like to particularly highlight JS.Class and Joose. While JS.Class is modeled after Ruby, Joose is modeled after Moose. I am not a mootools user so I can not comment on their advantages/disadvantages with respect to mootools. But I would summarize their key features.

JS.Class has a strong focus on simulating the Object oriented features of Ruby and does a pretty good job at that. It provides a powerful library modeled after the standard library of Ruby and also comes with a well integrated package management and testing framework.

Joose, while provides no testing framework/package management facilites, excels in terms of advanced attribute management facilities, filters and better introspection facilities.

Both of them have really good documentation and can be used in browser as well as server.

Post a Comment for "Oo Jquery And Classes"