Live data from Hacker News

Converting an existing Backbone.js project to Require.js

ozkatz.github.io

1–10 of 34 posts

Re: Converting an existing Backbone.js project to Require.js

#4
post #2

I thought Backbone and Require were 2 separate things?

They are. A better headline would have been something like "refactoring an existing backbone.js project to use require.js". His project still uses backbone.js, but now it also uses require.js.

Re: Converting an existing Backbone.js project to Require.js

#5
I'm really puzzled by the enthusiasm around RequireJS. I've read up on it, I've built several large JS applications (~100 models, views, whatevers in ExtJS and Knockout), have been involved with a large BackboneJS/RequireJS application and am currently rebuilding a large site in AngularJS. AFAICT, RequireJS did not solve any problems that I had more easily solved with proper namespacing (yes, the feared global, e.g. TheProject.controllers.chrome.SessionController) or that AngularJS doesn't solve more gracefully.

Instead, RequireJS yielded a fairly verbose and error-prone header* coupled with large and distinct JS downloads for each and every page. Add in Backbone's verbosity and it's boilerplate and hand-wiring all the way down. Just the kind of thing for when you want to handcraft an artisan website, but not so great for cranking out code.

* Good luck catching typos when you're pulling in 20 modules and you lose synchronization between the strings and the vars. Here's a simple example:

    define([
      'jquery',
      'handlebars',
      'backbone',
      'mymodel',
      'mycontrolleranimals',
      'mycontrollerpeople',
      'myview_cats',
      'myview_dogs',
      'myview_birds'
      ],
    function(
      $,
      Handlebars,
      Backbone,
      MyModel,
      MyControllerAnimals,
      MyControllerPeople,
      MyViewDogs,
      MyViewCats,
      MyViewBirds) {
Sure, you caught the error because you were looking for it, but what happens in a RequireJS application is that you get weird errors in your application because you were trying to use the MyViewDogs, but myview_cats was actually bound to that variable.

To be perfectly honest, I didn't build the system, so don't that it was configured correctly.

Re: Converting an existing Backbone.js project to Require.js

#6

I'm really puzzled by the enthusiasm around RequireJS. I've read up on it, I've built several large JS applications (~100 models, views, whatevers in ExtJS and Knockout), have been involved with a large BackboneJS/RequireJS application and am currently rebuilding a large site in AngularJS. AFAICT, RequireJS did not solve any problems that I had more easily solved with proper namespacing (yes, the feared global , e.g.…

Think about using two different versions of ExtJS, it's just not possible atm. because of globals.

You should look into Dojo Toolkit source code, which now is almost entirely built on AMD.

Regarding "distinct JS downloads for each and every page": again, look at Dojo Toolkit source code and its build system.

Re: Converting an existing Backbone.js project to Require.js

#7

I'm really puzzled by the enthusiasm around RequireJS. I've read up on it, I've built several large JS applications (~100 models, views, whatevers in ExtJS and Knockout), have been involved with a large BackboneJS/RequireJS application and am currently rebuilding a large site in AngularJS. AFAICT, RequireJS did not solve any problems that I had more easily solved with proper namespacing (yes, the feared global , e.g.…

Luckily there's an alternative syntax for requiring modules:

  define(function (require) {
    'use strict';

    var Backbone = require('backbone');
    var Handlebars = require('lib/handlebars/handlebars');
    var someTemplate = require('text!component/templates/some-template.hbs');
http://requirejs.org/docs/whyamd.html#sugar

Re: Converting an existing Backbone.js project to Require.js

#8

I'm really puzzled by the enthusiasm around RequireJS. I've read up on it, I've built several large JS applications (~100 models, views, whatevers in ExtJS and Knockout), have been involved with a large BackboneJS/RequireJS application and am currently rebuilding a large site in AngularJS. AFAICT, RequireJS did not solve any problems that I had more easily solved with proper namespacing (yes, the feared global , e.g.…

Hrm.... I don't think the angular documentation agrees with what you are saying: In the documentation on the $inject annotation:

> Care must be taken that the $inject annotation is kept in sync with the actual arguments in the function declaration.

> This method of annotation is useful for controller declarations since it assigns the annotation information with the function.

Re: Converting an existing Backbone.js project to Require.js

#9

I'm really puzzled by the enthusiasm around RequireJS. I've read up on it, I've built several large JS applications (~100 models, views, whatevers in ExtJS and Knockout), have been involved with a large BackboneJS/RequireJS application and am currently rebuilding a large site in AngularJS. AFAICT, RequireJS did not solve any problems that I had more easily solved with proper namespacing (yes, the feared global , e.g.…

Use this syntax, IMHO it's a LOT more readable:

   define(function(require) {
      var $ = require('jquery'),
          Handlebars = require('handlebars'),
          Backbone = require('backbone'),
          MyModel = require('mymodel'),
          MyControllerAnimals = require('mycontrolleranimals');
          
Also require.js comes with an optimizer that automatically concatenates all required files (and no more!) minifies them and you don't have to worry about script order. Also using the text plugin you can package templates (text files) along with the rest of the scripts.

Re: Converting an existing Backbone.js project to Require.js

#10

I'm really puzzled by the enthusiasm around RequireJS. I've read up on it, I've built several large JS applications (~100 models, views, whatevers in ExtJS and Knockout), have been involved with a large BackboneJS/RequireJS application and am currently rebuilding a large site in AngularJS. AFAICT, RequireJS did not solve any problems that I had more easily solved with proper namespacing (yes, the feared global , e.g.…

as someone refactoring a name spaced project into a requirejs one. Thw big benefit is more or less lexical scope for each page and cleaner entry amd exit points for the parts. Its easy to tell if changes to x break y if y has to import x
Post reply on HN