pattern, entwicklung

small Javascript Objects example

In addition to article "Javascript object notation" I still have a small example lieing around.

Its just a simple table element that is bind to a javascript object that is responsible for controlling the actions related to this table widget - the widgets with its javascript controller part are independend from each other. In theory they could delegate the actions in that widget to a coresponding controller on the server.

To the example

There is one "trick" to mention. We need a way to bind the html element "table" to the object that should control this widget. With jQuery you can do this with the ".data" method - it allows you to bind any stuff to a html element.

So in this case - to ensure that on a click on the "change color" link the correct object answers - we just bind the object itself to the link element:

// register for clicks on the "change color Link"
this.registerEvents=function() {
$('#'+this.id+' .click').data('object',this).click(function(event) { $(this).data('object').clickColorAction(); });
};

blog comments powered by Disqus
blogroll