Live data from Hacker News

Converting an existing Backbone.js project to Require.js

ozkatz.github.io

21–30 of 34 posts

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

#21

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.…

I have also maintained or architected two large JS applications (100k+ lines of javascript) in ExtJS, Knockout, Kendo and React and I couldn't imagine not having requirejs, for the benefits of enforcing modularity and sane dependency structure, just as much as the sane approach to an optimized build and the plugin system.

The pain point you describe is not a pain point for my team, and even if it was a pain point, it is perfectly possible to abstract the repetition[1], which you are of course aware of since you use Angular. FWIW, we have not felt the need to have any modules with 20 includes.

[1] http://www.dustingetz.com/2013/05/13/javascript-dependency-i...

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

#22
Perhaps someone can clue me in, because I'm still at a loss for why there's so much hype built around Require.js.

I develop JS primarily for the frontend, and I use Grunt, a global namespace, and concat (or similar tooling) to piece things together.

Doesn't r.js execute dependency resolution code at run time? Can all references to r.js be fully compiled out? Even a wrapper like Almond seems to leave behind this kind of extra cruft. I don't want any dependency resolution code in the production build, because that's a problem that needs to be solved at compile time.

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

#23

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.…

You can put some of that stuff into a core utility function and then just use require('core') everywhere else.

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

#24

Just use npm and browserify. Way simpler and you are already using npm already. Let each module manage the dependencies it needs.

Exactly. The benefits of having npm as a package manager really cannot be overstated.

In my experience, requirejs has been nothing but one over-complicated under-documented and inflexible mess

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

#25

Perhaps someone can clue me in, because I'm still at a loss for why there's so much hype built around Require.js. I develop JS primarily for the frontend, and I use Grunt, a global namespace, and concat (or similar tooling) to piece things together. Doesn't r.js execute dependency resolution code at run time? Can all references to r.js be fully compiled out? Even a wrapper like Almond seems to leave behind this kind…

I was thinking the same thing. Why would anyone want this?

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

#26
post #19
post #17

Minor nitpick which might simplify your life - instead of declaring dependencies in the header (i.e. define(['xxx'],..)), just use any of your dependencies with require('') inside your code. I.e. do something like this: define(function() { var $ = require("jquery"); }); This will require one module per file and either define aliases inside config.js or using full path to the modules, but will not push you to define a…

Offtopic: your employer MyEdu constantly spammed me throughout college with emails pretending to be from a university affiliate, when you had just slurped everyone's email from the public contact database. The intent was clearly to deceive and misrepresent the nature of the email. Shame on you for working for a spammy and exploitative business.

I am sorry to hear that. Could you please email details to pavel.karoukin@myedu.com so we could figure out what was going on and possible make it right?

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

#29
post #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…

Wow, I've been using require.js for more than a year and I've never seen that syntax used. `var module = require('module')` will only work if the module has already been downloaded; otherwise, there's no way it can make an async call and return the result.

What's interesting is that requireJS pulls a move very similar to Angular and reads Function.prototype.toString() to pull those definitions back up into the define() call, so it still works if an async call needs to be made. That's really fantastic. As we've seen with Angular, there are some quirks (old browsers, mainly), but code like this will be optimized out by r.js so there will be no problems in production.

Thanks for the pointer, I may just change all of my definitions after seeing this.

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

#30

Has anybody gotten Require.js to work with a CDN (say Cloudfront) and on IE browsers (below IE9) ? We had faced poor load time performance because of this.

If you aren't already, and depending on how big your app is, you may want to compile your app into a single file using r.js. Keep in mind, you lose the "load only what the current view needs" aspect of it. I've usually found it to be a better loading experience when compiled.
Post reply on HN