4 ms·
Use this syntax, IMHO it's a LOT more readable: define(function(require) { var $ = require('jquery'), Handlebars = require('handlebars'),
by ktt 13y ago
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.
- CoffeeDregs 13y agoTotally agreed and you get the benefit that it's the same syntax as NodeJS. But I rarely if ever see that syntax used...
- STRML 13y agoWow, 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.
- nilliams 13y agoAgree, I've used this syntax on my last 2 projects. It is so much better, and closer to Node/CommonJS, I don't know why anyone uses the alternative.