2

Im working on a framework based off bootstrap that will be used across multiple websites. At the moment im just writing a ton of jQuery functions for different events and doesn't seem scalable at all. I been reading up on MVC for js and not sure when it should be used or if its what I need to start applying.

Any suggestions on when its needed?

2
  • 1
    there are other ways to scale.. eg: do you use custom events? Commented Apr 19, 2012 at 13:40
  • Yes, I have a ton of custom events. I tend to duplicate a method just for the new custom event base Commented Apr 19, 2012 at 13:44

3 Answers 3

3

The answer to this is simple ... use MVC when your project gets large enough to warrant it. Many patterns can end up being overkill when you're writing a small project, or simple feature. But there will come a time when not using something like MVC to organize your code will result in an unmaintanable mess. It sounds like you're already hitting that point, so it could definitely make sense to put some thought into how you structure your application.

I actually wrote an article on how you can easily use the pattern from javascript without having to bring in any heavyweight external dependencies:
http://codecube.net/2009/06/mvc-pattern-with-javascript/

Sign up to request clarification or add additional context in comments.

1 Comment

I was reading up on backbone.js but it did seem a little heavy for my needs. This is just a framework with a tons of elements to be reused (similar to twitter's bootstrap), ill take a look at your post on my lunch break!
1

MVC stands for "Model View Controller" and it is a coding paradigm. You can quite easily apply it on the client using JS for webapplications. Models are just plain old JS objects. Views can be implemented using templates (jQuery has templates, but others such as Mustache, Handlebar or Dust are arguably better). And the Controllers are triggered by events and handle the dataflow and processing.

2 Comments

So will picking up one of those template libraries and just using the pattern would be easier then using something like backbone.js? At this point learning that seems to be a bigger learning curve since im not completely solid with MVC.
I think that understanding MVC ist the key to making a decision on what to use. Coding client code in a modular (and maintainable and scalable) fashion is however not dependent on the use of MVC - maybe that's what you should be looking into first. You may want to read the "Web Development Recipes" book which contains (IMHO - I have no affiliation) good information for starters.
1

Take a look at some of the other libraries which help create this sort of structure. My personal favourite is Backbone.js its pretty lightweight but extremely functional.

IMO the best time for structure is from the start, its easier to add to a code structure from the beginning than it is to make code fit into a structure after the fact.

Personally I think structure whatever the choice is, is important especially in JavaScript (Since its so damned mutable) for maintainability, scalability and just plain old helping other people understand what's going on when they have to do a bug fix in code they didn't write! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.