Search code examples
javascript-frameworkchaplinjs

Namespaces using Chaplinjs


I have just started using Chapling with JavaScript as the language for writing code. Question is how namespaces are handled while developing application using ChaplinJs?

Does Chaplinjs framework automatically takes care of the namespaces of the models, views and controllers and create them inside the extended application object OR we have to manage it manually?

e.g.

In my old project we used to have

(function(global) {
    who = global.mycompany || {};
    who.ui = {};
    who.app = who.app || {};
    who.models = {};
    who.templates = {};
})(typeof(window) === undefined ? this : window);

In our new Chaplin based project, we have just defined

var MyApplication = Chaplin.Application.extend({
//all init logic goes here

return MyApplication;
});

Solution

  • Chaplinjs automatically takes care of the objects, and allocates and deallocates them as necessary. The only thing you might want to think about is those controllers that you want to keep all the time for efficiency reasons, such as a header view controller. Take a look inside application.coffee (or the JS version if you don't use CoffeeScript).

    Of course if you want to reference a specific object within your code, you should consider putting it in an accessible variable at the right time. The controller will always have the ability to reference its views and models anyway, so that's not an issue. It's only where you have to cross-reference another object, which generally you'd want always active. So you'd put it in your application object (see above) and then there's no issue finding it.