Continuing my comment on the question, jQuery is a potential tool to work with, as it already provides some basics for what you need. However, having said this, he introduces his own difficulties, and, moreover, not all jQuery paths are equal. I suggest one way to use jQuery as an "object model", but it may or may not suit your needs.
First of all. The philosophy of jQuery is that you start everything by first selecting an element using $() or equivalent to jQuery() . All operations conceptually begin with this. This is a slightly different mindset than creating an object that wraps the element and maintains a reference to that wrapper, but essentially this is what jQuery does for you. Calling $('#some-id') calls the element with the identifier "some-id" and transfers it to the jQuery object.
One way: Record Filter plugins.
Replace the constructor with the initFilter() jQuery method. You can do this by modifying the jQuery prototype and using the jQuery object as your wrapper. The jQuery prototype refers to jQuery.fn , therefore:
jQuery.fn.initFilter = function (prop, callback) { // Save prop and callback this.data('filter-prop', prop); this.data('filter-callback', callback); // Bind events (makes sense to do this during init) this.click(function () { $(this).updateFeed(); }); };
Then do a similar thing for updateFeed() :
jQuery.fn.updateFeed = function () { this.data('filter-prop'); this.data('filter-callback')(); });
And use it as follows:
$('#filter').initFilter(prop, callback);
Note that updateFeed can simply be inserted into the click handler to prevent unnecessary pollution of the jQuery namespace. However, one of the advantages of using jQuery like this is that you do not need to keep a reference to the object if you need to call some function on it later, since jQuery associates all the links with the actual elements. If you want to call the updateFeed program, then:
$('#filter').updateFeed();
then will be called on the correct object.
Some things to consider
Of course, there are drawbacks to this method. First, all the properties that we saved against an element using .data() are shared by all jQuery functions that act on that element. I tried to make this easier by pre-assigning the filter names to the property names, but depending on the complexity of your object (s), this may not be acceptable.
Moreover, this exact method may not be so suitable for objects that require a lot of manipulation (i.e. objects with many functions), since all these functions become common to all jQuery objects. There are ways to encapsulate all this, which I will not go into, but jQuery-ui does this with its widgets, and I am experimenting with another alternative in the library being created.
However, backtracking a bit, the only reason I suggested using jQuery is primarily because your Filter object is heavily attached to the DOM. It binds events to the DOM, it modifies the DOM based on user interaction, it mainly lives in the DOM, so use something based on the DOM, i.e. jQuery.