in Frontend Tech, technology news

having much fun with backbone & requirejs

I’ve been working on a project that requires intensive javascript right now, so that I went with backbone and requirejs as fundamental framework for the project.

It’s multiple pages project so my structure would look something like this:

Obviously common.js defines paths and shim for require.config that are shared among all pages.

Each html page has require’s initialization code like this:

That way, common js containing require configuration can be shared among pages. Also I did not want to create main.js to reduce a number of javascript files.

apps directory contains page’s main javascript files. So in the example, I have 2 pages, which are home and profile, so I have home.js and profile.js. Those app javascript files are the key piece that has the core business logic to pulls necessary views, models, and necessary lib files for the page.

Everything has been very modularized and between modules, communication occurs via event aggregator, which is yet simple, but very very powerful. Without that, I don’t know how I would have separated logics and made them still modularized.

I really like how you can modularize your javascript code with requirejs. I think that is one of best things that were invented for javascript!