├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── tools ├── build.js └── r.js └── www ├── js ├── app │ ├── controller │ │ ├── Base.js │ │ ├── c1.js │ │ └── c2.js │ ├── lib.js │ ├── main1.js │ ├── main2.js │ └── model │ │ ├── Base.js │ │ ├── m1.js │ │ └── m2.js ├── common.js └── lib │ ├── backbone.js │ ├── jquery.js │ ├── require.js │ └── underscore.js ├── page1.html └── page2.html /.gitignore: -------------------------------------------------------------------------------- 1 | www-built 2 | www-ghpages -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright and related rights for this sample code are waived via CC0. Utilities 2 | and libraries in the sample code that are externally maintained have their own 3 | licenses; we recommend you read them, as their terms may differ from the sample 4 | code. 5 | 6 | CC0: http://creativecommons.org/publicdomain/zero/1.0/ 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # requirejs/example-multipage-shim 2 | 3 | This project shows how to set up a multi-page requirejs-based project that has 4 | the following goals: 5 | 6 | * Each page uses a mix of common and page-specific modules. 7 | * All pages share the same requirejs config. 8 | * After an optimization build, the common items should be in a shared common 9 | layer, and the page-specific modules should be in a page-specific layer. 10 | * The HTML page should not have to be changed after doing the build. 11 | * **[shim config](http://requirejs.org/docs/api.html#config-shim)** is used to 12 | load Backbone and underscore. 13 | 14 | This **project is different** from the standard 15 | [requirejs/example-multipage](https://github.com/requirejs/example-multipage) 16 | because [shim config](http://requirejs.org/docs/api.html#config-shim) 17 | is used. Shimmed modules need their dependencies loaded before they are executed. 18 | It is not as robust as normal modules. Additionally, the common.js file has 19 | shim config in it. See the js/app/main1.js file for the Backbone and underscore 20 | use. 21 | 22 | The shim config requires shimmed dependencies to be loaded first, before 23 | the shimmed script is executed. So instead of using `data-main="js/page1"` 24 | approach as used in example-multipage, this example inlines the requirejs calls 25 | in the HTML for the page. 26 | 27 | If data-main was used instead, then there would need to be a 'js/page1' that 28 | would contain the <script> contents in page1.html, but that js/page1.js could 29 | not be the target of the optimization step, since it would inline the 30 | 'app/main1' dependencies above the requirejs() call for 'js/common'. If 'js/page1' 31 | used a shimmed script, but the shim dependency was in 'js/common', then the 32 | shimmed code would execute before the common layer that contains the shim 33 | dependency loads. 34 | 35 | Since 'js/page1' cannot have any other modules inlined in its 36 | built version, it would add an extra HTTP request to use the 'js/page1' approach. 37 | By inlining that code in the HTML file, that extra HTTP request is avoided. If 38 | shim config is not needed, then the setup is simpler, see 39 | [requirejs/example-multipage](https://github.com/requirejs/example-multipage). 40 | 41 | ## Getting this project template 42 | 43 | If you are using [volo](https://github.com/volojs/volo): 44 | 45 | volo create projectname requirejs/example-multipage-shim 46 | 47 | Otherwise, 48 | [download latest zipball of master](https://github.com/requirejs/example-multipage-shim/zipball/master). 49 | 50 | ## Project layout 51 | 52 | This project has the following layout: 53 | 54 | * **tools**: The requirejs optimizer, **r.js**, and the optimizer config, 55 | **build.js.** 56 | * **www**: The code that runs in the browser while in development mode. 57 | * **www-built**: Generated after an optimizer build. Contains the built code 58 | that can be deployed to the live site. 59 | 60 | This **www** has the following layout: 61 | 62 | * **page1.html**: page 1 of the app. 63 | * **page2.html**: page 2 of the app. 64 | * **js** 65 | * **app**: the directory to store app-specific modules. 66 | * **lib**: the directory to hold third party modules, like jQuery. 67 | * **common.js**: contains the requirejs config, and it will be the build 68 | target for the set of common modules. 69 | 70 | To optimize, run: 71 | 72 | node tools/r.js -o tools/build.js 73 | 74 | That build command creates an optimized version of the project in a 75 | **www-built** directory. The **js/common.js** file will contain all the common 76 | modules. **js/app/main1.js** will contain the main1-specific modules, 77 | **js/app/main2.js** will contain the main2-specific modules. 78 | 79 | This means that for page 1, after an optimization, there will be two scripts 80 | loaded: 81 | 82 | * js/common.js 83 | * js/app/main1.js 84 | 85 | ## Building up the common layer 86 | 87 | As you do builds and see in the build output that each page is including the 88 | same module, add it to common's "include" array in **tools/build.js**. 89 | 90 | It is better to add these common modules to the **tools/build.js** config 91 | instead of doing a requirejs([]) call for them in **js/common.js**. Modules that 92 | are not explicitly required at runtime are not executed when added to common.js 93 | via the include build option. So by using **tools/build.js**, you can include 94 | common modules that may be in 2-3 pages but not all pages. For pages that do 95 | not need a particular common module, it will not be executed. If you put in a 96 | requirejs() call for it in **js/common.js**, then it will always be executed. 97 | 98 | ## More info 99 | 100 | For more information on the optimizer: 101 | http://requirejs.org/docs/optimization.html 102 | 103 | For more information on using requirejs: 104 | http://requirejs.org/docs/api.html 105 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "amd": {}, 3 | "volo": { 4 | "baseUrl": "www/js/lib", 5 | "dependencies": { 6 | "jquery": "github:jquery/jquery/1.8.0", 7 | "underscore": "github:documentcloud/underscore/1.3.3", 8 | "backbone": "github:documentcloud/backbone/0.9.2" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tools/build.js: -------------------------------------------------------------------------------- 1 | { 2 | appDir: '../www', 3 | mainConfigFile: '../www/js/common.js', 4 | dir: '../www-built', 5 | modules: [ 6 | //First set up the common build layer. 7 | { 8 | //module names are relative to baseUrl 9 | name: '../common', 10 | //List common dependencies here. Only need to list 11 | //top level dependencies, "include" will find 12 | //nested dependencies. 13 | include: ['jquery', 14 | 'app/lib', 15 | 'app/controller/Base', 16 | 'app/model/Base' 17 | ] 18 | }, 19 | 20 | //Now set up a build layer for each main layer, but exclude 21 | //the common one. "exclude" will exclude nested 22 | //the nested, built dependencies from "common". Any 23 | //"exclude" that includes built modules should be 24 | //listed before the build layer that wants to exclude it. 25 | //The "page1" and "page2" modules are **not** the targets of 26 | //the optimization, because shim config is in play, and 27 | //shimmed dependencies need to maintain their load order. 28 | //In this example, common.js will hold jquery, so backbone 29 | //needs to be delayed from loading until common.js finishes. 30 | //That loading sequence is controlled in page1.html. 31 | { 32 | //module names are relative to baseUrl/paths config 33 | name: 'app/main1', 34 | exclude: ['../common'] 35 | }, 36 | 37 | { 38 | //module names are relative to baseUrl 39 | name: 'app/main2', 40 | exclude: ['../common'] 41 | } 42 | 43 | ] 44 | } 45 | -------------------------------------------------------------------------------- /www/js/app/controller/Base.js: -------------------------------------------------------------------------------- 1 | define(function () { 2 | function controllerBase(id) { 3 | this.id = id; 4 | } 5 | 6 | controllerBase.prototype = { 7 | setModel: function (model) { 8 | this.model = model; 9 | }, 10 | 11 | render: function (bodyDom) { 12 | bodyDom.prepend('

Controller ' + this.id + ' says "' + 13 | this.model.getTitle() + '"

'); 14 | } 15 | }; 16 | 17 | return controllerBase; 18 | }); 19 | -------------------------------------------------------------------------------- /www/js/app/controller/c1.js: -------------------------------------------------------------------------------- 1 | define(['./Base'], function (Base) { 2 | var c1 = new Base('Controller 1'); 3 | return c1; 4 | }); 5 | -------------------------------------------------------------------------------- /www/js/app/controller/c2.js: -------------------------------------------------------------------------------- 1 | define(['./Base'], function (Base) { 2 | var c2 = new Base('Controller 2'); 3 | return c2; 4 | }); 5 | -------------------------------------------------------------------------------- /www/js/app/lib.js: -------------------------------------------------------------------------------- 1 | define(['jquery'], function ($) { 2 | return { 3 | getBody: function () { 4 | return $('body'); 5 | } 6 | } 7 | }); 8 | -------------------------------------------------------------------------------- /www/js/app/main1.js: -------------------------------------------------------------------------------- 1 | define(function (require) { 2 | var $ = require('jquery'), 3 | lib = require('./lib'), 4 | controller = require('./controller/c1'), 5 | model = require('./model/m1'), 6 | backbone = require('backbone'), 7 | underscore = require('underscore'); 8 | 9 | //A fabricated API to show interaction of 10 | //common and specific pieces. 11 | controller.setModel(model); 12 | $(function () { 13 | controller.render(lib.getBody()); 14 | 15 | //Display backbone and underscore versions 16 | $('body') 17 | .append('
backbone version: ' + backbone.VERSION + '
') 18 | .append('
underscore version: ' + underscore.VERSION + '
'); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /www/js/app/main2.js: -------------------------------------------------------------------------------- 1 | define(function (require) { 2 | var $ = require('jquery'), 3 | lib = require('./lib'), 4 | controller = require('./controller/c2'), 5 | model = require('./model/m2'); 6 | 7 | //A fabricated API to show interaction of 8 | //common and specific pieces. 9 | controller.setModel(model); 10 | $(function () { 11 | controller.render(lib.getBody()); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /www/js/app/model/Base.js: -------------------------------------------------------------------------------- 1 | define(function () { 2 | function modelBase(title) { 3 | this.title = title; 4 | } 5 | 6 | modelBase.prototype = { 7 | getTitle: function () { 8 | return this.title; 9 | } 10 | }; 11 | 12 | return modelBase; 13 | }); 14 | -------------------------------------------------------------------------------- /www/js/app/model/m1.js: -------------------------------------------------------------------------------- 1 | define(['./Base'], function (Base) { 2 | var m1 = new Base('This is the data for Page 1'); 3 | return m1; 4 | }); 5 | -------------------------------------------------------------------------------- /www/js/app/model/m2.js: -------------------------------------------------------------------------------- 1 | define(['./Base'], function (Base) { 2 | var m2 = new Base('This is the data for Page 2'); 3 | return m2; 4 | }); 5 | -------------------------------------------------------------------------------- /www/js/common.js: -------------------------------------------------------------------------------- 1 | //The build will inline common dependencies into this file. 2 | 3 | //For any third party dependencies, like jQuery, place them in the lib folder. 4 | 5 | //Configure loading modules from the lib directory, 6 | //except for 'app' ones, which are in a sibling 7 | //directory. 8 | requirejs.config({ 9 | baseUrl: 'js/lib', 10 | paths: { 11 | app: '../app' 12 | }, 13 | shim: { 14 | backbone: { 15 | deps: ['jquery', 'underscore'], 16 | exports: 'Backbone' 17 | }, 18 | underscore: { 19 | exports: '_' 20 | } 21 | } 22 | }); 23 | -------------------------------------------------------------------------------- /www/js/lib/backbone.js: -------------------------------------------------------------------------------- 1 | // Backbone.js 0.9.2 2 | 3 | // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. 4 | // Backbone may be freely distributed under the MIT license. 5 | // For all details and documentation: 6 | // http://backbonejs.org 7 | 8 | (function(){ 9 | 10 | // Initial Setup 11 | // ------------- 12 | 13 | // Save a reference to the global object (`window` in the browser, `global` 14 | // on the server). 15 | var root = this; 16 | 17 | // Save the previous value of the `Backbone` variable, so that it can be 18 | // restored later on, if `noConflict` is used. 19 | var previousBackbone = root.Backbone; 20 | 21 | // Create a local reference to slice/splice. 22 | var slice = Array.prototype.slice; 23 | var splice = Array.prototype.splice; 24 | 25 | // The top-level namespace. All public Backbone classes and modules will 26 | // be attached to this. Exported for both CommonJS and the browser. 27 | var Backbone; 28 | if (typeof exports !== 'undefined') { 29 | Backbone = exports; 30 | } else { 31 | Backbone = root.Backbone = {}; 32 | } 33 | 34 | // Current version of the library. Keep in sync with `package.json`. 35 | Backbone.VERSION = '0.9.2'; 36 | 37 | // Require Underscore, if we're on the server, and it's not already present. 38 | var _ = root._; 39 | if (!_ && (typeof require !== 'undefined')) _ = require('underscore'); 40 | 41 | // For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable. 42 | var $ = root.jQuery || root.Zepto || root.ender; 43 | 44 | // Set the JavaScript library that will be used for DOM manipulation and 45 | // Ajax calls (a.k.a. the `$` variable). By default Backbone will use: jQuery, 46 | // Zepto, or Ender; but the `setDomLibrary()` method lets you inject an 47 | // alternate JavaScript library (or a mock library for testing your views 48 | // outside of a browser). 49 | Backbone.setDomLibrary = function(lib) { 50 | $ = lib; 51 | }; 52 | 53 | // Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable 54 | // to its previous owner. Returns a reference to this Backbone object. 55 | Backbone.noConflict = function() { 56 | root.Backbone = previousBackbone; 57 | return this; 58 | }; 59 | 60 | // Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option 61 | // will fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and 62 | // set a `X-Http-Method-Override` header. 63 | Backbone.emulateHTTP = false; 64 | 65 | // Turn on `emulateJSON` to support legacy servers that can't deal with direct 66 | // `application/json` requests ... will encode the body as 67 | // `application/x-www-form-urlencoded` instead and will send the model in a 68 | // form param named `model`. 69 | Backbone.emulateJSON = false; 70 | 71 | // Backbone.Events 72 | // ----------------- 73 | 74 | // Regular expression used to split event strings 75 | var eventSplitter = /\s+/; 76 | 77 | // A module that can be mixed in to *any object* in order to provide it with 78 | // custom events. You may bind with `on` or remove with `off` callback functions 79 | // to an event; trigger`-ing an event fires all callbacks in succession. 80 | // 81 | // var object = {}; 82 | // _.extend(object, Backbone.Events); 83 | // object.on('expand', function(){ alert('expanded'); }); 84 | // object.trigger('expand'); 85 | // 86 | var Events = Backbone.Events = { 87 | 88 | // Bind one or more space separated events, `events`, to a `callback` 89 | // function. Passing `"all"` will bind the callback to all events fired. 90 | on: function(events, callback, context) { 91 | 92 | var calls, event, node, tail, list; 93 | if (!callback) return this; 94 | events = events.split(eventSplitter); 95 | calls = this._callbacks || (this._callbacks = {}); 96 | 97 | // Create an immutable callback list, allowing traversal during 98 | // modification. The tail is an empty object that will always be used 99 | // as the next node. 100 | while (event = events.shift()) { 101 | list = calls[event]; 102 | node = list ? list.tail : {}; 103 | node.next = tail = {}; 104 | node.context = context; 105 | node.callback = callback; 106 | calls[event] = {tail: tail, next: list ? list.next : node}; 107 | } 108 | 109 | return this; 110 | }, 111 | 112 | // Remove one or many callbacks. If `context` is null, removes all callbacks 113 | // with that function. If `callback` is null, removes all callbacks for the 114 | // event. If `events` is null, removes all bound callbacks for all events. 115 | off: function(events, callback, context) { 116 | var event, calls, node, tail, cb, ctx; 117 | 118 | // No events, or removing *all* events. 119 | if (!(calls = this._callbacks)) return; 120 | if (!(events || callback || context)) { 121 | delete this._callbacks; 122 | return this; 123 | } 124 | 125 | // Loop through the listed events and contexts, splicing them out of the 126 | // linked list of callbacks if appropriate. 127 | events = events ? events.split(eventSplitter) : _.keys(calls); 128 | while (event = events.shift()) { 129 | node = calls[event]; 130 | delete calls[event]; 131 | if (!node || !(callback || context)) continue; 132 | // Create a new list, omitting the indicated callbacks. 133 | tail = node.tail; 134 | while ((node = node.next) !== tail) { 135 | cb = node.callback; 136 | ctx = node.context; 137 | if ((callback && cb !== callback) || (context && ctx !== context)) { 138 | this.on(event, cb, ctx); 139 | } 140 | } 141 | } 142 | 143 | return this; 144 | }, 145 | 146 | // Trigger one or many events, firing all bound callbacks. Callbacks are 147 | // passed the same arguments as `trigger` is, apart from the event name 148 | // (unless you're listening on `"all"`, which will cause your callback to 149 | // receive the true name of the event as the first argument). 150 | trigger: function(events) { 151 | var event, node, calls, tail, args, all, rest; 152 | if (!(calls = this._callbacks)) return this; 153 | all = calls.all; 154 | events = events.split(eventSplitter); 155 | rest = slice.call(arguments, 1); 156 | 157 | // For each event, walk through the linked list of callbacks twice, 158 | // first to trigger the event, then to trigger any `"all"` callbacks. 159 | while (event = events.shift()) { 160 | if (node = calls[event]) { 161 | tail = node.tail; 162 | while ((node = node.next) !== tail) { 163 | node.callback.apply(node.context || this, rest); 164 | } 165 | } 166 | if (node = all) { 167 | tail = node.tail; 168 | args = [event].concat(rest); 169 | while ((node = node.next) !== tail) { 170 | node.callback.apply(node.context || this, args); 171 | } 172 | } 173 | } 174 | 175 | return this; 176 | } 177 | 178 | }; 179 | 180 | // Aliases for backwards compatibility. 181 | Events.bind = Events.on; 182 | Events.unbind = Events.off; 183 | 184 | // Backbone.Model 185 | // -------------- 186 | 187 | // Create a new model, with defined attributes. A client id (`cid`) 188 | // is automatically generated and assigned for you. 189 | var Model = Backbone.Model = function(attributes, options) { 190 | var defaults; 191 | attributes || (attributes = {}); 192 | if (options && options.parse) attributes = this.parse(attributes); 193 | if (defaults = getValue(this, 'defaults')) { 194 | attributes = _.extend({}, defaults, attributes); 195 | } 196 | if (options && options.collection) this.collection = options.collection; 197 | this.attributes = {}; 198 | this._escapedAttributes = {}; 199 | this.cid = _.uniqueId('c'); 200 | this.changed = {}; 201 | this._silent = {}; 202 | this._pending = {}; 203 | this.set(attributes, {silent: true}); 204 | // Reset change tracking. 205 | this.changed = {}; 206 | this._silent = {}; 207 | this._pending = {}; 208 | this._previousAttributes = _.clone(this.attributes); 209 | this.initialize.apply(this, arguments); 210 | }; 211 | 212 | // Attach all inheritable methods to the Model prototype. 213 | _.extend(Model.prototype, Events, { 214 | 215 | // A hash of attributes whose current and previous value differ. 216 | changed: null, 217 | 218 | // A hash of attributes that have silently changed since the last time 219 | // `change` was called. Will become pending attributes on the next call. 220 | _silent: null, 221 | 222 | // A hash of attributes that have changed since the last `'change'` event 223 | // began. 224 | _pending: null, 225 | 226 | // The default name for the JSON `id` attribute is `"id"`. MongoDB and 227 | // CouchDB users may want to set this to `"_id"`. 228 | idAttribute: 'id', 229 | 230 | // Initialize is an empty function by default. Override it with your own 231 | // initialization logic. 232 | initialize: function(){}, 233 | 234 | // Return a copy of the model's `attributes` object. 235 | toJSON: function(options) { 236 | return _.clone(this.attributes); 237 | }, 238 | 239 | // Get the value of an attribute. 240 | get: function(attr) { 241 | return this.attributes[attr]; 242 | }, 243 | 244 | // Get the HTML-escaped value of an attribute. 245 | escape: function(attr) { 246 | var html; 247 | if (html = this._escapedAttributes[attr]) return html; 248 | var val = this.get(attr); 249 | return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + val); 250 | }, 251 | 252 | // Returns `true` if the attribute contains a value that is not null 253 | // or undefined. 254 | has: function(attr) { 255 | return this.get(attr) != null; 256 | }, 257 | 258 | // Set a hash of model attributes on the object, firing `"change"` unless 259 | // you choose to silence it. 260 | set: function(key, value, options) { 261 | var attrs, attr, val; 262 | 263 | // Handle both 264 | if (_.isObject(key) || key == null) { 265 | attrs = key; 266 | options = value; 267 | } else { 268 | attrs = {}; 269 | attrs[key] = value; 270 | } 271 | 272 | // Extract attributes and options. 273 | options || (options = {}); 274 | if (!attrs) return this; 275 | if (attrs instanceof Model) attrs = attrs.attributes; 276 | if (options.unset) for (attr in attrs) attrs[attr] = void 0; 277 | 278 | // Run validation. 279 | if (!this._validate(attrs, options)) return false; 280 | 281 | // Check for changes of `id`. 282 | if (this.idAttribute in attrs) this.id = attrs[this.idAttribute]; 283 | 284 | var changes = options.changes = {}; 285 | var now = this.attributes; 286 | var escaped = this._escapedAttributes; 287 | var prev = this._previousAttributes || {}; 288 | 289 | // For each `set` attribute... 290 | for (attr in attrs) { 291 | val = attrs[attr]; 292 | 293 | // If the new and current value differ, record the change. 294 | if (!_.isEqual(now[attr], val) || (options.unset && _.has(now, attr))) { 295 | delete escaped[attr]; 296 | (options.silent ? this._silent : changes)[attr] = true; 297 | } 298 | 299 | // Update or delete the current value. 300 | options.unset ? delete now[attr] : now[attr] = val; 301 | 302 | // If the new and previous value differ, record the change. If not, 303 | // then remove changes for this attribute. 304 | if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) { 305 | this.changed[attr] = val; 306 | if (!options.silent) this._pending[attr] = true; 307 | } else { 308 | delete this.changed[attr]; 309 | delete this._pending[attr]; 310 | } 311 | } 312 | 313 | // Fire the `"change"` events. 314 | if (!options.silent) this.change(options); 315 | return this; 316 | }, 317 | 318 | // Remove an attribute from the model, firing `"change"` unless you choose 319 | // to silence it. `unset` is a noop if the attribute doesn't exist. 320 | unset: function(attr, options) { 321 | (options || (options = {})).unset = true; 322 | return this.set(attr, null, options); 323 | }, 324 | 325 | // Clear all attributes on the model, firing `"change"` unless you choose 326 | // to silence it. 327 | clear: function(options) { 328 | (options || (options = {})).unset = true; 329 | return this.set(_.clone(this.attributes), options); 330 | }, 331 | 332 | // Fetch the model from the server. If the server's representation of the 333 | // model differs from its current attributes, they will be overriden, 334 | // triggering a `"change"` event. 335 | fetch: function(options) { 336 | options = options ? _.clone(options) : {}; 337 | var model = this; 338 | var success = options.success; 339 | options.success = function(resp, status, xhr) { 340 | if (!model.set(model.parse(resp, xhr), options)) return false; 341 | if (success) success(model, resp); 342 | }; 343 | options.error = Backbone.wrapError(options.error, model, options); 344 | return (this.sync || Backbone.sync).call(this, 'read', this, options); 345 | }, 346 | 347 | // Set a hash of model attributes, and sync the model to the server. 348 | // If the server returns an attributes hash that differs, the model's 349 | // state will be `set` again. 350 | save: function(key, value, options) { 351 | var attrs, current; 352 | 353 | // Handle both `("key", value)` and `({key: value})` -style calls. 354 | if (_.isObject(key) || key == null) { 355 | attrs = key; 356 | options = value; 357 | } else { 358 | attrs = {}; 359 | attrs[key] = value; 360 | } 361 | options = options ? _.clone(options) : {}; 362 | 363 | // If we're "wait"-ing to set changed attributes, validate early. 364 | if (options.wait) { 365 | if (!this._validate(attrs, options)) return false; 366 | current = _.clone(this.attributes); 367 | } 368 | 369 | // Regular saves `set` attributes before persisting to the server. 370 | var silentOptions = _.extend({}, options, {silent: true}); 371 | if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) { 372 | return false; 373 | } 374 | 375 | // After a successful server-side save, the client is (optionally) 376 | // updated with the server-side state. 377 | var model = this; 378 | var success = options.success; 379 | options.success = function(resp, status, xhr) { 380 | var serverAttrs = model.parse(resp, xhr); 381 | if (options.wait) { 382 | delete options.wait; 383 | serverAttrs = _.extend(attrs || {}, serverAttrs); 384 | } 385 | if (!model.set(serverAttrs, options)) return false; 386 | if (success) { 387 | success(model, resp); 388 | } else { 389 | model.trigger('sync', model, resp, options); 390 | } 391 | }; 392 | 393 | // Finish configuring and sending the Ajax request. 394 | options.error = Backbone.wrapError(options.error, model, options); 395 | var method = this.isNew() ? 'create' : 'update'; 396 | var xhr = (this.sync || Backbone.sync).call(this, method, this, options); 397 | if (options.wait) this.set(current, silentOptions); 398 | return xhr; 399 | }, 400 | 401 | // Destroy this model on the server if it was already persisted. 402 | // Optimistically removes the model from its collection, if it has one. 403 | // If `wait: true` is passed, waits for the server to respond before removal. 404 | destroy: function(options) { 405 | options = options ? _.clone(options) : {}; 406 | var model = this; 407 | var success = options.success; 408 | 409 | var triggerDestroy = function() { 410 | model.trigger('destroy', model, model.collection, options); 411 | }; 412 | 413 | if (this.isNew()) { 414 | triggerDestroy(); 415 | return false; 416 | } 417 | 418 | options.success = function(resp) { 419 | if (options.wait) triggerDestroy(); 420 | if (success) { 421 | success(model, resp); 422 | } else { 423 | model.trigger('sync', model, resp, options); 424 | } 425 | }; 426 | 427 | options.error = Backbone.wrapError(options.error, model, options); 428 | var xhr = (this.sync || Backbone.sync).call(this, 'delete', this, options); 429 | if (!options.wait) triggerDestroy(); 430 | return xhr; 431 | }, 432 | 433 | // Default URL for the model's representation on the server -- if you're 434 | // using Backbone's restful methods, override this to change the endpoint 435 | // that will be called. 436 | url: function() { 437 | var base = getValue(this, 'urlRoot') || getValue(this.collection, 'url') || urlError(); 438 | if (this.isNew()) return base; 439 | return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id); 440 | }, 441 | 442 | // **parse** converts a response into the hash of attributes to be `set` on 443 | // the model. The default implementation is just to pass the response along. 444 | parse: function(resp, xhr) { 445 | return resp; 446 | }, 447 | 448 | // Create a new model with identical attributes to this one. 449 | clone: function() { 450 | return new this.constructor(this.attributes); 451 | }, 452 | 453 | // A model is new if it has never been saved to the server, and lacks an id. 454 | isNew: function() { 455 | return this.id == null; 456 | }, 457 | 458 | // Call this method to manually fire a `"change"` event for this model and 459 | // a `"change:attribute"` event for each changed attribute. 460 | // Calling this will cause all objects observing the model to update. 461 | change: function(options) { 462 | options || (options = {}); 463 | var changing = this._changing; 464 | this._changing = true; 465 | 466 | // Silent changes become pending changes. 467 | for (var attr in this._silent) this._pending[attr] = true; 468 | 469 | // Silent changes are triggered. 470 | var changes = _.extend({}, options.changes, this._silent); 471 | this._silent = {}; 472 | for (var attr in changes) { 473 | this.trigger('change:' + attr, this, this.get(attr), options); 474 | } 475 | if (changing) return this; 476 | 477 | // Continue firing `"change"` events while there are pending changes. 478 | while (!_.isEmpty(this._pending)) { 479 | this._pending = {}; 480 | this.trigger('change', this, options); 481 | // Pending and silent changes still remain. 482 | for (var attr in this.changed) { 483 | if (this._pending[attr] || this._silent[attr]) continue; 484 | delete this.changed[attr]; 485 | } 486 | this._previousAttributes = _.clone(this.attributes); 487 | } 488 | 489 | this._changing = false; 490 | return this; 491 | }, 492 | 493 | // Determine if the model has changed since the last `"change"` event. 494 | // If you specify an attribute name, determine if that attribute has changed. 495 | hasChanged: function(attr) { 496 | if (!arguments.length) return !_.isEmpty(this.changed); 497 | return _.has(this.changed, attr); 498 | }, 499 | 500 | // Return an object containing all the attributes that have changed, or 501 | // false if there are no changed attributes. Useful for determining what 502 | // parts of a view need to be updated and/or what attributes need to be 503 | // persisted to the server. Unset attributes will be set to undefined. 504 | // You can also pass an attributes object to diff against the model, 505 | // determining if there *would be* a change. 506 | changedAttributes: function(diff) { 507 | if (!diff) return this.hasChanged() ? _.clone(this.changed) : false; 508 | var val, changed = false, old = this._previousAttributes; 509 | for (var attr in diff) { 510 | if (_.isEqual(old[attr], (val = diff[attr]))) continue; 511 | (changed || (changed = {}))[attr] = val; 512 | } 513 | return changed; 514 | }, 515 | 516 | // Get the previous value of an attribute, recorded at the time the last 517 | // `"change"` event was fired. 518 | previous: function(attr) { 519 | if (!arguments.length || !this._previousAttributes) return null; 520 | return this._previousAttributes[attr]; 521 | }, 522 | 523 | // Get all of the attributes of the model at the time of the previous 524 | // `"change"` event. 525 | previousAttributes: function() { 526 | return _.clone(this._previousAttributes); 527 | }, 528 | 529 | // Check if the model is currently in a valid state. It's only possible to 530 | // get into an *invalid* state if you're using silent changes. 531 | isValid: function() { 532 | return !this.validate(this.attributes); 533 | }, 534 | 535 | // Run validation against the next complete set of model attributes, 536 | // returning `true` if all is well. If a specific `error` callback has 537 | // been passed, call that instead of firing the general `"error"` event. 538 | _validate: function(attrs, options) { 539 | if (options.silent || !this.validate) return true; 540 | attrs = _.extend({}, this.attributes, attrs); 541 | var error = this.validate(attrs, options); 542 | if (!error) return true; 543 | if (options && options.error) { 544 | options.error(this, error, options); 545 | } else { 546 | this.trigger('error', this, error, options); 547 | } 548 | return false; 549 | } 550 | 551 | }); 552 | 553 | // Backbone.Collection 554 | // ------------------- 555 | 556 | // Provides a standard collection class for our sets of models, ordered 557 | // or unordered. If a `comparator` is specified, the Collection will maintain 558 | // its models in sort order, as they're added and removed. 559 | var Collection = Backbone.Collection = function(models, options) { 560 | options || (options = {}); 561 | if (options.model) this.model = options.model; 562 | if (options.comparator) this.comparator = options.comparator; 563 | this._reset(); 564 | this.initialize.apply(this, arguments); 565 | if (models) this.reset(models, {silent: true, parse: options.parse}); 566 | }; 567 | 568 | // Define the Collection's inheritable methods. 569 | _.extend(Collection.prototype, Events, { 570 | 571 | // The default model for a collection is just a **Backbone.Model**. 572 | // This should be overridden in most cases. 573 | model: Model, 574 | 575 | // Initialize is an empty function by default. Override it with your own 576 | // initialization logic. 577 | initialize: function(){}, 578 | 579 | // The JSON representation of a Collection is an array of the 580 | // models' attributes. 581 | toJSON: function(options) { 582 | return this.map(function(model){ return model.toJSON(options); }); 583 | }, 584 | 585 | // Add a model, or list of models to the set. Pass **silent** to avoid 586 | // firing the `add` event for every new model. 587 | add: function(models, options) { 588 | var i, index, length, model, cid, id, cids = {}, ids = {}, dups = []; 589 | options || (options = {}); 590 | models = _.isArray(models) ? models.slice() : [models]; 591 | 592 | // Begin by turning bare objects into model references, and preventing 593 | // invalid models or duplicate models from being added. 594 | for (i = 0, length = models.length; i < length; i++) { 595 | if (!(model = models[i] = this._prepareModel(models[i], options))) { 596 | throw new Error("Can't add an invalid model to a collection"); 597 | } 598 | cid = model.cid; 599 | id = model.id; 600 | if (cids[cid] || this._byCid[cid] || ((id != null) && (ids[id] || this._byId[id]))) { 601 | dups.push(i); 602 | continue; 603 | } 604 | cids[cid] = ids[id] = model; 605 | } 606 | 607 | // Remove duplicates. 608 | i = dups.length; 609 | while (i--) { 610 | models.splice(dups[i], 1); 611 | } 612 | 613 | // Listen to added models' events, and index models for lookup by 614 | // `id` and by `cid`. 615 | for (i = 0, length = models.length; i < length; i++) { 616 | (model = models[i]).on('all', this._onModelEvent, this); 617 | this._byCid[model.cid] = model; 618 | if (model.id != null) this._byId[model.id] = model; 619 | } 620 | 621 | // Insert models into the collection, re-sorting if needed, and triggering 622 | // `add` events unless silenced. 623 | this.length += length; 624 | index = options.at != null ? options.at : this.models.length; 625 | splice.apply(this.models, [index, 0].concat(models)); 626 | if (this.comparator) this.sort({silent: true}); 627 | if (options.silent) return this; 628 | for (i = 0, length = this.models.length; i < length; i++) { 629 | if (!cids[(model = this.models[i]).cid]) continue; 630 | options.index = i; 631 | model.trigger('add', model, this, options); 632 | } 633 | return this; 634 | }, 635 | 636 | // Remove a model, or a list of models from the set. Pass silent to avoid 637 | // firing the `remove` event for every model removed. 638 | remove: function(models, options) { 639 | var i, l, index, model; 640 | options || (options = {}); 641 | models = _.isArray(models) ? models.slice() : [models]; 642 | for (i = 0, l = models.length; i < l; i++) { 643 | model = this.getByCid(models[i]) || this.get(models[i]); 644 | if (!model) continue; 645 | delete this._byId[model.id]; 646 | delete this._byCid[model.cid]; 647 | index = this.indexOf(model); 648 | this.models.splice(index, 1); 649 | this.length--; 650 | if (!options.silent) { 651 | options.index = index; 652 | model.trigger('remove', model, this, options); 653 | } 654 | this._removeReference(model); 655 | } 656 | return this; 657 | }, 658 | 659 | // Add a model to the end of the collection. 660 | push: function(model, options) { 661 | model = this._prepareModel(model, options); 662 | this.add(model, options); 663 | return model; 664 | }, 665 | 666 | // Remove a model from the end of the collection. 667 | pop: function(options) { 668 | var model = this.at(this.length - 1); 669 | this.remove(model, options); 670 | return model; 671 | }, 672 | 673 | // Add a model to the beginning of the collection. 674 | unshift: function(model, options) { 675 | model = this._prepareModel(model, options); 676 | this.add(model, _.extend({at: 0}, options)); 677 | return model; 678 | }, 679 | 680 | // Remove a model from the beginning of the collection. 681 | shift: function(options) { 682 | var model = this.at(0); 683 | this.remove(model, options); 684 | return model; 685 | }, 686 | 687 | // Get a model from the set by id. 688 | get: function(id) { 689 | if (id == null) return void 0; 690 | return this._byId[id.id != null ? id.id : id]; 691 | }, 692 | 693 | // Get a model from the set by client id. 694 | getByCid: function(cid) { 695 | return cid && this._byCid[cid.cid || cid]; 696 | }, 697 | 698 | // Get the model at the given index. 699 | at: function(index) { 700 | return this.models[index]; 701 | }, 702 | 703 | // Return models with matching attributes. Useful for simple cases of `filter`. 704 | where: function(attrs) { 705 | if (_.isEmpty(attrs)) return []; 706 | return this.filter(function(model) { 707 | for (var key in attrs) { 708 | if (attrs[key] !== model.get(key)) return false; 709 | } 710 | return true; 711 | }); 712 | }, 713 | 714 | // Force the collection to re-sort itself. You don't need to call this under 715 | // normal circumstances, as the set will maintain sort order as each item 716 | // is added. 717 | sort: function(options) { 718 | options || (options = {}); 719 | if (!this.comparator) throw new Error('Cannot sort a set without a comparator'); 720 | var boundComparator = _.bind(this.comparator, this); 721 | if (this.comparator.length == 1) { 722 | this.models = this.sortBy(boundComparator); 723 | } else { 724 | this.models.sort(boundComparator); 725 | } 726 | if (!options.silent) this.trigger('reset', this, options); 727 | return this; 728 | }, 729 | 730 | // Pluck an attribute from each model in the collection. 731 | pluck: function(attr) { 732 | return _.map(this.models, function(model){ return model.get(attr); }); 733 | }, 734 | 735 | // When you have more items than you want to add or remove individually, 736 | // you can reset the entire set with a new list of models, without firing 737 | // any `add` or `remove` events. Fires `reset` when finished. 738 | reset: function(models, options) { 739 | models || (models = []); 740 | options || (options = {}); 741 | for (var i = 0, l = this.models.length; i < l; i++) { 742 | this._removeReference(this.models[i]); 743 | } 744 | this._reset(); 745 | this.add(models, _.extend({silent: true}, options)); 746 | if (!options.silent) this.trigger('reset', this, options); 747 | return this; 748 | }, 749 | 750 | // Fetch the default set of models for this collection, resetting the 751 | // collection when they arrive. If `add: true` is passed, appends the 752 | // models to the collection instead of resetting. 753 | fetch: function(options) { 754 | options = options ? _.clone(options) : {}; 755 | if (options.parse === undefined) options.parse = true; 756 | var collection = this; 757 | var success = options.success; 758 | options.success = function(resp, status, xhr) { 759 | collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options); 760 | if (success) success(collection, resp); 761 | }; 762 | options.error = Backbone.wrapError(options.error, collection, options); 763 | return (this.sync || Backbone.sync).call(this, 'read', this, options); 764 | }, 765 | 766 | // Create a new instance of a model in this collection. Add the model to the 767 | // collection immediately, unless `wait: true` is passed, in which case we 768 | // wait for the server to agree. 769 | create: function(model, options) { 770 | var coll = this; 771 | options = options ? _.clone(options) : {}; 772 | model = this._prepareModel(model, options); 773 | if (!model) return false; 774 | if (!options.wait) coll.add(model, options); 775 | var success = options.success; 776 | options.success = function(nextModel, resp, xhr) { 777 | if (options.wait) coll.add(nextModel, options); 778 | if (success) { 779 | success(nextModel, resp); 780 | } else { 781 | nextModel.trigger('sync', model, resp, options); 782 | } 783 | }; 784 | model.save(null, options); 785 | return model; 786 | }, 787 | 788 | // **parse** converts a response into a list of models to be added to the 789 | // collection. The default implementation is just to pass it through. 790 | parse: function(resp, xhr) { 791 | return resp; 792 | }, 793 | 794 | // Proxy to _'s chain. Can't be proxied the same way the rest of the 795 | // underscore methods are proxied because it relies on the underscore 796 | // constructor. 797 | chain: function () { 798 | return _(this.models).chain(); 799 | }, 800 | 801 | // Reset all internal state. Called when the collection is reset. 802 | _reset: function(options) { 803 | this.length = 0; 804 | this.models = []; 805 | this._byId = {}; 806 | this._byCid = {}; 807 | }, 808 | 809 | // Prepare a model or hash of attributes to be added to this collection. 810 | _prepareModel: function(model, options) { 811 | options || (options = {}); 812 | if (!(model instanceof Model)) { 813 | var attrs = model; 814 | options.collection = this; 815 | model = new this.model(attrs, options); 816 | if (!model._validate(model.attributes, options)) model = false; 817 | } else if (!model.collection) { 818 | model.collection = this; 819 | } 820 | return model; 821 | }, 822 | 823 | // Internal method to remove a model's ties to a collection. 824 | _removeReference: function(model) { 825 | if (this == model.collection) { 826 | delete model.collection; 827 | } 828 | model.off('all', this._onModelEvent, this); 829 | }, 830 | 831 | // Internal method called every time a model in the set fires an event. 832 | // Sets need to update their indexes when models change ids. All other 833 | // events simply proxy through. "add" and "remove" events that originate 834 | // in other collections are ignored. 835 | _onModelEvent: function(event, model, collection, options) { 836 | if ((event == 'add' || event == 'remove') && collection != this) return; 837 | if (event == 'destroy') { 838 | this.remove(model, options); 839 | } 840 | if (model && event === 'change:' + model.idAttribute) { 841 | delete this._byId[model.previous(model.idAttribute)]; 842 | this._byId[model.id] = model; 843 | } 844 | this.trigger.apply(this, arguments); 845 | } 846 | 847 | }); 848 | 849 | // Underscore methods that we want to implement on the Collection. 850 | var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 851 | 'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 852 | 'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 853 | 'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf', 854 | 'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy']; 855 | 856 | // Mix in each Underscore method as a proxy to `Collection#models`. 857 | _.each(methods, function(method) { 858 | Collection.prototype[method] = function() { 859 | return _[method].apply(_, [this.models].concat(_.toArray(arguments))); 860 | }; 861 | }); 862 | 863 | // Backbone.Router 864 | // ------------------- 865 | 866 | // Routers map faux-URLs to actions, and fire events when routes are 867 | // matched. Creating a new one sets its `routes` hash, if not set statically. 868 | var Router = Backbone.Router = function(options) { 869 | options || (options = {}); 870 | if (options.routes) this.routes = options.routes; 871 | this._bindRoutes(); 872 | this.initialize.apply(this, arguments); 873 | }; 874 | 875 | // Cached regular expressions for matching named param parts and splatted 876 | // parts of route strings. 877 | var namedParam = /:\w+/g; 878 | var splatParam = /\*\w+/g; 879 | var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g; 880 | 881 | // Set up all inheritable **Backbone.Router** properties and methods. 882 | _.extend(Router.prototype, Events, { 883 | 884 | // Initialize is an empty function by default. Override it with your own 885 | // initialization logic. 886 | initialize: function(){}, 887 | 888 | // Manually bind a single named route to a callback. For example: 889 | // 890 | // this.route('search/:query/p:num', 'search', function(query, num) { 891 | // ... 892 | // }); 893 | // 894 | route: function(route, name, callback) { 895 | Backbone.history || (Backbone.history = new History); 896 | if (!_.isRegExp(route)) route = this._routeToRegExp(route); 897 | if (!callback) callback = this[name]; 898 | Backbone.history.route(route, _.bind(function(fragment) { 899 | var args = this._extractParameters(route, fragment); 900 | callback && callback.apply(this, args); 901 | this.trigger.apply(this, ['route:' + name].concat(args)); 902 | Backbone.history.trigger('route', this, name, args); 903 | }, this)); 904 | return this; 905 | }, 906 | 907 | // Simple proxy to `Backbone.history` to save a fragment into the history. 908 | navigate: function(fragment, options) { 909 | Backbone.history.navigate(fragment, options); 910 | }, 911 | 912 | // Bind all defined routes to `Backbone.history`. We have to reverse the 913 | // order of the routes here to support behavior where the most general 914 | // routes can be defined at the bottom of the route map. 915 | _bindRoutes: function() { 916 | if (!this.routes) return; 917 | var routes = []; 918 | for (var route in this.routes) { 919 | routes.unshift([route, this.routes[route]]); 920 | } 921 | for (var i = 0, l = routes.length; i < l; i++) { 922 | this.route(routes[i][0], routes[i][1], this[routes[i][1]]); 923 | } 924 | }, 925 | 926 | // Convert a route string into a regular expression, suitable for matching 927 | // against the current location hash. 928 | _routeToRegExp: function(route) { 929 | route = route.replace(escapeRegExp, '\\$&') 930 | .replace(namedParam, '([^\/]+)') 931 | .replace(splatParam, '(.*?)'); 932 | return new RegExp('^' + route + '$'); 933 | }, 934 | 935 | // Given a route, and a URL fragment that it matches, return the array of 936 | // extracted parameters. 937 | _extractParameters: function(route, fragment) { 938 | return route.exec(fragment).slice(1); 939 | } 940 | 941 | }); 942 | 943 | // Backbone.History 944 | // ---------------- 945 | 946 | // Handles cross-browser history management, based on URL fragments. If the 947 | // browser does not support `onhashchange`, falls back to polling. 948 | var History = Backbone.History = function() { 949 | this.handlers = []; 950 | _.bindAll(this, 'checkUrl'); 951 | }; 952 | 953 | // Cached regex for cleaning leading hashes and slashes . 954 | var routeStripper = /^[#\/]/; 955 | 956 | // Cached regex for detecting MSIE. 957 | var isExplorer = /msie [\w.]+/; 958 | 959 | // Has the history handling already been started? 960 | History.started = false; 961 | 962 | // Set up all inheritable **Backbone.History** properties and methods. 963 | _.extend(History.prototype, Events, { 964 | 965 | // The default interval to poll for hash changes, if necessary, is 966 | // twenty times a second. 967 | interval: 50, 968 | 969 | // Gets the true hash value. Cannot use location.hash directly due to bug 970 | // in Firefox where location.hash will always be decoded. 971 | getHash: function(windowOverride) { 972 | var loc = windowOverride ? windowOverride.location : window.location; 973 | var match = loc.href.match(/#(.*)$/); 974 | return match ? match[1] : ''; 975 | }, 976 | 977 | // Get the cross-browser normalized URL fragment, either from the URL, 978 | // the hash, or the override. 979 | getFragment: function(fragment, forcePushState) { 980 | if (fragment == null) { 981 | if (this._hasPushState || forcePushState) { 982 | fragment = window.location.pathname; 983 | var search = window.location.search; 984 | if (search) fragment += search; 985 | } else { 986 | fragment = this.getHash(); 987 | } 988 | } 989 | if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length); 990 | return fragment.replace(routeStripper, ''); 991 | }, 992 | 993 | // Start the hash change handling, returning `true` if the current URL matches 994 | // an existing route, and `false` otherwise. 995 | start: function(options) { 996 | if (History.started) throw new Error("Backbone.history has already been started"); 997 | History.started = true; 998 | 999 | // Figure out the initial configuration. Do we need an iframe? 1000 | // Is pushState desired ... is it available? 1001 | this.options = _.extend({}, {root: '/'}, this.options, options); 1002 | this._wantsHashChange = this.options.hashChange !== false; 1003 | this._wantsPushState = !!this.options.pushState; 1004 | this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState); 1005 | var fragment = this.getFragment(); 1006 | var docMode = document.documentMode; 1007 | var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7)); 1008 | 1009 | if (oldIE) { 1010 | this.iframe = $('