Appjs
Since repetition is the killer of all productivity, we decided to define a generic way, for handling stuff like modularization and dependency-handling/-injection.
Concepts
Modularization
It's all about the modules you are creating or you want to reuse on a later project. We follow a similar approach as require.js:
app.define('views.TodoList', function () {
var Backbone = app.require('Backbone'),
$ = app.require('jQuery'),
U = app.require('Underscore');
view,
render_helper;
render_helper = function () {
// ...
};
view = Backbone.View.extend({
render: render_helper
});
return view;
});
Effectively, this creates an Object TodoList
in the Namespace views
.
Main advantage is the lazy definition of this helper. The function is not called, until somewhere a call in the later application demands this specific Object:
var TodoList = app.require('views.TodoList'),
$ = app.require('jQuery'),
my_list;
my_list = new TodoList({
el: $('.todo-list')
});
Predefined mediators
Since most of the libraries you can use pollute somehow the global namespace; we where searching for a way, to integrate those tools into our define-require-schema.
Luckily, most of them provide a no-conflict mode. This lead us to some helper
for common libraries. An application.js
could look as following:
//= require underscore
//= require jquery
//= require backbone
//
//= require app
//= require app/mediators/underscore
//= require app/mediators/jquery
//= require app/mediators/backbone
The mediators automatically invoke no-conflict calls and store the main constant(s) of the libraries into a app-definition:
mediators/underscore
providesapp.require('Underscore')
mediators/jquery
providesapp.require('jQuery')
mediators/backbone
providesapp.require('Backbone')
Installation
Add this line to your application's Gemfile:
gem 'appjs'
And then execute:
$ bundle
Or install it yourself as:
$ gem install appjs
Usage
First step would be to add
//= require app
to your application.js. This should be followed by the appropiate mediators, to clean-up your global namespace.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request