How does RequireJS work with multiple pages and partial views?

Update – I’ve added an example of using RequireJS with modular HTML components. Build tool example included – https://github.com/simonsmith/modular-html-requirejs I have also written a blog article about this – http://simonsmith.io/modular-html-components-with-requirejs/ The approach of just using main.js for everything is probably more suited to a single page application. The way I’ve handled this situation is to … Read more

How to load bootstrapped models in Backbone.js while using AMD (require.js)

This is how we bootstrap data in a way that it doesn’t pollute the global namespace. Instead it uses require.js exclusively. It also helps you provide the initial app configuration based on variables within the template. Within your rendered page <script src=”https://stackoverflow.com/questions/9916073/require.js”></script> <script> define(‘config’, function() { return { bootstrappedAccounts: <%= @accounts.to_json %>, bootstrappedProjects: <%= @projects.to_json(:collaborators … Read more

angular-ui-router with requirejs, lazy loading of controller

I created working plunker here. Let’s have this index.html: <!DOCTYPE html> <html> <head> <title>my lazy</title> </head> <body ng-app=”app”> <a href=”#/home”>#/home</a> // we have three states – ‘home’ is NOT lazy <a href=”#/”>#/</a> – index // ‘index’ is lazy, with two views <a href=”#/other”>#/other</a> // ‘other’ is lazy with unnamed view <div data-ui-view=”topMenu”></div> <div data-ui-view=””></div> <script … Read more

How to handle circular dependencies with RequireJS/AMD?

This is indeed a restriction in the AMD format. You could use exports, and that problem goes away. I find exports to be ugly, but it is how regular CommonJS modules solve the problem: define(“Employee”, [“exports”, “Company”], function(exports, Company) { function Employee(name) { this.name = name; this.company = new Company.Company(name + “‘s own company”); }; … Read more

Dynamic require in RequireJS, getting “Module name has not been loaded yet for context” error?

The limitation relates to the simplified CommonJS syntax vs. the normal callback syntax: http://requirejs.org/docs/whyamd.html#commonjscompat https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define Loading a module is inherently an asynchronous process due to the unknown timing of downloading it. However, RequireJS in emulation of the server-side CommonJS spec tries to give you a simplified syntax. When you do something like this: var foomodule … Read more

Loading Backbone and Underscore using RequireJS

RequireJS 2.X now organically addresses non-AMD modules such as Backbone & Underscore much better, using the new shim configuration. The shim configuration is simple to use: (1) one states the dependencies (deps), if any, (which may be from the paths configuration, or may be valid paths themselves). (2) (optionally) specify the global variable name from … Read more

Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

Is there any practical difference [between my examples]? The user may have a JavaScript object created with Object.create(null), which will have a null [[Prototype]] chain, and therefore won’t have hasOwnProperty() available on it. Using your second form would fail to work for this reason. It’s also a safer reference to Object.prototype.hasOwnProperty() (and also shorter). You … Read more

Excluding files/directories from Gulp task

Quick answer On src, you can always specify files to ignore using “!”. Example (you want to exclude all *.min.js files on your js folder and subfolder: gulp.src([‘js/**/*.js’, ‘!js/**/*.min.js’]) You can do it as well for individual files. Expanded answer: Extracted from gulp documentation: gulp.src(globs[, options]) Emits files matching provided glob or an array of … Read more