I’ve been playing with jquery for a week or two now and have something that resembles a hackey overgrown example. It needs structure – I’ve worked with MVC before in java etc.. but im finding it hard to translate my large .js file into a number of distinct and logically separated views.
Within these views I’d like to be able create instances of certain widgets (its basically a div with a few buttons and some draggable behaviour). Obviously I’d like to be able to take advantage of inheritance as certain overgrown divs share the same sort of characteristics.
I know how to do this in Java but I cant seem to get my head within javascript. I can get to a point I can isolate a function call which in theory would hit something resembling a controller. At this point I proceed to check certain styles of a parent div etc. and then do a lot of appendTo’s to a particular div thus creating my component.
In short how might I create a little widget and reference it within my jquery topped javascript. I’m not to sure about defining the object and creating event handlers etc for it. I guess I’ve been a hacker of js too long !
If you want an idea at what my current code resembles please see http://jqueryui.com/demos/droppable/#photo-manager I’ve basically added a number of other droppables and have to determine what div im dragging to and then do various things. Again its all kinda hackey. I’d like to make it a bit more engineered.
I’ve started to look at something like Backbone.js but I’d like advise on how to properly create my widgets.
Many thanks SO
I am also in the process of wrangling a dangerously growing jQuery application.
I found this article by Alex Sexton quite helpful: http://alexsexton.com/?p=51 which also says that you should not be afraid to create your own specialised jQuery plugins for your own page to structure your code (and your page).
I also read a lot of articles on class inheritance patterns in Javascript but I found Douglas Crockfords advice the wisest: “Because objects in JavaScript are so flexible, you will want to think differently about class hierarchies. Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.” http://www.crockford.com/javascript/inheritance.html.
So you might have an MVC type pattern at the back of your mind but try to implement it with as little object hierarchy as possible.