Think Simple, Yeoman, Bower, i <3 you but sometimes youâre too heavy!
(sorry for my english ;))
Last week, I had to prepare a draft web application for a demonstration. I decided to play with Yeoman and Backbone Generator. After the yo backbone
command, iâd get a big project of almost 150 MB. ⊠I do not need all this. So, iâd try withn only Bower : 25 MB ⊠I do not need all this.
So I decided to make lighter, iâve created an other (again!) Backbone Boilerplate âmanuallyâ and i use the beautifully simple Pulldown https://github.com/jackfranklin/pulldown to load and update my javascript libraries.
Iâve called it M33 and you can find it here : https://github.com/k33g/m33. And itâs very easy to use.
How to use M33 ?
First you have to install it ⊠And itâs simple
Type git clone --q --depth 0 git@github.com:k33g/m33.git killerapp
where killerapp
is the directory name of your web application (the directory is created).
Then youâve just get the skeleton of your webapp :
M33 structure
# index.html
Bootstrap css file an Requirejs are declared in the header :
data-main="js/main"
explains that main.js
is the main javascript file to load (with the configuration)
# main.js
In main.js
, we declare the vendors javascript libraries and call the initialize
method of application
module :
We explain that application
module is in the application.js
file : require(['application']
and declare it as application
with the parameter function : function (application) {
.
# application.js
The initialize
method of application
module allows us to define dependencies : define(['jquery', ...
, load the Router.js
module : 'routers/Router'
and the ApplicationView.js
module : 'views/ApplicationView'
. And we instanciate the Router
and the ApplicationView
(as parameter of the router).
# views/ApplicationView.js
ApplicationView
is a kind of pattern that allow to manage views, models and collections.
# routers/Router.js
Router.js
aims to check if there is an applicationView, and to define the âroutesâ and there actions.
Now letâs code!
A very quick little sample
We just want display a persons list.
# Data
Create a humans.data.js
file at the root of your webapp directory, with some records :
# Models and Collections : Human and Humans
Create Human.js
(model) and Humans.js
(collection) in the js/models
directory :
Model : Human.js
Collection : Humans.js
You can note :
url : "/humans.data.js"
allows load data when weâll fetch the collection'models/Human'
allows to declare the Humn model in the collection
Now we want to create the view to display the humans.
# Views, templates etc. âŠ
First, go to edit the index.html
file, we want indicate where data will be displayed, inside the tag <div class="human-view"></div>
:
Remark: class="human-view"
will allow to identify the <div>
tag
Now, we can create the template humans.tpl.html
in the js/templates
: (iâm using undrescore templating)
And finally, we are going to create the Backbone.View
. Create HumansView.js
in the js/views
directory :
be careful:
'text!templates/humans.tpl.html'
loads the template, and we can access it through thehumansTpl
variableel : $(".human-view")
allows to the view to âfindâ this :<div class="human-view"></div>
inindex.html
this.listenTo(this.collection, "sync", this.render)
: the view listen to sync events (http request) of the collection, so, when weâll call thefetch
(thesync
event is fired) method of the collection, therender
method of the view will be called.this.trigger("humansAreRendered")
the view trigger a custom event when she has rendered the collection.
# Load data, instanciate the view, display humans
We are going to instanciate HumansView
and Humans
collection in the initialize
method of ApplicationView
.
In the first place, we have to declare the collection : 'models/Humans'
and the view : 'views/HumansView'
. Donât forget to add function parameters Humans, HumansView
:
be careful:
this.humans = new Humans();
: we create a new collectionthis.humansView = new HumansView({ collection : this.humans });
: we create a new view and set the collection property view whitthis.humans
.this.listenTo(this.humansView, "humansAreRendered", this.sendMessage)
: theApplicationView
listen to the custom eventhumansAreRendered
of thehumansView
and callsendMessage
method when the event is fired.- the
sendMessage
method display a message in the html page.
And, thatâall!.
How to test the webapp?
If youâre working with OSX or Linux, you can (inside the webapp directory) run python -m SimpleHTTPServer 8080
and then open http://localhost:8080.
Otherwise you can also install http-server : npm install http-server -g
, then, run http-server -p 8080
and, open http://localhost:8080.
How to update vendors libraries of your webapp
Itâs easy, just run ./loadjs.sh
(OSX or Linux) or loadjs.cmd
in the webapp directory. The script only call pulldown to load libraries.
Et voilĂ !
Tweet