I've decided I need to improve my javascript programming skills, as well as my OO skills. I am currently reading through some books, but sometimes it's hard to get a grip on the theory without seeing some practical examples first. So, I have a theoretical question about "best practices" for the following scenario...
I would like to create an OO script that displays a list of searchtag records retrieved from the server. I also want to be able to edit each searchtag record in place.
Currently I am doing this procedurally with a bit of help from the jQuery library:
I accept a JSON-encoded list of searchtag records from the server. It looks like this:
[
{ "searchTagName" : "tagOne", "searchTagID" : "1" },
{ "searchTagName" : "tagTwo", "searchTagID" : "2" },
{ "searchTagName" : "tagThree", "searchTagID" : "3" },
etc...
]
I dump the JSON directly into jTemplates to create the appropriate html, like so:
$("#searchTagList")
.setTemplateElement("template_searchTagList")
.processTemplate(searchTagData);
Finally I want it to be possible to modify each searchtag with an edit-in-place method, so I attach a pre-built edit-in-place method to each html element:
$(".searchTag").editInPlace();
This works very well procedurally. And maybe the smartest thing would be to leave well enough alone. :) But, for the sake of argument, what is the best way to write something like this from an OO perspective.
Should I have a single object "searchTagList" that has methods for each of the steps discussed above?
var searchTagList =
{
searchTagData: JSONdata,
renderList: function () { /*send to jTemplates */ }
bindEdit: function() { /* attach edit-in-place */ }
}
Or is that incorrect? (It seems like all I'm doing is wrapping my procedural functions in an object.) Should I somehow be parsing the JSON data into instances of each searchtag, and then attaching individual methods to each searchtag? (This seems like a lot of overhead, for no gain.)
Apologies in advance if it seems like I am picking at hairs. But I really want to get this stuff straight in my head.
Thanks,
Travis