jcontroller: A small and simple controller for javascript

It’s been a long time since the last post, so I’ve though Hey! I’m loosing users, I cannot let that happen, let’s get something from any project I’ve done recently, so I can do a post :p

The thing is that I recently noticed that the javascript I’ve been writing lately becomes messy after some time, after the page and the javascript grows a little bit, there are more events, … If you understand what I’m saying then it’s a good time for you too to change.

So I’ve said to myself: “hey! (I always start talking to myself with “Hey”), so Hey! You can do a couple of things, one intelligent and one really dumb”

  • The intelligent: You can go and get a great controller that is fully documented, tested by the community, and start cleaning the mess. For example using a plugin for jQuery (CorMVC),  or even a framework like JavaScriptMVCJamal
  • Or do the dumbest thing, that is, cerate it yourself

The smart reader probably have guessed the stupid I should be a couple of paragraphs before.

Anyway, here it is. I’m glad to introduce jcontroller and his little brother the jcontroller minified version (compressed using yuicompressor).

This controller has it’s own tests (not included), but basically all those tests are passed in Firefox, Chrome e Internet Explorer. Hold on a second!… Wait I’m going to try something… here it is, it also works on Opera. So it seems it’s working fine with the major browsers (I presume it will also work well in Safari, although I haven’t tried it).

What is jcontroller all about?

Basically it’s like any controller that uses the MVC(Model-View-Controller),  it’s just a way to have kind of pretty code instead of a big mess of code

What about the Model or the View?

Are you kidding? What kind of stupid question is that? Well, is not stupid at all… But in fact, the view is controlled by your browser, and the model… well I have a jdata class, but I’m not ready to share it (this class it’s been working for long long time, but I would have to explain some concepts before sharing, so assume there is nothing else).

jcontroller Example

Now I’m going to show you an example, so you can do whatever you want with it. But before showing you the example, I want to say a few words about the keys of the example:

  • The controller is initiated by the ‘init’ method. If you name your controller “controller” then you should call “controller.init()”
  • The controller will call ‘_init’ method at the beginning, so you might want to define it.
  • The functions on your controller that start by ‘_’ are private and cannot be called by users (except _init, _default and _clear which have a special meaning)
  • When the controller loads, automatically looks at the hash part of the URL and calls the proper handler.
  • (ignore this paragraph now) Right now everything works for anchors for the same page, like <a href=”#whatever”> but you can implement your own trigger by returning your own function on the ‘_init’ method
  • (ignore also this paragraph) You can define as many controllers as you want, each controller will be independent of the others, BUT if the controllers are triggered by the same elements, expect them to execute their actions at the same time.

Here are the links for the example:

The example is really simple. It’s that simple that I feel stupid for writing it, but anyway, have a look at it and you will start to understand the capabilities of jcontroller.

Note: Right now jcontroller uses jQuery, but I don’t think it takes more than 10 minutes to change it by any other framework.

Finally, here are the links (again) to jcontroller, so you can download them again:

So, nothing else to say. If you like it, you can use it as you wish (commercially, personally, academically, *ally). If you don’t like it, then I encourage you to print the jcontroller.js in paper and then break it in a hundred pieces, you will probably feel better (you can also leave a constructive message, but just after calming down).

Constructive comments and suggestions are always welcome, and destructive comments are ignored.

Cheers 😉

Trackback URL

, ,

  1. kanian
    21/06/2011 at 5:41 am Permalink

    I just relaised that everything becomes much easier when I decide to keep the Model, View and Controller loosely coupled. All I do is share messages, not functions. So no tight coupling can arise from function signature. So, naturally, the Pub/Sub pattern becomes the cornerstone of approach to MVC. Here is an example I wrote: http://kanian77.wordpress.com/2011/06/21/simple-javascript-mvc/ .