diff options
author | talasila <talasila@research.att.com> | 2017-02-07 15:03:57 -0500 |
---|---|---|
committer | talasila <talasila@research.att.com> | 2017-02-07 15:05:15 -0500 |
commit | 4ad39a5c96dd99acf819ce189b13fec946d7506b (patch) | |
tree | a1449286441947cc3d07a45227fa0d6f978e1a7d /ecomp-portal-FE/client/bower_components/lodash/vendor/backbone | |
parent | 5500448cbd1f374d0ac743ee2fd636fe2d3c0027 (diff) |
Initial OpenECOMP Portal commit
Change-Id: I804b80e0830c092e307da1599bd9fbb5c3e2da77
Signed-off-by: talasila <talasila@research.att.com>
Diffstat (limited to 'ecomp-portal-FE/client/bower_components/lodash/vendor/backbone')
11 files changed, 7922 insertions, 0 deletions
diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/LICENSE b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/LICENSE new file mode 100644 index 00000000..02c89b26 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2010-2016 Jeremy Ashkenas, DocumentCloud + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/backbone.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/backbone.js new file mode 100644 index 00000000..55ccb22b --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/backbone.js @@ -0,0 +1,1920 @@ +// Backbone.js 1.3.3 + +// (c) 2010-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors +// Backbone may be freely distributed under the MIT license. +// For all details and documentation: +// http://backbonejs.org + +(function(factory) { + + // Establish the root object, `window` (`self`) in the browser, or `global` on the server. + // We use `self` instead of `window` for `WebWorker` support. + var root = (typeof self == 'object' && self.self === self && self) || + (typeof global == 'object' && global.global === global && global); + + // Set up Backbone appropriately for the environment. Start with AMD. + if (typeof define === 'function' && define.amd) { + define(['underscore', 'jquery', 'exports'], function(_, $, exports) { + // Export global even in AMD case in case this script is loaded with + // others that may still expect a global Backbone. + root.Backbone = factory(root, exports, _, $); + }); + + // Next for Node.js or CommonJS. jQuery may not be needed as a module. + } else if (typeof exports !== 'undefined') { + var _ = require('underscore'), $; + try { $ = require('jquery'); } catch (e) {} + factory(root, exports, _, $); + + // Finally, as a browser global. + } else { + root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$)); + } + +})(function(root, Backbone, _, $) { + + // Initial Setup + // ------------- + + // Save the previous value of the `Backbone` variable, so that it can be + // restored later on, if `noConflict` is used. + var previousBackbone = root.Backbone; + + // Create a local reference to a common array method we'll want to use later. + var slice = Array.prototype.slice; + + // Current version of the library. Keep in sync with `package.json`. + Backbone.VERSION = '1.3.3'; + + // For Backbone's purposes, jQuery, Zepto, Ender, or My Library (kidding) owns + // the `$` variable. + Backbone.$ = $; + + // Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable + // to its previous owner. Returns a reference to this Backbone object. + Backbone.noConflict = function() { + root.Backbone = previousBackbone; + return this; + }; + + // Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option + // will fake `"PATCH"`, `"PUT"` and `"DELETE"` requests via the `_method` parameter and + // set a `X-Http-Method-Override` header. + Backbone.emulateHTTP = false; + + // Turn on `emulateJSON` to support legacy servers that can't deal with direct + // `application/json` requests ... this will encode the body as + // `application/x-www-form-urlencoded` instead and will send the model in a + // form param named `model`. + Backbone.emulateJSON = false; + + // Proxy Backbone class methods to Underscore functions, wrapping the model's + // `attributes` object or collection's `models` array behind the scenes. + // + // collection.filter(function(model) { return model.get('age') > 10 }); + // collection.each(this.addView); + // + // `Function#apply` can be slow so we use the method's arg count, if we know it. + var addMethod = function(length, method, attribute) { + switch (length) { + case 1: return function() { + return _[method](this[attribute]); + }; + case 2: return function(value) { + return _[method](this[attribute], value); + }; + case 3: return function(iteratee, context) { + return _[method](this[attribute], cb(iteratee, this), context); + }; + case 4: return function(iteratee, defaultVal, context) { + return _[method](this[attribute], cb(iteratee, this), defaultVal, context); + }; + default: return function() { + var args = slice.call(arguments); + args.unshift(this[attribute]); + return _[method].apply(_, args); + }; + } + }; + var addUnderscoreMethods = function(Class, methods, attribute) { + _.each(methods, function(length, method) { + if (_[method]) Class.prototype[method] = addMethod(length, method, attribute); + }); + }; + + // Support `collection.sortBy('attr')` and `collection.findWhere({id: 1})`. + var cb = function(iteratee, instance) { + if (_.isFunction(iteratee)) return iteratee; + if (_.isObject(iteratee) && !instance._isModel(iteratee)) return modelMatcher(iteratee); + if (_.isString(iteratee)) return function(model) { return model.get(iteratee); }; + return iteratee; + }; + var modelMatcher = function(attrs) { + var matcher = _.matches(attrs); + return function(model) { + return matcher(model.attributes); + }; + }; + + // Backbone.Events + // --------------- + + // A module that can be mixed in to *any object* in order to provide it with + // a custom event channel. You may bind a callback to an event with `on` or + // remove with `off`; `trigger`-ing an event fires all callbacks in + // succession. + // + // var object = {}; + // _.extend(object, Backbone.Events); + // object.on('expand', function(){ alert('expanded'); }); + // object.trigger('expand'); + // + var Events = Backbone.Events = {}; + + // Regular expression used to split event strings. + var eventSplitter = /\s+/; + + // Iterates over the standard `event, callback` (as well as the fancy multiple + // space-separated events `"change blur", callback` and jQuery-style event + // maps `{event: callback}`). + var eventsApi = function(iteratee, events, name, callback, opts) { + var i = 0, names; + if (name && typeof name === 'object') { + // Handle event maps. + if (callback !== void 0 && 'context' in opts && opts.context === void 0) opts.context = callback; + for (names = _.keys(name); i < names.length ; i++) { + events = eventsApi(iteratee, events, names[i], name[names[i]], opts); + } + } else if (name && eventSplitter.test(name)) { + // Handle space-separated event names by delegating them individually. + for (names = name.split(eventSplitter); i < names.length; i++) { + events = iteratee(events, names[i], callback, opts); + } + } else { + // Finally, standard events. + events = iteratee(events, name, callback, opts); + } + return events; + }; + + // Bind an event to a `callback` function. Passing `"all"` will bind + // the callback to all events fired. + Events.on = function(name, callback, context) { + return internalOn(this, name, callback, context); + }; + + // Guard the `listening` argument from the public API. + var internalOn = function(obj, name, callback, context, listening) { + obj._events = eventsApi(onApi, obj._events || {}, name, callback, { + context: context, + ctx: obj, + listening: listening + }); + + if (listening) { + var listeners = obj._listeners || (obj._listeners = {}); + listeners[listening.id] = listening; + } + + return obj; + }; + + // Inversion-of-control versions of `on`. Tell *this* object to listen to + // an event in another object... keeping track of what it's listening to + // for easier unbinding later. + Events.listenTo = function(obj, name, callback) { + if (!obj) return this; + var id = obj._listenId || (obj._listenId = _.uniqueId('l')); + var listeningTo = this._listeningTo || (this._listeningTo = {}); + var listening = listeningTo[id]; + + // This object is not listening to any other events on `obj` yet. + // Setup the necessary references to track the listening callbacks. + if (!listening) { + var thisId = this._listenId || (this._listenId = _.uniqueId('l')); + listening = listeningTo[id] = {obj: obj, objId: id, id: thisId, listeningTo: listeningTo, count: 0}; + } + + // Bind callbacks on obj, and keep track of them on listening. + internalOn(obj, name, callback, this, listening); + return this; + }; + + // The reducing API that adds a callback to the `events` object. + var onApi = function(events, name, callback, options) { + if (callback) { + var handlers = events[name] || (events[name] = []); + var context = options.context, ctx = options.ctx, listening = options.listening; + if (listening) listening.count++; + + handlers.push({callback: callback, context: context, ctx: context || ctx, listening: listening}); + } + return events; + }; + + // Remove one or many callbacks. If `context` is null, removes all + // callbacks with that function. If `callback` is null, removes all + // callbacks for the event. If `name` is null, removes all bound + // callbacks for all events. + Events.off = function(name, callback, context) { + if (!this._events) return this; + this._events = eventsApi(offApi, this._events, name, callback, { + context: context, + listeners: this._listeners + }); + return this; + }; + + // Tell this object to stop listening to either specific events ... or + // to every object it's currently listening to. + Events.stopListening = function(obj, name, callback) { + var listeningTo = this._listeningTo; + if (!listeningTo) return this; + + var ids = obj ? [obj._listenId] : _.keys(listeningTo); + + for (var i = 0; i < ids.length; i++) { + var listening = listeningTo[ids[i]]; + + // If listening doesn't exist, this object is not currently + // listening to obj. Break out early. + if (!listening) break; + + listening.obj.off(name, callback, this); + } + + return this; + }; + + // The reducing API that removes a callback from the `events` object. + var offApi = function(events, name, callback, options) { + if (!events) return; + + var i = 0, listening; + var context = options.context, listeners = options.listeners; + + // Delete all events listeners and "drop" events. + if (!name && !callback && !context) { + var ids = _.keys(listeners); + for (; i < ids.length; i++) { + listening = listeners[ids[i]]; + delete listeners[listening.id]; + delete listening.listeningTo[listening.objId]; + } + return; + } + + var names = name ? [name] : _.keys(events); + for (; i < names.length; i++) { + name = names[i]; + var handlers = events[name]; + + // Bail out if there are no events stored. + if (!handlers) break; + + // Replace events if there are any remaining. Otherwise, clean up. + var remaining = []; + for (var j = 0; j < handlers.length; j++) { + var handler = handlers[j]; + if ( + callback && callback !== handler.callback && + callback !== handler.callback._callback || + context && context !== handler.context + ) { + remaining.push(handler); + } else { + listening = handler.listening; + if (listening && --listening.count === 0) { + delete listeners[listening.id]; + delete listening.listeningTo[listening.objId]; + } + } + } + + // Update tail event if the list has any events. Otherwise, clean up. + if (remaining.length) { + events[name] = remaining; + } else { + delete events[name]; + } + } + return events; + }; + + // Bind an event to only be triggered a single time. After the first time + // the callback is invoked, its listener will be removed. If multiple events + // are passed in using the space-separated syntax, the handler will fire + // once for each event, not once for a combination of all events. + Events.once = function(name, callback, context) { + // Map the event into a `{event: once}` object. + var events = eventsApi(onceMap, {}, name, callback, _.bind(this.off, this)); + if (typeof name === 'string' && context == null) callback = void 0; + return this.on(events, callback, context); + }; + + // Inversion-of-control versions of `once`. + Events.listenToOnce = function(obj, name, callback) { + // Map the event into a `{event: once}` object. + var events = eventsApi(onceMap, {}, name, callback, _.bind(this.stopListening, this, obj)); + return this.listenTo(obj, events); + }; + + // Reduces the event callbacks into a map of `{event: onceWrapper}`. + // `offer` unbinds the `onceWrapper` after it has been called. + var onceMap = function(map, name, callback, offer) { + if (callback) { + var once = map[name] = _.once(function() { + offer(name, once); + callback.apply(this, arguments); + }); + once._callback = callback; + } + return map; + }; + + // Trigger one or many events, firing all bound callbacks. Callbacks are + // passed the same arguments as `trigger` is, apart from the event name + // (unless you're listening on `"all"`, which will cause your callback to + // receive the true name of the event as the first argument). + Events.trigger = function(name) { + if (!this._events) return this; + + var length = Math.max(0, arguments.length - 1); + var args = Array(length); + for (var i = 0; i < length; i++) args[i] = arguments[i + 1]; + + eventsApi(triggerApi, this._events, name, void 0, args); + return this; + }; + + // Handles triggering the appropriate event callbacks. + var triggerApi = function(objEvents, name, callback, args) { + if (objEvents) { + var events = objEvents[name]; + var allEvents = objEvents.all; + if (events && allEvents) allEvents = allEvents.slice(); + if (events) triggerEvents(events, args); + if (allEvents) triggerEvents(allEvents, [name].concat(args)); + } + return objEvents; + }; + + // A difficult-to-believe, but optimized internal dispatch function for + // triggering events. Tries to keep the usual cases speedy (most internal + // Backbone events have 3 arguments). + var triggerEvents = function(events, args) { + var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2]; + switch (args.length) { + case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return; + case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return; + case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return; + case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return; + default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); return; + } + }; + + // Aliases for backwards compatibility. + Events.bind = Events.on; + Events.unbind = Events.off; + + // Allow the `Backbone` object to serve as a global event bus, for folks who + // want global "pubsub" in a convenient place. + _.extend(Backbone, Events); + + // Backbone.Model + // -------------- + + // Backbone **Models** are the basic data object in the framework -- + // frequently representing a row in a table in a database on your server. + // A discrete chunk of data and a bunch of useful, related methods for + // performing computations and transformations on that data. + + // Create a new model with the specified attributes. A client id (`cid`) + // is automatically generated and assigned for you. + var Model = Backbone.Model = function(attributes, options) { + var attrs = attributes || {}; + options || (options = {}); + this.cid = _.uniqueId(this.cidPrefix); + this.attributes = {}; + if (options.collection) this.collection = options.collection; + if (options.parse) attrs = this.parse(attrs, options) || {}; + var defaults = _.result(this, 'defaults'); + attrs = _.defaults(_.extend({}, defaults, attrs), defaults); + this.set(attrs, options); + this.changed = {}; + this.initialize.apply(this, arguments); + }; + + // Attach all inheritable methods to the Model prototype. + _.extend(Model.prototype, Events, { + + // A hash of attributes whose current and previous value differ. + changed: null, + + // The value returned during the last failed validation. + validationError: null, + + // The default name for the JSON `id` attribute is `"id"`. MongoDB and + // CouchDB users may want to set this to `"_id"`. + idAttribute: 'id', + + // The prefix is used to create the client id which is used to identify models locally. + // You may want to override this if you're experiencing name clashes with model ids. + cidPrefix: 'c', + + // Initialize is an empty function by default. Override it with your own + // initialization logic. + initialize: function(){}, + + // Return a copy of the model's `attributes` object. + toJSON: function(options) { + return _.clone(this.attributes); + }, + + // Proxy `Backbone.sync` by default -- but override this if you need + // custom syncing semantics for *this* particular model. + sync: function() { + return Backbone.sync.apply(this, arguments); + }, + + // Get the value of an attribute. + get: function(attr) { + return this.attributes[attr]; + }, + + // Get the HTML-escaped value of an attribute. + escape: function(attr) { + return _.escape(this.get(attr)); + }, + + // Returns `true` if the attribute contains a value that is not null + // or undefined. + has: function(attr) { + return this.get(attr) != null; + }, + + // Special-cased proxy to underscore's `_.matches` method. + matches: function(attrs) { + return !!_.iteratee(attrs, this)(this.attributes); + }, + + // Set a hash of model attributes on the object, firing `"change"`. This is + // the core primitive operation of a model, updating the data and notifying + // anyone who needs to know about the change in state. The heart of the beast. + set: function(key, val, options) { + if (key == null) return this; + + // Handle both `"key", value` and `{key: value}` -style arguments. + var attrs; + if (typeof key === 'object') { + attrs = key; + options = val; + } else { + (attrs = {})[key] = val; + } + + options || (options = {}); + + // Run validation. + if (!this._validate(attrs, options)) return false; + + // Extract attributes and options. + var unset = options.unset; + var silent = options.silent; + var changes = []; + var changing = this._changing; + this._changing = true; + + if (!changing) { + this._previousAttributes = _.clone(this.attributes); + this.changed = {}; + } + + var current = this.attributes; + var changed = this.changed; + var prev = this._previousAttributes; + + // For each `set` attribute, update or delete the current value. + for (var attr in attrs) { + val = attrs[attr]; + if (!_.isEqual(current[attr], val)) changes.push(attr); + if (!_.isEqual(prev[attr], val)) { + changed[attr] = val; + } else { + delete changed[attr]; + } + unset ? delete current[attr] : current[attr] = val; + } + + // Update the `id`. + if (this.idAttribute in attrs) this.id = this.get(this.idAttribute); + + // Trigger all relevant attribute changes. + if (!silent) { + if (changes.length) this._pending = options; + for (var i = 0; i < changes.length; i++) { + this.trigger('change:' + changes[i], this, current[changes[i]], options); + } + } + + // You might be wondering why there's a `while` loop here. Changes can + // be recursively nested within `"change"` events. + if (changing) return this; + if (!silent) { + while (this._pending) { + options = this._pending; + this._pending = false; + this.trigger('change', this, options); + } + } + this._pending = false; + this._changing = false; + return this; + }, + + // Remove an attribute from the model, firing `"change"`. `unset` is a noop + // if the attribute doesn't exist. + unset: function(attr, options) { + return this.set(attr, void 0, _.extend({}, options, {unset: true})); + }, + + // Clear all attributes on the model, firing `"change"`. + clear: function(options) { + var attrs = {}; + for (var key in this.attributes) attrs[key] = void 0; + return this.set(attrs, _.extend({}, options, {unset: true})); + }, + + // Determine if the model has changed since the last `"change"` event. + // If you specify an attribute name, determine if that attribute has changed. + hasChanged: function(attr) { + if (attr == null) return !_.isEmpty(this.changed); + return _.has(this.changed, attr); + }, + + // Return an object containing all the attributes that have changed, or + // false if there are no changed attributes. Useful for determining what + // parts of a view need to be updated and/or what attributes need to be + // persisted to the server. Unset attributes will be set to undefined. + // You can also pass an attributes object to diff against the model, + // determining if there *would be* a change. + changedAttributes: function(diff) { + if (!diff) return this.hasChanged() ? _.clone(this.changed) : false; + var old = this._changing ? this._previousAttributes : this.attributes; + var changed = {}; + for (var attr in diff) { + var val = diff[attr]; + if (_.isEqual(old[attr], val)) continue; + changed[attr] = val; + } + return _.size(changed) ? changed : false; + }, + + // Get the previous value of an attribute, recorded at the time the last + // `"change"` event was fired. + previous: function(attr) { + if (attr == null || !this._previousAttributes) return null; + return this._previousAttributes[attr]; + }, + + // Get all of the attributes of the model at the time of the previous + // `"change"` event. + previousAttributes: function() { + return _.clone(this._previousAttributes); + }, + + // Fetch the model from the server, merging the response with the model's + // local attributes. Any changed attributes will trigger a "change" event. + fetch: function(options) { + options = _.extend({parse: true}, options); + var model = this; + var success = options.success; + options.success = function(resp) { + var serverAttrs = options.parse ? model.parse(resp, options) : resp; + if (!model.set(serverAttrs, options)) return false; + if (success) success.call(options.context, model, resp, options); + model.trigger('sync', model, resp, options); + }; + wrapError(this, options); + return this.sync('read', this, options); + }, + + // Set a hash of model attributes, and sync the model to the server. + // If the server returns an attributes hash that differs, the model's + // state will be `set` again. + save: function(key, val, options) { + // Handle both `"key", value` and `{key: value}` -style arguments. + var attrs; + if (key == null || typeof key === 'object') { + attrs = key; + options = val; + } else { + (attrs = {})[key] = val; + } + + options = _.extend({validate: true, parse: true}, options); + var wait = options.wait; + + // If we're not waiting and attributes exist, save acts as + // `set(attr).save(null, opts)` with validation. Otherwise, check if + // the model will be valid when the attributes, if any, are set. + if (attrs && !wait) { + if (!this.set(attrs, options)) return false; + } else if (!this._validate(attrs, options)) { + return false; + } + + // After a successful server-side save, the client is (optionally) + // updated with the server-side state. + var model = this; + var success = options.success; + var attributes = this.attributes; + options.success = function(resp) { + // Ensure attributes are restored during synchronous saves. + model.attributes = attributes; + var serverAttrs = options.parse ? model.parse(resp, options) : resp; + if (wait) serverAttrs = _.extend({}, attrs, serverAttrs); + if (serverAttrs && !model.set(serverAttrs, options)) return false; + if (success) success.call(options.context, model, resp, options); + model.trigger('sync', model, resp, options); + }; + wrapError(this, options); + + // Set temporary attributes if `{wait: true}` to properly find new ids. + if (attrs && wait) this.attributes = _.extend({}, attributes, attrs); + + var method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update'); + if (method === 'patch' && !options.attrs) options.attrs = attrs; + var xhr = this.sync(method, this, options); + + // Restore attributes. + this.attributes = attributes; + + return xhr; + }, + + // Destroy this model on the server if it was already persisted. + // Optimistically removes the model from its collection, if it has one. + // If `wait: true` is passed, waits for the server to respond before removal. + destroy: function(options) { + options = options ? _.clone(options) : {}; + var model = this; + var success = options.success; + var wait = options.wait; + + var destroy = function() { + model.stopListening(); + model.trigger('destroy', model, model.collection, options); + }; + + options.success = function(resp) { + if (wait) destroy(); + if (success) success.call(options.context, model, resp, options); + if (!model.isNew()) model.trigger('sync', model, resp, options); + }; + + var xhr = false; + if (this.isNew()) { + _.defer(options.success); + } else { + wrapError(this, options); + xhr = this.sync('delete', this, options); + } + if (!wait) destroy(); + return xhr; + }, + + // Default URL for the model's representation on the server -- if you're + // using Backbone's restful methods, override this to change the endpoint + // that will be called. + url: function() { + var base = + _.result(this, 'urlRoot') || + _.result(this.collection, 'url') || + urlError(); + if (this.isNew()) return base; + var id = this.get(this.idAttribute); + return base.replace(/[^\/]$/, '$&/') + encodeURIComponent(id); + }, + + // **parse** converts a response into the hash of attributes to be `set` on + // the model. The default implementation is just to pass the response along. + parse: function(resp, options) { + return resp; + }, + + // Create a new model with identical attributes to this one. + clone: function() { + return new this.constructor(this.attributes); + }, + + // A model is new if it has never been saved to the server, and lacks an id. + isNew: function() { + return !this.has(this.idAttribute); + }, + + // Check if the model is currently in a valid state. + isValid: function(options) { + return this._validate({}, _.extend({}, options, {validate: true})); + }, + + // Run validation against the next complete set of model attributes, + // returning `true` if all is well. Otherwise, fire an `"invalid"` event. + _validate: function(attrs, options) { + if (!options.validate || !this.validate) return true; + attrs = _.extend({}, this.attributes, attrs); + var error = this.validationError = this.validate(attrs, options) || null; + if (!error) return true; + this.trigger('invalid', this, error, _.extend(options, {validationError: error})); + return false; + } + + }); + + // Underscore methods that we want to implement on the Model, mapped to the + // number of arguments they take. + var modelMethods = {keys: 1, values: 1, pairs: 1, invert: 1, pick: 0, + omit: 0, chain: 1, isEmpty: 1}; + + // Mix in each Underscore method as a proxy to `Model#attributes`. + addUnderscoreMethods(Model, modelMethods, 'attributes'); + + // Backbone.Collection + // ------------------- + + // If models tend to represent a single row of data, a Backbone Collection is + // more analogous to a table full of data ... or a small slice or page of that + // table, or a collection of rows that belong together for a particular reason + // -- all of the messages in this particular folder, all of the documents + // belonging to this particular author, and so on. Collections maintain + // indexes of their models, both in order, and for lookup by `id`. + + // Create a new **Collection**, perhaps to contain a specific type of `model`. + // If a `comparator` is specified, the Collection will maintain + // its models in sort order, as they're added and removed. + var Collection = Backbone.Collection = function(models, options) { + options || (options = {}); + if (options.model) this.model = options.model; + if (options.comparator !== void 0) this.comparator = options.comparator; + this._reset(); + this.initialize.apply(this, arguments); + if (models) this.reset(models, _.extend({silent: true}, options)); + }; + + // Default options for `Collection#set`. + var setOptions = {add: true, remove: true, merge: true}; + var addOptions = {add: true, remove: false}; + + // Splices `insert` into `array` at index `at`. + var splice = function(array, insert, at) { + at = Math.min(Math.max(at, 0), array.length); + var tail = Array(array.length - at); + var length = insert.length; + var i; + for (i = 0; i < tail.length; i++) tail[i] = array[i + at]; + for (i = 0; i < length; i++) array[i + at] = insert[i]; + for (i = 0; i < tail.length; i++) array[i + length + at] = tail[i]; + }; + + // Define the Collection's inheritable methods. + _.extend(Collection.prototype, Events, { + + // The default model for a collection is just a **Backbone.Model**. + // This should be overridden in most cases. + model: Model, + + // Initialize is an empty function by default. Override it with your own + // initialization logic. + initialize: function(){}, + + // The JSON representation of a Collection is an array of the + // models' attributes. + toJSON: function(options) { + return this.map(function(model) { return model.toJSON(options); }); + }, + + // Proxy `Backbone.sync` by default. + sync: function() { + return Backbone.sync.apply(this, arguments); + }, + + // Add a model, or list of models to the set. `models` may be Backbone + // Models or raw JavaScript objects to be converted to Models, or any + // combination of the two. + add: function(models, options) { + return this.set(models, _.extend({merge: false}, options, addOptions)); + }, + + // Remove a model, or a list of models from the set. + remove: function(models, options) { + options = _.extend({}, options); + var singular = !_.isArray(models); + models = singular ? [models] : models.slice(); + var removed = this._removeModels(models, options); + if (!options.silent && removed.length) { + options.changes = {added: [], merged: [], removed: removed}; + this.trigger('update', this, options); + } + return singular ? removed[0] : removed; + }, + + // Update a collection by `set`-ing a new list of models, adding new ones, + // removing models that are no longer present, and merging models that + // already exist in the collection, as necessary. Similar to **Model#set**, + // the core operation for updating the data contained by the collection. + set: function(models, options) { + if (models == null) return; + + options = _.extend({}, setOptions, options); + if (options.parse && !this._isModel(models)) { + models = this.parse(models, options) || []; + } + + var singular = !_.isArray(models); + models = singular ? [models] : models.slice(); + + var at = options.at; + if (at != null) at = +at; + if (at > this.length) at = this.length; + if (at < 0) at += this.length + 1; + + var set = []; + var toAdd = []; + var toMerge = []; + var toRemove = []; + var modelMap = {}; + + var add = options.add; + var merge = options.merge; + var remove = options.remove; + + var sort = false; + var sortable = this.comparator && at == null && options.sort !== false; + var sortAttr = _.isString(this.comparator) ? this.comparator : null; + + // Turn bare objects into model references, and prevent invalid models + // from being added. + var model, i; + for (i = 0; i < models.length; i++) { + model = models[i]; + + // If a duplicate is found, prevent it from being added and + // optionally merge it into the existing model. + var existing = this.get(model); + if (existing) { + if (merge && model !== existing) { + var attrs = this._isModel(model) ? model.attributes : model; + if (options.parse) attrs = existing.parse(attrs, options); + existing.set(attrs, options); + toMerge.push(existing); + if (sortable && !sort) sort = existing.hasChanged(sortAttr); + } + if (!modelMap[existing.cid]) { + modelMap[existing.cid] = true; + set.push(existing); + } + models[i] = existing; + + // If this is a new, valid model, push it to the `toAdd` list. + } else if (add) { + model = models[i] = this._prepareModel(model, options); + if (model) { + toAdd.push(model); + this._addReference(model, options); + modelMap[model.cid] = true; + set.push(model); + } + } + } + + // Remove stale models. + if (remove) { + for (i = 0; i < this.length; i++) { + model = this.models[i]; + if (!modelMap[model.cid]) toRemove.push(model); + } + if (toRemove.length) this._removeModels(toRemove, options); + } + + // See if sorting is needed, update `length` and splice in new models. + var orderChanged = false; + var replace = !sortable && add && remove; + if (set.length && replace) { + orderChanged = this.length !== set.length || _.some(this.models, function(m, index) { + return m !== set[index]; + }); + this.models.length = 0; + splice(this.models, set, 0); + this.length = this.models.length; + } else if (toAdd.length) { + if (sortable) sort = true; + splice(this.models, toAdd, at == null ? this.length : at); + this.length = this.models.length; + } + + // Silently sort the collection if appropriate. + if (sort) this.sort({silent: true}); + + // Unless silenced, it's time to fire all appropriate add/sort/update events. + if (!options.silent) { + for (i = 0; i < toAdd.length; i++) { + if (at != null) options.index = at + i; + model = toAdd[i]; + model.trigger('add', model, this, options); + } + if (sort || orderChanged) this.trigger('sort', this, options); + if (toAdd.length || toRemove.length || toMerge.length) { + options.changes = { + added: toAdd, + removed: toRemove, + merged: toMerge + }; + this.trigger('update', this, options); + } + } + + // Return the added (or merged) model (or models). + return singular ? models[0] : models; + }, + + // When you have more items than you want to add or remove individually, + // you can reset the entire set with a new list of models, without firing + // any granular `add` or `remove` events. Fires `reset` when finished. + // Useful for bulk operations and optimizations. + reset: function(models, options) { + options = options ? _.clone(options) : {}; + for (var i = 0; i < this.models.length; i++) { + this._removeReference(this.models[i], options); + } + options.previousModels = this.models; + this._reset(); + models = this.add(models, _.extend({silent: true}, options)); + if (!options.silent) this.trigger('reset', this, options); + return models; + }, + + // Add a model to the end of the collection. + push: function(model, options) { + return this.add(model, _.extend({at: this.length}, options)); + }, + + // Remove a model from the end of the collection. + pop: function(options) { + var model = this.at(this.length - 1); + return this.remove(model, options); + }, + + // Add a model to the beginning of the collection. + unshift: function(model, options) { + return this.add(model, _.extend({at: 0}, options)); + }, + + // Remove a model from the beginning of the collection. + shift: function(options) { + var model = this.at(0); + return this.remove(model, options); + }, + + // Slice out a sub-array of models from the collection. + slice: function() { + return slice.apply(this.models, arguments); + }, + + // Get a model from the set by id, cid, model object with id or cid + // properties, or an attributes object that is transformed through modelId. + get: function(obj) { + if (obj == null) return void 0; + return this._byId[obj] || + this._byId[this.modelId(obj.attributes || obj)] || + obj.cid && this._byId[obj.cid]; + }, + + // Returns `true` if the model is in the collection. + has: function(obj) { + return this.get(obj) != null; + }, + + // Get the model at the given index. + at: function(index) { + if (index < 0) index += this.length; + return this.models[index]; + }, + + // Return models with matching attributes. Useful for simple cases of + // `filter`. + where: function(attrs, first) { + return this[first ? 'find' : 'filter'](attrs); + }, + + // Return the first model with matching attributes. Useful for simple cases + // of `find`. + findWhere: function(attrs) { + return this.where(attrs, true); + }, + + // Force the collection to re-sort itself. You don't need to call this under + // normal circumstances, as the set will maintain sort order as each item + // is added. + sort: function(options) { + var comparator = this.comparator; + if (!comparator) throw new Error('Cannot sort a set without a comparator'); + options || (options = {}); + + var length = comparator.length; + if (_.isFunction(comparator)) comparator = _.bind(comparator, this); + + // Run sort based on type of `comparator`. + if (length === 1 || _.isString(comparator)) { + this.models = this.sortBy(comparator); + } else { + this.models.sort(comparator); + } + if (!options.silent) this.trigger('sort', this, options); + return this; + }, + + // Pluck an attribute from each model in the collection. + pluck: function(attr) { + return this.map(attr + ''); + }, + + // Fetch the default set of models for this collection, resetting the + // collection when they arrive. If `reset: true` is passed, the response + // data will be passed through the `reset` method instead of `set`. + fetch: function(options) { + options = _.extend({parse: true}, options); + var success = options.success; + var collection = this; + options.success = function(resp) { + var method = options.reset ? 'reset' : 'set'; + collection[method](resp, options); + if (success) success.call(options.context, collection, resp, options); + collection.trigger('sync', collection, resp, options); + }; + wrapError(this, options); + return this.sync('read', this, options); + }, + + // Create a new instance of a model in this collection. Add the model to the + // collection immediately, unless `wait: true` is passed, in which case we + // wait for the server to agree. + create: function(model, options) { + options = options ? _.clone(options) : {}; + var wait = options.wait; + model = this._prepareModel(model, options); + if (!model) return false; + if (!wait) this.add(model, options); + var collection = this; + var success = options.success; + options.success = function(m, resp, callbackOpts) { + if (wait) collection.add(m, callbackOpts); + if (success) success.call(callbackOpts.context, m, resp, callbackOpts); + }; + model.save(null, options); + return model; + }, + + // **parse** converts a response into a list of models to be added to the + // collection. The default implementation is just to pass it through. + parse: function(resp, options) { + return resp; + }, + + // Create a new collection with an identical list of models as this one. + clone: function() { + return new this.constructor(this.models, { + model: this.model, + comparator: this.comparator + }); + }, + + // Define how to uniquely identify models in the collection. + modelId: function(attrs) { + return attrs[this.model.prototype.idAttribute || 'id']; + }, + + // Private method to reset all internal state. Called when the collection + // is first initialized or reset. + _reset: function() { + this.length = 0; + this.models = []; + this._byId = {}; + }, + + // Prepare a hash of attributes (or other model) to be added to this + // collection. + _prepareModel: function(attrs, options) { + if (this._isModel(attrs)) { + if (!attrs.collection) attrs.collection = this; + return attrs; + } + options = options ? _.clone(options) : {}; + options.collection = this; + var model = new this.model(attrs, options); + if (!model.validationError) return model; + this.trigger('invalid', this, model.validationError, options); + return false; + }, + + // Internal method called by both remove and set. + _removeModels: function(models, options) { + var removed = []; + for (var i = 0; i < models.length; i++) { + var model = this.get(models[i]); + if (!model) continue; + + var index = this.indexOf(model); + this.models.splice(index, 1); + this.length--; + + // Remove references before triggering 'remove' event to prevent an + // infinite loop. #3693 + delete this._byId[model.cid]; + var id = this.modelId(model.attributes); + if (id != null) delete this._byId[id]; + + if (!options.silent) { + options.index = index; + model.trigger('remove', model, this, options); + } + + removed.push(model); + this._removeReference(model, options); + } + return removed; + }, + + // Method for checking whether an object should be considered a model for + // the purposes of adding to the collection. + _isModel: function(model) { + return model instanceof Model; + }, + + // Internal method to create a model's ties to a collection. + _addReference: function(model, options) { + this._byId[model.cid] = model; + var id = this.modelId(model.attributes); + if (id != null) this._byId[id] = model; + model.on('all', this._onModelEvent, this); + }, + + // Internal method to sever a model's ties to a collection. + _removeReference: function(model, options) { + delete this._byId[model.cid]; + var id = this.modelId(model.attributes); + if (id != null) delete this._byId[id]; + if (this === model.collection) delete model.collection; + model.off('all', this._onModelEvent, this); + }, + + // Internal method called every time a model in the set fires an event. + // Sets need to update their indexes when models change ids. All other + // events simply proxy through. "add" and "remove" events that originate + // in other collections are ignored. + _onModelEvent: function(event, model, collection, options) { + if (model) { + if ((event === 'add' || event === 'remove') && collection !== this) return; + if (event === 'destroy') this.remove(model, options); + if (event === 'change') { + var prevId = this.modelId(model.previousAttributes()); + var id = this.modelId(model.attributes); + if (prevId !== id) { + if (prevId != null) delete this._byId[prevId]; + if (id != null) this._byId[id] = model; + } + } + } + this.trigger.apply(this, arguments); + } + + }); + + // Underscore methods that we want to implement on the Collection. + // 90% of the core usefulness of Backbone Collections is actually implemented + // right here: + var collectionMethods = {forEach: 3, each: 3, map: 3, collect: 3, reduce: 0, + foldl: 0, inject: 0, reduceRight: 0, foldr: 0, find: 3, detect: 3, filter: 3, + select: 3, reject: 3, every: 3, all: 3, some: 3, any: 3, include: 3, includes: 3, + contains: 3, invoke: 0, max: 3, min: 3, toArray: 1, size: 1, first: 3, + head: 3, take: 3, initial: 3, rest: 3, tail: 3, drop: 3, last: 3, + without: 0, difference: 0, indexOf: 3, shuffle: 1, lastIndexOf: 3, + isEmpty: 1, chain: 1, sample: 3, partition: 3, groupBy: 3, countBy: 3, + sortBy: 3, indexBy: 3, findIndex: 3, findLastIndex: 3}; + + // Mix in each Underscore method as a proxy to `Collection#models`. + addUnderscoreMethods(Collection, collectionMethods, 'models'); + + // Backbone.View + // ------------- + + // Backbone Views are almost more convention than they are actual code. A View + // is simply a JavaScript object that represents a logical chunk of UI in the + // DOM. This might be a single item, an entire list, a sidebar or panel, or + // even the surrounding frame which wraps your whole app. Defining a chunk of + // UI as a **View** allows you to define your DOM events declaratively, without + // having to worry about render order ... and makes it easy for the view to + // react to specific changes in the state of your models. + + // Creating a Backbone.View creates its initial element outside of the DOM, + // if an existing element is not provided... + var View = Backbone.View = function(options) { + this.cid = _.uniqueId('view'); + _.extend(this, _.pick(options, viewOptions)); + this._ensureElement(); + this.initialize.apply(this, arguments); + }; + + // Cached regex to split keys for `delegate`. + var delegateEventSplitter = /^(\S+)\s*(.*)$/; + + // List of view options to be set as properties. + var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events']; + + // Set up all inheritable **Backbone.View** properties and methods. + _.extend(View.prototype, Events, { + + // The default `tagName` of a View's element is `"div"`. + tagName: 'div', + + // jQuery delegate for element lookup, scoped to DOM elements within the + // current view. This should be preferred to global lookups where possible. + $: function(selector) { + return this.$el.find(selector); + }, + + // Initialize is an empty function by default. Override it with your own + // initialization logic. + initialize: function(){}, + + // **render** is the core function that your view should override, in order + // to populate its element (`this.el`), with the appropriate HTML. The + // convention is for **render** to always return `this`. + render: function() { + return this; + }, + + // Remove this view by taking the element out of the DOM, and removing any + // applicable Backbone.Events listeners. + remove: function() { + this._removeElement(); + this.stopListening(); + return this; + }, + + // Remove this view's element from the document and all event listeners + // attached to it. Exposed for subclasses using an alternative DOM + // manipulation API. + _removeElement: function() { + this.$el.remove(); + }, + + // Change the view's element (`this.el` property) and re-delegate the + // view's events on the new element. + setElement: function(element) { + this.undelegateEvents(); + this._setElement(element); + this.delegateEvents(); + return this; + }, + + // Creates the `this.el` and `this.$el` references for this view using the + // given `el`. `el` can be a CSS selector or an HTML string, a jQuery + // context or an element. Subclasses can override this to utilize an + // alternative DOM manipulation API and are only required to set the + // `this.el` property. + _setElement: function(el) { + this.$el = el instanceof Backbone.$ ? el : Backbone.$(el); + this.el = this.$el[0]; + }, + + // Set callbacks, where `this.events` is a hash of + // + // *{"event selector": "callback"}* + // + // { + // 'mousedown .title': 'edit', + // 'click .button': 'save', + // 'click .open': function(e) { ... } + // } + // + // pairs. Callbacks will be bound to the view, with `this` set properly. + // Uses event delegation for efficiency. + // Omitting the selector binds the event to `this.el`. + delegateEvents: function(events) { + events || (events = _.result(this, 'events')); + if (!events) return this; + this.undelegateEvents(); + for (var key in events) { + var method = events[key]; + if (!_.isFunction(method)) method = this[method]; + if (!method) continue; + var match = key.match(delegateEventSplitter); + this.delegate(match[1], match[2], _.bind(method, this)); + } + return this; + }, + + // Add a single event listener to the view's element (or a child element + // using `selector`). This only works for delegate-able events: not `focus`, + // `blur`, and not `change`, `submit`, and `reset` in Internet Explorer. + delegate: function(eventName, selector, listener) { + this.$el.on(eventName + '.delegateEvents' + this.cid, selector, listener); + return this; + }, + + // Clears all callbacks previously bound to the view by `delegateEvents`. + // You usually don't need to use this, but may wish to if you have multiple + // Backbone views attached to the same DOM element. + undelegateEvents: function() { + if (this.$el) this.$el.off('.delegateEvents' + this.cid); + return this; + }, + + // A finer-grained `undelegateEvents` for removing a single delegated event. + // `selector` and `listener` are both optional. + undelegate: function(eventName, selector, listener) { + this.$el.off(eventName + '.delegateEvents' + this.cid, selector, listener); + return this; + }, + + // Produces a DOM element to be assigned to your view. Exposed for + // subclasses using an alternative DOM manipulation API. + _createElement: function(tagName) { + return document.createElement(tagName); + }, + + // Ensure that the View has a DOM element to render into. + // If `this.el` is a string, pass it through `$()`, take the first + // matching element, and re-assign it to `el`. Otherwise, create + // an element from the `id`, `className` and `tagName` properties. + _ensureElement: function() { + if (!this.el) { + var attrs = _.extend({}, _.result(this, 'attributes')); + if (this.id) attrs.id = _.result(this, 'id'); + if (this.className) attrs['class'] = _.result(this, 'className'); + this.setElement(this._createElement(_.result(this, 'tagName'))); + this._setAttributes(attrs); + } else { + this.setElement(_.result(this, 'el')); + } + }, + + // Set attributes from a hash on this view's element. Exposed for + // subclasses using an alternative DOM manipulation API. + _setAttributes: function(attributes) { + this.$el.attr(attributes); + } + + }); + + // Backbone.sync + // ------------- + + // Override this function to change the manner in which Backbone persists + // models to the server. You will be passed the type of request, and the + // model in question. By default, makes a RESTful Ajax request + // to the model's `url()`. Some possible customizations could be: + // + // * Use `setTimeout` to batch rapid-fire updates into a single request. + // * Send up the models as XML instead of JSON. + // * Persist models via WebSockets instead of Ajax. + // + // Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests + // as `POST`, with a `_method` parameter containing the true HTTP method, + // as well as all requests with the body as `application/x-www-form-urlencoded` + // instead of `application/json` with the model in a param named `model`. + // Useful when interfacing with server-side languages like **PHP** that make + // it difficult to read the body of `PUT` requests. + Backbone.sync = function(method, model, options) { + var type = methodMap[method]; + + // Default options, unless specified. + _.defaults(options || (options = {}), { + emulateHTTP: Backbone.emulateHTTP, + emulateJSON: Backbone.emulateJSON + }); + + // Default JSON-request options. + var params = {type: type, dataType: 'json'}; + + // Ensure that we have a URL. + if (!options.url) { + params.url = _.result(model, 'url') || urlError(); + } + + // Ensure that we have the appropriate request data. + if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) { + params.contentType = 'application/json'; + params.data = JSON.stringify(options.attrs || model.toJSON(options)); + } + + // For older servers, emulate JSON by encoding the request into an HTML-form. + if (options.emulateJSON) { + params.contentType = 'application/x-www-form-urlencoded'; + params.data = params.data ? {model: params.data} : {}; + } + + // For older servers, emulate HTTP by mimicking the HTTP method with `_method` + // And an `X-HTTP-Method-Override` header. + if (options.emulateHTTP && (type === 'PUT' || type === 'DELETE' || type === 'PATCH')) { + params.type = 'POST'; + if (options.emulateJSON) params.data._method = type; + var beforeSend = options.beforeSend; + options.beforeSend = function(xhr) { + xhr.setRequestHeader('X-HTTP-Method-Override', type); + if (beforeSend) return beforeSend.apply(this, arguments); + }; + } + + // Don't process data on a non-GET request. + if (params.type !== 'GET' && !options.emulateJSON) { + params.processData = false; + } + + // Pass along `textStatus` and `errorThrown` from jQuery. + var error = options.error; + options.error = function(xhr, textStatus, errorThrown) { + options.textStatus = textStatus; + options.errorThrown = errorThrown; + if (error) error.call(options.context, xhr, textStatus, errorThrown); + }; + + // Make the request, allowing the user to override any Ajax options. + var xhr = options.xhr = Backbone.ajax(_.extend(params, options)); + model.trigger('request', model, xhr, options); + return xhr; + }; + + // Map from CRUD to HTTP for our default `Backbone.sync` implementation. + var methodMap = { + 'create': 'POST', + 'update': 'PUT', + 'patch': 'PATCH', + 'delete': 'DELETE', + 'read': 'GET' + }; + + // Set the default implementation of `Backbone.ajax` to proxy through to `$`. + // Override this if you'd like to use a different library. + Backbone.ajax = function() { + return Backbone.$.ajax.apply(Backbone.$, arguments); + }; + + // Backbone.Router + // --------------- + + // Routers map faux-URLs to actions, and fire events when routes are + // matched. Creating a new one sets its `routes` hash, if not set statically. + var Router = Backbone.Router = function(options) { + options || (options = {}); + if (options.routes) this.routes = options.routes; + this._bindRoutes(); + this.initialize.apply(this, arguments); + }; + + // Cached regular expressions for matching named param parts and splatted + // parts of route strings. + var optionalParam = /\((.*?)\)/g; + var namedParam = /(\(\?)?:\w+/g; + var splatParam = /\*\w+/g; + var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g; + + // Set up all inheritable **Backbone.Router** properties and methods. + _.extend(Router.prototype, Events, { + + // Initialize is an empty function by default. Override it with your own + // initialization logic. + initialize: function(){}, + + // Manually bind a single named route to a callback. For example: + // + // this.route('search/:query/p:num', 'search', function(query, num) { + // ... + // }); + // + route: function(route, name, callback) { + if (!_.isRegExp(route)) route = this._routeToRegExp(route); + if (_.isFunction(name)) { + callback = name; + name = ''; + } + if (!callback) callback = this[name]; + var router = this; + Backbone.history.route(route, function(fragment) { + var args = router._extractParameters(route, fragment); + if (router.execute(callback, args, name) !== false) { + router.trigger.apply(router, ['route:' + name].concat(args)); + router.trigger('route', name, args); + Backbone.history.trigger('route', router, name, args); + } + }); + return this; + }, + + // Execute a route handler with the provided parameters. This is an + // excellent place to do pre-route setup or post-route cleanup. + execute: function(callback, args, name) { + if (callback) callback.apply(this, args); + }, + + // Simple proxy to `Backbone.history` to save a fragment into the history. + navigate: function(fragment, options) { + Backbone.history.navigate(fragment, options); + return this; + }, + + // Bind all defined routes to `Backbone.history`. We have to reverse the + // order of the routes here to support behavior where the most general + // routes can be defined at the bottom of the route map. + _bindRoutes: function() { + if (!this.routes) return; + this.routes = _.result(this, 'routes'); + var route, routes = _.keys(this.routes); + while ((route = routes.pop()) != null) { + this.route(route, this.routes[route]); + } + }, + + // Convert a route string into a regular expression, suitable for matching + // against the current location hash. + _routeToRegExp: function(route) { + route = route.replace(escapeRegExp, '\\$&') + .replace(optionalParam, '(?:$1)?') + .replace(namedParam, function(match, optional) { + return optional ? match : '([^/?]+)'; + }) + .replace(splatParam, '([^?]*?)'); + return new RegExp('^' + route + '(?:\\?([\\s\\S]*))?$'); + }, + + // Given a route, and a URL fragment that it matches, return the array of + // extracted decoded parameters. Empty or unmatched parameters will be + // treated as `null` to normalize cross-browser behavior. + _extractParameters: function(route, fragment) { + var params = route.exec(fragment).slice(1); + return _.map(params, function(param, i) { + // Don't decode the search params. + if (i === params.length - 1) return param || null; + return param ? decodeURIComponent(param) : null; + }); + } + + }); + + // Backbone.History + // ---------------- + + // Handles cross-browser history management, based on either + // [pushState](http://diveintohtml5.info/history.html) and real URLs, or + // [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange) + // and URL fragments. If the browser supports neither (old IE, natch), + // falls back to polling. + var History = Backbone.History = function() { + this.handlers = []; + this.checkUrl = _.bind(this.checkUrl, this); + + // Ensure that `History` can be used outside of the browser. + if (typeof window !== 'undefined') { + this.location = window.location; + this.history = window.history; + } + }; + + // Cached regex for stripping a leading hash/slash and trailing space. + var routeStripper = /^[#\/]|\s+$/g; + + // Cached regex for stripping leading and trailing slashes. + var rootStripper = /^\/+|\/+$/g; + + // Cached regex for stripping urls of hash. + var pathStripper = /#.*$/; + + // Has the history handling already been started? + History.started = false; + + // Set up all inheritable **Backbone.History** properties and methods. + _.extend(History.prototype, Events, { + + // The default interval to poll for hash changes, if necessary, is + // twenty times a second. + interval: 50, + + // Are we at the app root? + atRoot: function() { + var path = this.location.pathname.replace(/[^\/]$/, '$&/'); + return path === this.root && !this.getSearch(); + }, + + // Does the pathname match the root? + matchRoot: function() { + var path = this.decodeFragment(this.location.pathname); + var rootPath = path.slice(0, this.root.length - 1) + '/'; + return rootPath === this.root; + }, + + // Unicode characters in `location.pathname` are percent encoded so they're + // decoded for comparison. `%25` should not be decoded since it may be part + // of an encoded parameter. + decodeFragment: function(fragment) { + return decodeURI(fragment.replace(/%25/g, '%2525')); + }, + + // In IE6, the hash fragment and search params are incorrect if the + // fragment contains `?`. + getSearch: function() { + var match = this.location.href.replace(/#.*/, '').match(/\?.+/); + return match ? match[0] : ''; + }, + + // Gets the true hash value. Cannot use location.hash directly due to bug + // in Firefox where location.hash will always be decoded. + getHash: function(window) { + var match = (window || this).location.href.match(/#(.*)$/); + return match ? match[1] : ''; + }, + + // Get the pathname and search params, without the root. + getPath: function() { + var path = this.decodeFragment( + this.location.pathname + this.getSearch() + ).slice(this.root.length - 1); + return path.charAt(0) === '/' ? path.slice(1) : path; + }, + + // Get the cross-browser normalized URL fragment from the path or hash. + getFragment: function(fragment) { + if (fragment == null) { + if (this._usePushState || !this._wantsHashChange) { + fragment = this.getPath(); + } else { + fragment = this.getHash(); + } + } + return fragment.replace(routeStripper, ''); + }, + + // Start the hash change handling, returning `true` if the current URL matches + // an existing route, and `false` otherwise. + start: function(options) { + if (History.started) throw new Error('Backbone.history has already been started'); + History.started = true; + + // Figure out the initial configuration. Do we need an iframe? + // Is pushState desired ... is it available? + this.options = _.extend({root: '/'}, this.options, options); + this.root = this.options.root; + this._wantsHashChange = this.options.hashChange !== false; + this._hasHashChange = 'onhashchange' in window && (document.documentMode === void 0 || document.documentMode > 7); + this._useHashChange = this._wantsHashChange && this._hasHashChange; + this._wantsPushState = !!this.options.pushState; + this._hasPushState = !!(this.history && this.history.pushState); + this._usePushState = this._wantsPushState && this._hasPushState; + this.fragment = this.getFragment(); + + // Normalize root to always include a leading and trailing slash. + this.root = ('/' + this.root + '/').replace(rootStripper, '/'); + + // Transition from hashChange to pushState or vice versa if both are + // requested. + if (this._wantsHashChange && this._wantsPushState) { + + // If we've started off with a route from a `pushState`-enabled + // browser, but we're currently in a browser that doesn't support it... + if (!this._hasPushState && !this.atRoot()) { + var rootPath = this.root.slice(0, -1) || '/'; + this.location.replace(rootPath + '#' + this.getPath()); + // Return immediately as browser will do redirect to new url + return true; + + // Or if we've started out with a hash-based route, but we're currently + // in a browser where it could be `pushState`-based instead... + } else if (this._hasPushState && this.atRoot()) { + this.navigate(this.getHash(), {replace: true}); + } + + } + + // Proxy an iframe to handle location events if the browser doesn't + // support the `hashchange` event, HTML5 history, or the user wants + // `hashChange` but not `pushState`. + if (!this._hasHashChange && this._wantsHashChange && !this._usePushState) { + this.iframe = document.createElement('iframe'); + this.iframe.src = 'javascript:0'; + this.iframe.style.display = 'none'; + this.iframe.tabIndex = -1; + var body = document.body; + // Using `appendChild` will throw on IE < 9 if the document is not ready. + var iWindow = body.insertBefore(this.iframe, body.firstChild).contentWindow; + iWindow.document.open(); + iWindow.document.close(); + iWindow.location.hash = '#' + this.fragment; + } + + // Add a cross-platform `addEventListener` shim for older browsers. + var addEventListener = window.addEventListener || function(eventName, listener) { + return attachEvent('on' + eventName, listener); + }; + + // Depending on whether we're using pushState or hashes, and whether + // 'onhashchange' is supported, determine how we check the URL state. + if (this._usePushState) { + addEventListener('popstate', this.checkUrl, false); + } else if (this._useHashChange && !this.iframe) { + addEventListener('hashchange', this.checkUrl, false); + } else if (this._wantsHashChange) { + this._checkUrlInterval = setInterval(this.checkUrl, this.interval); + } + + if (!this.options.silent) return this.loadUrl(); + }, + + // Disable Backbone.history, perhaps temporarily. Not useful in a real app, + // but possibly useful for unit testing Routers. + stop: function() { + // Add a cross-platform `removeEventListener` shim for older browsers. + var removeEventListener = window.removeEventListener || function(eventName, listener) { + return detachEvent('on' + eventName, listener); + }; + + // Remove window listeners. + if (this._usePushState) { + removeEventListener('popstate', this.checkUrl, false); + } else if (this._useHashChange && !this.iframe) { + removeEventListener('hashchange', this.checkUrl, false); + } + + // Clean up the iframe if necessary. + if (this.iframe) { + document.body.removeChild(this.iframe); + this.iframe = null; + } + + // Some environments will throw when clearing an undefined interval. + if (this._checkUrlInterval) clearInterval(this._checkUrlInterval); + History.started = false; + }, + + // Add a route to be tested when the fragment changes. Routes added later + // may override previous routes. + route: function(route, callback) { + this.handlers.unshift({route: route, callback: callback}); + }, + + // Checks the current URL to see if it has changed, and if it has, + // calls `loadUrl`, normalizing across the hidden iframe. + checkUrl: function(e) { + var current = this.getFragment(); + + // If the user pressed the back button, the iframe's hash will have + // changed and we should use that for comparison. + if (current === this.fragment && this.iframe) { + current = this.getHash(this.iframe.contentWindow); + } + + if (current === this.fragment) return false; + if (this.iframe) this.navigate(current); + this.loadUrl(); + }, + + // Attempt to load the current URL fragment. If a route succeeds with a + // match, returns `true`. If no defined routes matches the fragment, + // returns `false`. + loadUrl: function(fragment) { + // If the root doesn't match, no routes can match either. + if (!this.matchRoot()) return false; + fragment = this.fragment = this.getFragment(fragment); + return _.some(this.handlers, function(handler) { + if (handler.route.test(fragment)) { + handler.callback(fragment); + return true; + } + }); + }, + + // Save a fragment into the hash history, or replace the URL state if the + // 'replace' option is passed. You are responsible for properly URL-encoding + // the fragment in advance. + // + // The options object can contain `trigger: true` if you wish to have the + // route callback be fired (not usually desirable), or `replace: true`, if + // you wish to modify the current URL without adding an entry to the history. + navigate: function(fragment, options) { + if (!History.started) return false; + if (!options || options === true) options = {trigger: !!options}; + + // Normalize the fragment. + fragment = this.getFragment(fragment || ''); + + // Don't include a trailing slash on the root. + var rootPath = this.root; + if (fragment === '' || fragment.charAt(0) === '?') { + rootPath = rootPath.slice(0, -1) || '/'; + } + var url = rootPath + fragment; + + // Strip the hash and decode for matching. + fragment = this.decodeFragment(fragment.replace(pathStripper, '')); + + if (this.fragment === fragment) return; + this.fragment = fragment; + + // If pushState is available, we use it to set the fragment as a real URL. + if (this._usePushState) { + this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url); + + // If hash changes haven't been explicitly disabled, update the hash + // fragment to store history. + } else if (this._wantsHashChange) { + this._updateHash(this.location, fragment, options.replace); + if (this.iframe && fragment !== this.getHash(this.iframe.contentWindow)) { + var iWindow = this.iframe.contentWindow; + + // Opening and closing the iframe tricks IE7 and earlier to push a + // history entry on hash-tag change. When replace is true, we don't + // want this. + if (!options.replace) { + iWindow.document.open(); + iWindow.document.close(); + } + + this._updateHash(iWindow.location, fragment, options.replace); + } + + // If you've told us that you explicitly don't want fallback hashchange- + // based history, then `navigate` becomes a page refresh. + } else { + return this.location.assign(url); + } + if (options.trigger) return this.loadUrl(fragment); + }, + + // Update the hash location, either replacing the current entry, or adding + // a new one to the browser history. + _updateHash: function(location, fragment, replace) { + if (replace) { + var href = location.href.replace(/(javascript:|#).*$/, ''); + location.replace(href + '#' + fragment); + } else { + // Some browsers require that `hash` contains a leading #. + location.hash = '#' + fragment; + } + } + + }); + + // Create the default Backbone.history. + Backbone.history = new History; + + // Helpers + // ------- + + // Helper function to correctly set up the prototype chain for subclasses. + // Similar to `goog.inherits`, but uses a hash of prototype properties and + // class properties to be extended. + var extend = function(protoProps, staticProps) { + var parent = this; + var child; + + // The constructor function for the new subclass is either defined by you + // (the "constructor" property in your `extend` definition), or defaulted + // by us to simply call the parent constructor. + if (protoProps && _.has(protoProps, 'constructor')) { + child = protoProps.constructor; + } else { + child = function(){ return parent.apply(this, arguments); }; + } + + // Add static properties to the constructor function, if supplied. + _.extend(child, parent, staticProps); + + // Set the prototype chain to inherit from `parent`, without calling + // `parent`'s constructor function and add the prototype properties. + child.prototype = _.create(parent.prototype, protoProps); + child.prototype.constructor = child; + + // Set a convenience property in case the parent's prototype is needed + // later. + child.__super__ = parent.prototype; + + return child; + }; + + // Set up inheritance for the model, collection, router, view and history. + Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend; + + // Throw an error when a URL is needed, and none is supplied. + var urlError = function() { + throw new Error('A "url" property or function must be specified'); + }; + + // Wrap an optional error callback with a fallback error event. + var wrapError = function(model, options) { + var error = options.error; + options.error = function(resp) { + if (error) error.call(options.context, model, resp, options); + model.trigger('error', model, resp, options); + }; + }; + + return Backbone; +}); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/collection.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/collection.js new file mode 100644 index 00000000..dd98aca5 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/collection.js @@ -0,0 +1,1998 @@ +(function() { + + var a, b, c, d, e, col, otherCol; + + QUnit.module('Backbone.Collection', { + + beforeEach: function(assert) { + a = new Backbone.Model({id: 3, label: 'a'}); + b = new Backbone.Model({id: 2, label: 'b'}); + c = new Backbone.Model({id: 1, label: 'c'}); + d = new Backbone.Model({id: 0, label: 'd'}); + e = null; + col = new Backbone.Collection([a, b, c, d]); + otherCol = new Backbone.Collection(); + } + + }); + + QUnit.test('new and sort', function(assert) { + assert.expect(6); + var counter = 0; + col.on('sort', function(){ counter++; }); + assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']); + col.comparator = function(m1, m2) { + return m1.id > m2.id ? -1 : 1; + }; + col.sort(); + assert.equal(counter, 1); + assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']); + col.comparator = function(model) { return model.id; }; + col.sort(); + assert.equal(counter, 2); + assert.deepEqual(col.pluck('label'), ['d', 'c', 'b', 'a']); + assert.equal(col.length, 4); + }); + + QUnit.test('String comparator.', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([ + {id: 3}, + {id: 1}, + {id: 2} + ], {comparator: 'id'}); + assert.deepEqual(collection.pluck('id'), [1, 2, 3]); + }); + + QUnit.test('new and parse', function(assert) { + assert.expect(3); + var Collection = Backbone.Collection.extend({ + parse: function(data) { + return _.filter(data, function(datum) { + return datum.a % 2 === 0; + }); + } + }); + var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; + var collection = new Collection(models, {parse: true}); + assert.strictEqual(collection.length, 2); + assert.strictEqual(collection.first().get('a'), 2); + assert.strictEqual(collection.last().get('a'), 4); + }); + + QUnit.test('clone preserves model and comparator', function(assert) { + assert.expect(3); + var Model = Backbone.Model.extend(); + var comparator = function(model){ return model.id; }; + + var collection = new Backbone.Collection([{id: 1}], { + model: Model, + comparator: comparator + }).clone(); + collection.add({id: 2}); + assert.ok(collection.at(0) instanceof Model); + assert.ok(collection.at(1) instanceof Model); + assert.strictEqual(collection.comparator, comparator); + }); + + QUnit.test('get', function(assert) { + assert.expect(6); + assert.equal(col.get(0), d); + assert.equal(col.get(d.clone()), d); + assert.equal(col.get(2), b); + assert.equal(col.get({id: 1}), c); + assert.equal(col.get(c.clone()), c); + assert.equal(col.get(col.first().cid), col.first()); + }); + + QUnit.test('get with non-default ids', function(assert) { + assert.expect(5); + var MongoModel = Backbone.Model.extend({idAttribute: '_id'}); + var model = new MongoModel({_id: 100}); + var collection = new Backbone.Collection([model], {model: MongoModel}); + assert.equal(collection.get(100), model); + assert.equal(collection.get(model.cid), model); + assert.equal(collection.get(model), model); + assert.equal(collection.get(101), void 0); + + var collection2 = new Backbone.Collection(); + collection2.model = MongoModel; + collection2.add(model.attributes); + assert.equal(collection2.get(model.clone()), collection2.first()); + }); + + QUnit.test('has', function(assert) { + assert.expect(15); + assert.ok(col.has(a)); + assert.ok(col.has(b)); + assert.ok(col.has(c)); + assert.ok(col.has(d)); + assert.ok(col.has(a.id)); + assert.ok(col.has(b.id)); + assert.ok(col.has(c.id)); + assert.ok(col.has(d.id)); + assert.ok(col.has(a.cid)); + assert.ok(col.has(b.cid)); + assert.ok(col.has(c.cid)); + assert.ok(col.has(d.cid)); + var outsider = new Backbone.Model({id: 4}); + assert.notOk(col.has(outsider)); + assert.notOk(col.has(outsider.id)); + assert.notOk(col.has(outsider.cid)); + }); + + QUnit.test('update index when id changes', function(assert) { + assert.expect(4); + var collection = new Backbone.Collection(); + collection.add([ + {id: 0, name: 'one'}, + {id: 1, name: 'two'} + ]); + var one = collection.get(0); + assert.equal(one.get('name'), 'one'); + collection.on('change:name', function(model) { assert.ok(this.get(model)); }); + one.set({name: 'dalmatians', id: 101}); + assert.equal(collection.get(0), null); + assert.equal(collection.get(101).get('name'), 'dalmatians'); + }); + + QUnit.test('at', function(assert) { + assert.expect(2); + assert.equal(col.at(2), c); + assert.equal(col.at(-2), c); + }); + + QUnit.test('pluck', function(assert) { + assert.expect(1); + assert.equal(col.pluck('label').join(' '), 'a b c d'); + }); + + QUnit.test('add', function(assert) { + assert.expect(14); + var added, opts, secondAdded; + added = opts = secondAdded = null; + e = new Backbone.Model({id: 10, label: 'e'}); + otherCol.add(e); + otherCol.on('add', function() { + secondAdded = true; + }); + col.on('add', function(model, collection, options){ + added = model.get('label'); + opts = options; + }); + col.add(e, {amazing: true}); + assert.equal(added, 'e'); + assert.equal(col.length, 5); + assert.equal(col.last(), e); + assert.equal(otherCol.length, 1); + assert.equal(secondAdded, null); + assert.ok(opts.amazing); + + var f = new Backbone.Model({id: 20, label: 'f'}); + var g = new Backbone.Model({id: 21, label: 'g'}); + var h = new Backbone.Model({id: 22, label: 'h'}); + var atCol = new Backbone.Collection([f, g, h]); + assert.equal(atCol.length, 3); + atCol.add(e, {at: 1}); + assert.equal(atCol.length, 4); + assert.equal(atCol.at(1), e); + assert.equal(atCol.last(), h); + + var coll = new Backbone.Collection(new Array(2)); + var addCount = 0; + coll.on('add', function(){ + addCount += 1; + }); + coll.add([undefined, f, g]); + assert.equal(coll.length, 5); + assert.equal(addCount, 3); + coll.add(new Array(4)); + assert.equal(coll.length, 9); + assert.equal(addCount, 7); + }); + + QUnit.test('add multiple models', function(assert) { + assert.expect(6); + var collection = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]); + collection.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2}); + for (var i = 0; i <= 5; i++) { + assert.equal(collection.at(i).get('at'), i); + } + }); + + QUnit.test('add; at should have preference over comparator', function(assert) { + assert.expect(1); + var Col = Backbone.Collection.extend({ + comparator: function(m1, m2) { + return m1.id > m2.id ? -1 : 1; + } + }); + + var collection = new Col([{id: 2}, {id: 3}]); + collection.add(new Backbone.Model({id: 1}), {at: 1}); + + assert.equal(collection.pluck('id').join(' '), '3 1 2'); + }); + + QUnit.test('add; at should add to the end if the index is out of bounds', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 2}, {id: 3}]); + collection.add(new Backbone.Model({id: 1}), {at: 5}); + + assert.equal(collection.pluck('id').join(' '), '2 3 1'); + }); + + QUnit.test("can't add model to collection twice", function(assert) { + var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]); + assert.equal(collection.pluck('id').join(' '), '1 2 3'); + }); + + QUnit.test("can't add different model with same id to collection twice", function(assert) { + assert.expect(1); + var collection = new Backbone.Collection; + collection.unshift({id: 101}); + collection.add({id: 101}); + assert.equal(collection.length, 1); + }); + + QUnit.test('merge in duplicate models with {merge: true}', function(assert) { + assert.expect(3); + var collection = new Backbone.Collection; + collection.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]); + collection.add({id: 1, name: 'Moses'}); + assert.equal(collection.first().get('name'), 'Moe'); + collection.add({id: 1, name: 'Moses'}, {merge: true}); + assert.equal(collection.first().get('name'), 'Moses'); + collection.add({id: 1, name: 'Tim'}, {merge: true, silent: true}); + assert.equal(collection.first().get('name'), 'Tim'); + }); + + QUnit.test('add model to multiple collections', function(assert) { + assert.expect(10); + var counter = 0; + var m = new Backbone.Model({id: 10, label: 'm'}); + m.on('add', function(model, collection) { + counter++; + assert.equal(m, model); + if (counter > 1) { + assert.equal(collection, col2); + } else { + assert.equal(collection, col1); + } + }); + var col1 = new Backbone.Collection([]); + col1.on('add', function(model, collection) { + assert.equal(m, model); + assert.equal(col1, collection); + }); + var col2 = new Backbone.Collection([]); + col2.on('add', function(model, collection) { + assert.equal(m, model); + assert.equal(col2, collection); + }); + col1.add(m); + assert.equal(m.collection, col1); + col2.add(m); + assert.equal(m.collection, col1); + }); + + QUnit.test('add model with parse', function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + parse: function(obj) { + obj.value += 1; + return obj; + } + }); + + var Col = Backbone.Collection.extend({model: Model}); + var collection = new Col; + collection.add({value: 1}, {parse: true}); + assert.equal(collection.at(0).get('value'), 2); + }); + + QUnit.test('add with parse and merge', function(assert) { + var collection = new Backbone.Collection(); + collection.parse = function(attrs) { + return _.map(attrs, function(model) { + if (model.model) return model.model; + return model; + }); + }; + collection.add({id: 1}); + collection.add({model: {id: 1, name: 'Alf'}}, {parse: true, merge: true}); + assert.equal(collection.first().get('name'), 'Alf'); + }); + + QUnit.test('add model to collection with sort()-style comparator', function(assert) { + assert.expect(3); + var collection = new Backbone.Collection; + collection.comparator = function(m1, m2) { + return m1.get('name') < m2.get('name') ? -1 : 1; + }; + var tom = new Backbone.Model({name: 'Tom'}); + var rob = new Backbone.Model({name: 'Rob'}); + var tim = new Backbone.Model({name: 'Tim'}); + collection.add(tom); + collection.add(rob); + collection.add(tim); + assert.equal(collection.indexOf(rob), 0); + assert.equal(collection.indexOf(tim), 1); + assert.equal(collection.indexOf(tom), 2); + }); + + QUnit.test('comparator that depends on `this`', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection; + collection.negative = function(num) { + return -num; + }; + collection.comparator = function(model) { + return this.negative(model.id); + }; + collection.add([{id: 1}, {id: 2}, {id: 3}]); + assert.deepEqual(collection.pluck('id'), [3, 2, 1]); + collection.comparator = function(m1, m2) { + return this.negative(m2.id) - this.negative(m1.id); + }; + collection.sort(); + assert.deepEqual(collection.pluck('id'), [1, 2, 3]); + }); + + QUnit.test('remove', function(assert) { + assert.expect(12); + var removed = null; + var result = null; + col.on('remove', function(model, collection, options) { + removed = model.get('label'); + assert.equal(options.index, 3); + assert.equal(collection.get(model), undefined, '#3693: model cannot be fetched from collection'); + }); + result = col.remove(d); + assert.equal(removed, 'd'); + assert.strictEqual(result, d); + //if we try to remove d again, it's not going to actually get removed + result = col.remove(d); + assert.strictEqual(result, undefined); + assert.equal(col.length, 3); + assert.equal(col.first(), a); + col.off(); + result = col.remove([c, d]); + assert.equal(result.length, 1, 'only returns removed models'); + assert.equal(result[0], c, 'only returns removed models'); + result = col.remove([c, b]); + assert.equal(result.length, 1, 'only returns removed models'); + assert.equal(result[0], b, 'only returns removed models'); + result = col.remove([]); + assert.deepEqual(result, [], 'returns empty array when nothing removed'); + }); + + QUnit.test('add and remove return values', function(assert) { + assert.expect(13); + var Even = Backbone.Model.extend({ + validate: function(attrs) { + if (attrs.id % 2 !== 0) return 'odd'; + } + }); + var collection = new Backbone.Collection; + collection.model = Even; + + var list = collection.add([{id: 2}, {id: 4}], {validate: true}); + assert.equal(list.length, 2); + assert.ok(list[0] instanceof Backbone.Model); + assert.equal(list[1], collection.last()); + assert.equal(list[1].get('id'), 4); + + list = collection.add([{id: 3}, {id: 6}], {validate: true}); + assert.equal(collection.length, 3); + assert.equal(list[0], false); + assert.equal(list[1].get('id'), 6); + + var result = collection.add({id: 6}); + assert.equal(result.cid, list[1].cid); + + result = collection.remove({id: 6}); + assert.equal(collection.length, 2); + assert.equal(result.id, 6); + + list = collection.remove([{id: 2}, {id: 8}]); + assert.equal(collection.length, 1); + assert.equal(list[0].get('id'), 2); + assert.equal(list[1], null); + }); + + QUnit.test('shift and pop', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); + assert.equal(collection.shift().get('a'), 'a'); + assert.equal(collection.pop().get('c'), 'c'); + }); + + QUnit.test('slice', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); + var array = collection.slice(1, 3); + assert.equal(array.length, 2); + assert.equal(array[0].get('b'), 'b'); + }); + + QUnit.test('events are unbound on remove', function(assert) { + assert.expect(3); + var counter = 0; + var dj = new Backbone.Model(); + var emcees = new Backbone.Collection([dj]); + emcees.on('change', function(){ counter++; }); + dj.set({name: 'Kool'}); + assert.equal(counter, 1); + emcees.reset([]); + assert.equal(dj.collection, undefined); + dj.set({name: 'Shadow'}); + assert.equal(counter, 1); + }); + + QUnit.test('remove in multiple collections', function(assert) { + assert.expect(7); + var modelData = { + id: 5, + title: 'Othello' + }; + var passed = false; + var m1 = new Backbone.Model(modelData); + var m2 = new Backbone.Model(modelData); + m2.on('remove', function() { + passed = true; + }); + var col1 = new Backbone.Collection([m1]); + var col2 = new Backbone.Collection([m2]); + assert.notEqual(m1, m2); + assert.ok(col1.length === 1); + assert.ok(col2.length === 1); + col1.remove(m1); + assert.equal(passed, false); + assert.ok(col1.length === 0); + col2.remove(m1); + assert.ok(col2.length === 0); + assert.equal(passed, true); + }); + + QUnit.test('remove same model in multiple collection', function(assert) { + assert.expect(16); + var counter = 0; + var m = new Backbone.Model({id: 5, title: 'Othello'}); + m.on('remove', function(model, collection) { + counter++; + assert.equal(m, model); + if (counter > 1) { + assert.equal(collection, col1); + } else { + assert.equal(collection, col2); + } + }); + var col1 = new Backbone.Collection([m]); + col1.on('remove', function(model, collection) { + assert.equal(m, model); + assert.equal(col1, collection); + }); + var col2 = new Backbone.Collection([m]); + col2.on('remove', function(model, collection) { + assert.equal(m, model); + assert.equal(col2, collection); + }); + assert.equal(col1, m.collection); + col2.remove(m); + assert.ok(col2.length === 0); + assert.ok(col1.length === 1); + assert.equal(counter, 1); + assert.equal(col1, m.collection); + col1.remove(m); + assert.equal(null, m.collection); + assert.ok(col1.length === 0); + assert.equal(counter, 2); + }); + + QUnit.test('model destroy removes from all collections', function(assert) { + assert.expect(3); + var m = new Backbone.Model({id: 5, title: 'Othello'}); + m.sync = function(method, model, options) { options.success(); }; + var col1 = new Backbone.Collection([m]); + var col2 = new Backbone.Collection([m]); + m.destroy(); + assert.ok(col1.length === 0); + assert.ok(col2.length === 0); + assert.equal(undefined, m.collection); + }); + + QUnit.test('Collection: non-persisted model destroy removes from all collections', function(assert) { + assert.expect(3); + var m = new Backbone.Model({title: 'Othello'}); + m.sync = function(method, model, options) { throw 'should not be called'; }; + var col1 = new Backbone.Collection([m]); + var col2 = new Backbone.Collection([m]); + m.destroy(); + assert.ok(col1.length === 0); + assert.ok(col2.length === 0); + assert.equal(undefined, m.collection); + }); + + QUnit.test('fetch', function(assert) { + assert.expect(4); + var collection = new Backbone.Collection; + collection.url = '/test'; + collection.fetch(); + assert.equal(this.syncArgs.method, 'read'); + assert.equal(this.syncArgs.model, collection); + assert.equal(this.syncArgs.options.parse, true); + + collection.fetch({parse: false}); + assert.equal(this.syncArgs.options.parse, false); + }); + + QUnit.test('fetch with an error response triggers an error event', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection(); + collection.on('error', function() { + assert.ok(true); + }); + collection.sync = function(method, model, options) { options.error(); }; + collection.fetch(); + }); + + QUnit.test('#3283 - fetch with an error response calls error with context', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection(); + var obj = {}; + var options = { + context: obj, + error: function() { + assert.equal(this, obj); + } + }; + collection.sync = function(method, model, opts) { + opts.error.call(opts.context); + }; + collection.fetch(options); + }); + + QUnit.test('ensure fetch only parses once', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection; + var counter = 0; + collection.parse = function(models) { + counter++; + return models; + }; + collection.url = '/test'; + collection.fetch(); + this.syncArgs.options.success([]); + assert.equal(counter, 1); + }); + + QUnit.test('create', function(assert) { + assert.expect(4); + var collection = new Backbone.Collection; + collection.url = '/test'; + var model = collection.create({label: 'f'}, {wait: true}); + assert.equal(this.syncArgs.method, 'create'); + assert.equal(this.syncArgs.model, model); + assert.equal(model.get('label'), 'f'); + assert.equal(model.collection, collection); + }); + + QUnit.test('create with validate:true enforces validation', function(assert) { + assert.expect(3); + var ValidatingModel = Backbone.Model.extend({ + validate: function(attrs) { + return 'fail'; + } + }); + var ValidatingCollection = Backbone.Collection.extend({ + model: ValidatingModel + }); + var collection = new ValidatingCollection(); + collection.on('invalid', function(coll, error, options) { + assert.equal(error, 'fail'); + assert.equal(options.validationError, 'fail'); + }); + assert.equal(collection.create({'foo': 'bar'}, {validate: true}), false); + }); + + QUnit.test('create will pass extra options to success callback', function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + sync: function(method, model, options) { + _.extend(options, {specialSync: true}); + return Backbone.Model.prototype.sync.call(this, method, model, options); + } + }); + + var Collection = Backbone.Collection.extend({ + model: Model, + url: '/test' + }); + + var collection = new Collection; + + var success = function(model, response, options) { + assert.ok(options.specialSync, 'Options were passed correctly to callback'); + }; + + collection.create({}, {success: success}); + this.ajaxSettings.success(); + }); + + QUnit.test('create with wait:true should not call collection.parse', function(assert) { + assert.expect(0); + var Collection = Backbone.Collection.extend({ + url: '/test', + parse: function() { + assert.ok(false); + } + }); + + var collection = new Collection; + + collection.create({}, {wait: true}); + this.ajaxSettings.success(); + }); + + QUnit.test('a failing create returns model with errors', function(assert) { + var ValidatingModel = Backbone.Model.extend({ + validate: function(attrs) { + return 'fail'; + } + }); + var ValidatingCollection = Backbone.Collection.extend({ + model: ValidatingModel + }); + var collection = new ValidatingCollection(); + var m = collection.create({foo: 'bar'}); + assert.equal(m.validationError, 'fail'); + assert.equal(collection.length, 1); + }); + + QUnit.test('initialize', function(assert) { + assert.expect(1); + var Collection = Backbone.Collection.extend({ + initialize: function() { + this.one = 1; + } + }); + var coll = new Collection; + assert.equal(coll.one, 1); + }); + + QUnit.test('toJSON', function(assert) { + assert.expect(1); + assert.equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]'); + }); + + QUnit.test('where and findWhere', function(assert) { + assert.expect(8); + var model = new Backbone.Model({a: 1}); + var coll = new Backbone.Collection([ + model, + {a: 1}, + {a: 1, b: 2}, + {a: 2, b: 2}, + {a: 3} + ]); + assert.equal(coll.where({a: 1}).length, 3); + assert.equal(coll.where({a: 2}).length, 1); + assert.equal(coll.where({a: 3}).length, 1); + assert.equal(coll.where({b: 1}).length, 0); + assert.equal(coll.where({b: 2}).length, 2); + assert.equal(coll.where({a: 1, b: 2}).length, 1); + assert.equal(coll.findWhere({a: 1}), model); + assert.equal(coll.findWhere({a: 4}), void 0); + }); + + QUnit.test('Underscore methods', function(assert) { + assert.expect(21); + assert.equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d'); + assert.equal(col.some(function(model){ return model.id === 100; }), false); + assert.equal(col.some(function(model){ return model.id === 0; }), true); + assert.equal(col.reduce(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).id, 3); + assert.equal(col.reduceRight(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).id, 3); + assert.equal(col.indexOf(b), 1); + assert.equal(col.size(), 4); + assert.equal(col.rest().length, 3); + assert.ok(!_.includes(col.rest(), a)); + assert.ok(_.includes(col.rest(), d)); + assert.ok(!col.isEmpty()); + assert.ok(!_.includes(col.without(d), d)); + + var wrapped = col.chain(); + assert.equal(wrapped.map('id').max().value(), 3); + assert.equal(wrapped.map('id').min().value(), 0); + assert.deepEqual(wrapped + .filter(function(o){ return o.id % 2 === 0; }) + .map(function(o){ return o.id * 2; }) + .value(), + [4, 0]); + assert.deepEqual(col.difference([c, d]), [a, b]); + assert.ok(col.includes(col.sample())); + + var first = col.first(); + assert.deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]); + assert.deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1}); + assert.deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at(3)); + assert.ok(col.indexBy('id')[first.id] === first); + }); + + QUnit.test('Underscore methods with object-style and property-style iteratee', function(assert) { + assert.expect(26); + var model = new Backbone.Model({a: 4, b: 1, e: 3}); + var coll = new Backbone.Collection([ + {a: 1, b: 1}, + {a: 2, b: 1, c: 1}, + {a: 3, b: 1}, + model + ]); + assert.equal(coll.find({a: 0}), undefined); + assert.deepEqual(coll.find({a: 4}), model); + assert.equal(coll.find('d'), undefined); + assert.deepEqual(coll.find('e'), model); + assert.equal(coll.filter({a: 0}), false); + assert.deepEqual(coll.filter({a: 4}), [model]); + assert.equal(coll.some({a: 0}), false); + assert.equal(coll.some({a: 1}), true); + assert.equal(coll.reject({a: 0}).length, 4); + assert.deepEqual(coll.reject({a: 4}), _.without(coll.models, model)); + assert.equal(coll.every({a: 0}), false); + assert.equal(coll.every({b: 1}), true); + assert.deepEqual(coll.partition({a: 0})[0], []); + assert.deepEqual(coll.partition({a: 0})[1], coll.models); + assert.deepEqual(coll.partition({a: 4})[0], [model]); + assert.deepEqual(coll.partition({a: 4})[1], _.without(coll.models, model)); + assert.deepEqual(coll.map({a: 2}), [false, true, false, false]); + assert.deepEqual(coll.map('a'), [1, 2, 3, 4]); + assert.deepEqual(coll.sortBy('a')[3], model); + assert.deepEqual(coll.sortBy('e')[0], model); + assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1}); + assert.deepEqual(coll.countBy('d'), {'undefined': 4}); + assert.equal(coll.findIndex({b: 1}), 0); + assert.equal(coll.findIndex({b: 9}), -1); + assert.equal(coll.findLastIndex({b: 1}), 3); + assert.equal(coll.findLastIndex({b: 9}), -1); + }); + + QUnit.test('reset', function(assert) { + assert.expect(16); + + var resetCount = 0; + var models = col.models; + col.on('reset', function() { resetCount += 1; }); + col.reset([]); + assert.equal(resetCount, 1); + assert.equal(col.length, 0); + assert.equal(col.last(), null); + col.reset(models); + assert.equal(resetCount, 2); + assert.equal(col.length, 4); + assert.equal(col.last(), d); + col.reset(_.map(models, function(m){ return m.attributes; })); + assert.equal(resetCount, 3); + assert.equal(col.length, 4); + assert.ok(col.last() !== d); + assert.ok(_.isEqual(col.last().attributes, d.attributes)); + col.reset(); + assert.equal(col.length, 0); + assert.equal(resetCount, 4); + + var f = new Backbone.Model({id: 20, label: 'f'}); + col.reset([undefined, f]); + assert.equal(col.length, 2); + assert.equal(resetCount, 5); + + col.reset(new Array(4)); + assert.equal(col.length, 4); + assert.equal(resetCount, 6); + }); + + QUnit.test('reset with different values', function(assert) { + var collection = new Backbone.Collection({id: 1}); + collection.reset({id: 1, a: 1}); + assert.equal(collection.get(1).get('a'), 1); + }); + + QUnit.test('same references in reset', function(assert) { + var model = new Backbone.Model({id: 1}); + var collection = new Backbone.Collection({id: 1}); + collection.reset(model); + assert.equal(collection.get(1), model); + }); + + QUnit.test('reset passes caller options', function(assert) { + assert.expect(3); + var Model = Backbone.Model.extend({ + initialize: function(attrs, options) { + this.modelParameter = options.modelParameter; + } + }); + var collection = new (Backbone.Collection.extend({model: Model}))(); + collection.reset([{astring: 'green', anumber: 1}, {astring: 'blue', anumber: 2}], {modelParameter: 'model parameter'}); + assert.equal(collection.length, 2); + collection.each(function(model) { + assert.equal(model.modelParameter, 'model parameter'); + }); + }); + + QUnit.test('reset does not alter options by reference', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection([{id: 1}]); + var origOpts = {}; + collection.on('reset', function(coll, opts){ + assert.equal(origOpts.previousModels, undefined); + assert.equal(opts.previousModels[0].id, 1); + }); + collection.reset([], origOpts); + }); + + QUnit.test('trigger custom events on models', function(assert) { + assert.expect(1); + var fired = null; + a.on('custom', function() { fired = true; }); + a.trigger('custom'); + assert.equal(fired, true); + }); + + QUnit.test('add does not alter arguments', function(assert) { + assert.expect(2); + var attrs = {}; + var models = [attrs]; + new Backbone.Collection().add(models); + assert.equal(models.length, 1); + assert.ok(attrs === models[0]); + }); + + QUnit.test('#714: access `model.collection` in a brand new model.', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection; + collection.url = '/test'; + var Model = Backbone.Model.extend({ + set: function(attrs) { + assert.equal(attrs.prop, 'value'); + assert.equal(this.collection, collection); + return this; + } + }); + collection.model = Model; + collection.create({prop: 'value'}); + }); + + QUnit.test('#574, remove its own reference to the .models array.', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection([ + {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6} + ]); + assert.equal(collection.length, 6); + collection.remove(collection.models); + assert.equal(collection.length, 0); + }); + + QUnit.test('#861, adding models to a collection which do not pass validation, with validate:true', function(assert) { + assert.expect(2); + var Model = Backbone.Model.extend({ + validate: function(attrs) { + if (attrs.id === 3) return "id can't be 3"; + } + }); + + var Collection = Backbone.Collection.extend({ + model: Model + }); + + var collection = new Collection; + collection.on('invalid', function() { assert.ok(true); }); + + collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {validate: true}); + assert.deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]); + }); + + QUnit.test('Invalid models are discarded with validate:true.', function(assert) { + assert.expect(5); + var collection = new Backbone.Collection; + collection.on('test', function() { assert.ok(true); }); + collection.model = Backbone.Model.extend({ + validate: function(attrs){ if (!attrs.valid) return 'invalid'; } + }); + var model = new collection.model({id: 1, valid: true}); + collection.add([model, {id: 2}], {validate: true}); + model.trigger('test'); + assert.ok(collection.get(model.cid)); + assert.ok(collection.get(1)); + assert.ok(!collection.get(2)); + assert.equal(collection.length, 1); + }); + + QUnit.test('multiple copies of the same model', function(assert) { + assert.expect(3); + var collection = new Backbone.Collection(); + var model = new Backbone.Model(); + collection.add([model, model]); + assert.equal(collection.length, 1); + collection.add([{id: 1}, {id: 1}]); + assert.equal(collection.length, 2); + assert.equal(collection.last().id, 1); + }); + + QUnit.test('#964 - collection.get return inconsistent', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection(); + assert.ok(collection.get(null) === undefined); + assert.ok(collection.get() === undefined); + }); + + QUnit.test('#1112 - passing options.model sets collection.model', function(assert) { + assert.expect(2); + var Model = Backbone.Model.extend({}); + var collection = new Backbone.Collection([{id: 1}], {model: Model}); + assert.ok(collection.model === Model); + assert.ok(collection.at(0) instanceof Model); + }); + + QUnit.test('null and undefined are invalid ids.', function(assert) { + assert.expect(2); + var model = new Backbone.Model({id: 1}); + var collection = new Backbone.Collection([model]); + model.set({id: null}); + assert.ok(!collection.get('null')); + model.set({id: 1}); + model.set({id: undefined}); + assert.ok(!collection.get('undefined')); + }); + + QUnit.test('falsy comparator', function(assert) { + assert.expect(4); + var Col = Backbone.Collection.extend({ + comparator: function(model){ return model.id; } + }); + var collection = new Col(); + var colFalse = new Col(null, {comparator: false}); + var colNull = new Col(null, {comparator: null}); + var colUndefined = new Col(null, {comparator: undefined}); + assert.ok(collection.comparator); + assert.ok(!colFalse.comparator); + assert.ok(!colNull.comparator); + assert.ok(colUndefined.comparator); + }); + + QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) { + assert.expect(2); + var m = new Backbone.Model({x: 1}); + var collection = new Backbone.Collection(); + var opts = { + opts: true, + success: function(coll, resp, options) { + assert.ok(options.opts); + } + }; + collection.sync = m.sync = function( method, coll, options ){ + options.success({}); + }; + collection.fetch(opts); + collection.create(m, opts); + }); + + QUnit.test("#1412 - Trigger 'request' and 'sync' events.", function(assert) { + assert.expect(4); + var collection = new Backbone.Collection; + collection.url = '/test'; + Backbone.ajax = function(settings){ settings.success(); }; + + collection.on('request', function(obj, xhr, options) { + assert.ok(obj === collection, "collection has correct 'request' event after fetching"); + }); + collection.on('sync', function(obj, response, options) { + assert.ok(obj === collection, "collection has correct 'sync' event after fetching"); + }); + collection.fetch(); + collection.off(); + + collection.on('request', function(obj, xhr, options) { + assert.ok(obj === collection.get(1), "collection has correct 'request' event after one of its models save"); + }); + collection.on('sync', function(obj, response, options) { + assert.ok(obj === collection.get(1), "collection has correct 'sync' event after one of its models save"); + }); + collection.create({id: 1}); + collection.off(); + }); + + QUnit.test('#3283 - fetch, create calls success with context', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection; + collection.url = '/test'; + Backbone.ajax = function(settings) { + settings.success.call(settings.context); + }; + var obj = {}; + var options = { + context: obj, + success: function() { + assert.equal(this, obj); + } + }; + + collection.fetch(options); + collection.create({id: 1}, options); + }); + + QUnit.test('#1447 - create with wait adds model.', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection; + var model = new Backbone.Model; + model.sync = function(method, m, options){ options.success(); }; + collection.on('add', function(){ assert.ok(true); }); + collection.create(model, {wait: true}); + }); + + QUnit.test('#1448 - add sorts collection after merge.', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([ + {id: 1, x: 1}, + {id: 2, x: 2} + ]); + collection.comparator = function(model){ return model.get('x'); }; + collection.add({id: 1, x: 3}, {merge: true}); + assert.deepEqual(collection.pluck('id'), [2, 1]); + }); + + QUnit.test('#1655 - groupBy can be used with a string argument.', function(assert) { + assert.expect(3); + var collection = new Backbone.Collection([{x: 1}, {x: 2}]); + var grouped = collection.groupBy('x'); + assert.strictEqual(_.keys(grouped).length, 2); + assert.strictEqual(grouped[1][0].get('x'), 1); + assert.strictEqual(grouped[2][0].get('x'), 2); + }); + + QUnit.test('#1655 - sortBy can be used with a string argument.', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]); + var values = _.map(collection.sortBy('x'), function(model) { + return model.get('x'); + }); + assert.deepEqual(values, [1, 2, 3]); + }); + + QUnit.test('#1604 - Removal during iteration.', function(assert) { + assert.expect(0); + var collection = new Backbone.Collection([{}, {}]); + collection.on('add', function() { + collection.at(0).destroy(); + }); + collection.add({}, {at: 0}); + }); + + QUnit.test('#1638 - `sort` during `add` triggers correctly.', function(assert) { + var collection = new Backbone.Collection; + collection.comparator = function(model) { return model.get('x'); }; + var added = []; + collection.on('add', function(model) { + model.set({x: 3}); + collection.sort(); + added.push(model.id); + }); + collection.add([{id: 1, x: 1}, {id: 2, x: 2}]); + assert.deepEqual(added, [1, 2]); + }); + + QUnit.test('fetch parses models by default', function(assert) { + assert.expect(1); + var model = {}; + var Collection = Backbone.Collection.extend({ + url: 'test', + model: Backbone.Model.extend({ + parse: function(resp) { + assert.strictEqual(resp, model); + } + }) + }); + new Collection().fetch(); + this.ajaxSettings.success([model]); + }); + + QUnit.test("`sort` shouldn't always fire on `add`", function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], { + comparator: 'id' + }); + collection.sort = function(){ assert.ok(true); }; + collection.add([]); + collection.add({id: 1}); + collection.add([{id: 2}, {id: 3}]); + collection.add({id: 4}); + }); + + QUnit.test('#1407 parse option on constructor parses collection and models', function(assert) { + assert.expect(2); + var model = { + namespace: [{id: 1}, {id: 2}] + }; + var Collection = Backbone.Collection.extend({ + model: Backbone.Model.extend({ + parse: function(m) { + m.name = 'test'; + return m; + } + }), + parse: function(m) { + return m.namespace; + } + }); + var collection = new Collection(model, {parse: true}); + + assert.equal(collection.length, 2); + assert.equal(collection.at(0).get('name'), 'test'); + }); + + QUnit.test('#1407 parse option on reset parses collection and models', function(assert) { + assert.expect(2); + var model = { + namespace: [{id: 1}, {id: 2}] + }; + var Collection = Backbone.Collection.extend({ + model: Backbone.Model.extend({ + parse: function(m) { + m.name = 'test'; + return m; + } + }), + parse: function(m) { + return m.namespace; + } + }); + var collection = new Collection(); + collection.reset(model, {parse: true}); + + assert.equal(collection.length, 2); + assert.equal(collection.at(0).get('name'), 'test'); + }); + + + QUnit.test('Reset includes previous models in triggered event.', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + var collection = new Backbone.Collection([model]); + collection.on('reset', function(coll, options) { + assert.deepEqual(options.previousModels, [model]); + }); + collection.reset([]); + }); + + QUnit.test('set', function(assert) { + var m1 = new Backbone.Model(); + var m2 = new Backbone.Model({id: 2}); + var m3 = new Backbone.Model(); + var collection = new Backbone.Collection([m1, m2]); + + // Test add/change/remove events + collection.on('add', function(model) { + assert.strictEqual(model, m3); + }); + collection.on('change', function(model) { + assert.strictEqual(model, m2); + }); + collection.on('remove', function(model) { + assert.strictEqual(model, m1); + }); + + // remove: false doesn't remove any models + collection.set([], {remove: false}); + assert.strictEqual(collection.length, 2); + + // add: false doesn't add any models + collection.set([m1, m2, m3], {add: false}); + assert.strictEqual(collection.length, 2); + + // merge: false doesn't change any models + collection.set([m1, {id: 2, a: 1}], {merge: false}); + assert.strictEqual(m2.get('a'), void 0); + + // add: false, remove: false only merges existing models + collection.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false}); + assert.strictEqual(collection.length, 2); + assert.strictEqual(m2.get('a'), 0); + + // default options add/remove/merge as appropriate + collection.set([{id: 2, a: 1}, m3]); + assert.strictEqual(collection.length, 2); + assert.strictEqual(m2.get('a'), 1); + + // Test removing models not passing an argument + collection.off('remove').on('remove', function(model) { + assert.ok(model === m2 || model === m3); + }); + collection.set([]); + assert.strictEqual(collection.length, 0); + + // Test null models on set doesn't clear collection + collection.off(); + collection.set([{id: 1}]); + collection.set(); + assert.strictEqual(collection.length, 1); + }); + + QUnit.test('set with only cids', function(assert) { + assert.expect(3); + var m1 = new Backbone.Model; + var m2 = new Backbone.Model; + var collection = new Backbone.Collection; + collection.set([m1, m2]); + assert.equal(collection.length, 2); + collection.set([m1]); + assert.equal(collection.length, 1); + collection.set([m1, m1, m1, m2, m2], {remove: false}); + assert.equal(collection.length, 2); + }); + + QUnit.test('set with only idAttribute', function(assert) { + assert.expect(3); + var m1 = {_id: 1}; + var m2 = {_id: 2}; + var Col = Backbone.Collection.extend({ + model: Backbone.Model.extend({ + idAttribute: '_id' + }) + }); + var collection = new Col; + collection.set([m1, m2]); + assert.equal(collection.length, 2); + collection.set([m1]); + assert.equal(collection.length, 1); + collection.set([m1, m1, m1, m2, m2], {remove: false}); + assert.equal(collection.length, 2); + }); + + QUnit.test('set + merge with default values defined', function(assert) { + var Model = Backbone.Model.extend({ + defaults: { + key: 'value' + } + }); + var m = new Model({id: 1}); + var collection = new Backbone.Collection([m], {model: Model}); + assert.equal(collection.first().get('key'), 'value'); + + collection.set({id: 1, key: 'other'}); + assert.equal(collection.first().get('key'), 'other'); + + collection.set({id: 1, other: 'value'}); + assert.equal(collection.first().get('key'), 'other'); + assert.equal(collection.length, 1); + }); + + QUnit.test('merge without mutation', function(assert) { + var Model = Backbone.Model.extend({ + initialize: function(attrs, options) { + if (attrs.child) { + this.set('child', new Model(attrs.child, options), options); + } + } + }); + var Collection = Backbone.Collection.extend({model: Model}); + var data = [{id: 1, child: {id: 2}}]; + var collection = new Collection(data); + assert.equal(collection.first().id, 1); + collection.set(data); + assert.equal(collection.first().id, 1); + collection.set([{id: 2, child: {id: 2}}].concat(data)); + assert.deepEqual(collection.pluck('id'), [2, 1]); + }); + + QUnit.test('`set` and model level `parse`', function(assert) { + var Model = Backbone.Model.extend({}); + var Collection = Backbone.Collection.extend({ + model: Model, + parse: function(res) { return _.map(res.models, 'model'); } + }); + var model = new Model({id: 1}); + var collection = new Collection(model); + collection.set({models: [ + {model: {id: 1}}, + {model: {id: 2}} + ]}, {parse: true}); + assert.equal(collection.first(), model); + }); + + QUnit.test('`set` data is only parsed once', function(assert) { + var collection = new Backbone.Collection(); + collection.model = Backbone.Model.extend({ + parse: function(data) { + assert.equal(data.parsed, void 0); + data.parsed = true; + return data; + } + }); + collection.set({}, {parse: true}); + }); + + QUnit.test('`set` matches input order in the absence of a comparator', function(assert) { + var one = new Backbone.Model({id: 1}); + var two = new Backbone.Model({id: 2}); + var three = new Backbone.Model({id: 3}); + var collection = new Backbone.Collection([one, two, three]); + collection.set([{id: 3}, {id: 2}, {id: 1}]); + assert.deepEqual(collection.models, [three, two, one]); + collection.set([{id: 1}, {id: 2}]); + assert.deepEqual(collection.models, [one, two]); + collection.set([two, three, one]); + assert.deepEqual(collection.models, [two, three, one]); + collection.set([{id: 1}, {id: 2}], {remove: false}); + assert.deepEqual(collection.models, [two, three, one]); + collection.set([{id: 1}, {id: 2}, {id: 3}], {merge: false}); + assert.deepEqual(collection.models, [one, two, three]); + collection.set([three, two, one, {id: 4}], {add: false}); + assert.deepEqual(collection.models, [one, two, three]); + }); + + QUnit.test('#1894 - Push should not trigger a sort', function(assert) { + assert.expect(0); + var Collection = Backbone.Collection.extend({ + comparator: 'id', + sort: function() { assert.ok(false); } + }); + new Collection().push({id: 1}); + }); + + QUnit.test('#2428 - push duplicate models, return the correct one', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection; + var model1 = collection.push({id: 101}); + var model2 = collection.push({id: 101}); + assert.ok(model2.cid === model1.cid); + }); + + QUnit.test('`set` with non-normal id', function(assert) { + var Collection = Backbone.Collection.extend({ + model: Backbone.Model.extend({idAttribute: '_id'}) + }); + var collection = new Collection({_id: 1}); + collection.set([{_id: 1, a: 1}], {add: false}); + assert.equal(collection.first().get('a'), 1); + }); + + QUnit.test('#1894 - `sort` can optionally be turned off', function(assert) { + assert.expect(0); + var Collection = Backbone.Collection.extend({ + comparator: 'id', + sort: function() { assert.ok(false); } + }); + new Collection().add({id: 1}, {sort: false}); + }); + + QUnit.test('#1915 - `parse` data in the right order in `set`', function(assert) { + var collection = new (Backbone.Collection.extend({ + parse: function(data) { + assert.strictEqual(data.status, 'ok'); + return data.data; + } + })); + var res = {status: 'ok', data: [{id: 1}]}; + collection.set(res, {parse: true}); + }); + + QUnit.test('#1939 - `parse` is passed `options`', function(assert) { + var done = assert.async(); + assert.expect(1); + var collection = new (Backbone.Collection.extend({ + url: '/', + parse: function(data, options) { + assert.strictEqual(options.xhr.someHeader, 'headerValue'); + return data; + } + })); + var ajax = Backbone.ajax; + Backbone.ajax = function(params) { + _.defer(params.success, []); + return {someHeader: 'headerValue'}; + }; + collection.fetch({ + success: function() { done(); } + }); + Backbone.ajax = ajax; + }); + + QUnit.test('fetch will pass extra options to success callback', function(assert) { + assert.expect(1); + var SpecialSyncCollection = Backbone.Collection.extend({ + url: '/test', + sync: function(method, collection, options) { + _.extend(options, {specialSync: true}); + return Backbone.Collection.prototype.sync.call(this, method, collection, options); + } + }); + + var collection = new SpecialSyncCollection(); + + var onSuccess = function(coll, resp, options) { + assert.ok(options.specialSync, 'Options were passed correctly to callback'); + }; + + collection.fetch({success: onSuccess}); + this.ajaxSettings.success(); + }); + + QUnit.test('`add` only `sort`s when necessary', function(assert) { + assert.expect(2); + var collection = new (Backbone.Collection.extend({ + comparator: 'a' + }))([{id: 1}, {id: 2}, {id: 3}]); + collection.on('sort', function() { assert.ok(true); }); + collection.add({id: 4}); // do sort, new model + collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change + collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator change + collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator change + collection.add(collection.models); // don't sort, nothing new + collection.add(collection.models, {merge: true}); // don't sort + }); + + QUnit.test('`add` only `sort`s when necessary with comparator function', function(assert) { + assert.expect(3); + var collection = new (Backbone.Collection.extend({ + comparator: function(m1, m2) { + return m1.get('a') > m2.get('a') ? 1 : (m1.get('a') < m2.get('a') ? -1 : 0); + } + }))([{id: 1}, {id: 2}, {id: 3}]); + collection.on('sort', function() { assert.ok(true); }); + collection.add({id: 4}); // do sort, new model + collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change + collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change + collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change + collection.add(collection.models); // don't sort, nothing new + collection.add(collection.models, {merge: true}); // don't sort + }); + + QUnit.test('Attach options to collection.', function(assert) { + assert.expect(2); + var Model = Backbone.Model; + var comparator = function(){}; + + var collection = new Backbone.Collection([], { + model: Model, + comparator: comparator + }); + + assert.ok(collection.model === Model); + assert.ok(collection.comparator === comparator); + }); + + QUnit.test('Pass falsey for `models` for empty Col with `options`', function(assert) { + assert.expect(9); + var opts = {a: 1, b: 2}; + _.forEach([undefined, null, false], function(falsey) { + var Collection = Backbone.Collection.extend({ + initialize: function(models, options) { + assert.strictEqual(models, falsey); + assert.strictEqual(options, opts); + } + }); + + var collection = new Collection(falsey, opts); + assert.strictEqual(collection.length, 0); + }); + }); + + QUnit.test('`add` overrides `set` flags', function(assert) { + var collection = new Backbone.Collection(); + collection.once('add', function(model, coll, options) { + coll.add({id: 2}, options); + }); + collection.set({id: 1}); + assert.equal(collection.length, 2); + }); + + QUnit.test('#2606 - Collection#create, success arguments', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection; + collection.url = 'test'; + collection.create({}, { + success: function(model, resp, options) { + assert.strictEqual(resp, 'response'); + } + }); + this.ajaxSettings.success('response'); + }); + + QUnit.test('#2612 - nested `parse` works with `Collection#set`', function(assert) { + + var Job = Backbone.Model.extend({ + constructor: function() { + this.items = new Items(); + Backbone.Model.apply(this, arguments); + }, + parse: function(attrs) { + this.items.set(attrs.items, {parse: true}); + return _.omit(attrs, 'items'); + } + }); + + var Item = Backbone.Model.extend({ + constructor: function() { + this.subItems = new Backbone.Collection(); + Backbone.Model.apply(this, arguments); + }, + parse: function(attrs) { + this.subItems.set(attrs.subItems, {parse: true}); + return _.omit(attrs, 'subItems'); + } + }); + + var Items = Backbone.Collection.extend({ + model: Item + }); + + var data = { + name: 'JobName', + id: 1, + items: [{ + id: 1, + name: 'Sub1', + subItems: [ + {id: 1, subName: 'One'}, + {id: 2, subName: 'Two'} + ] + }, { + id: 2, + name: 'Sub2', + subItems: [ + {id: 3, subName: 'Three'}, + {id: 4, subName: 'Four'} + ] + }] + }; + + var newData = { + name: 'NewJobName', + id: 1, + items: [{ + id: 1, + name: 'NewSub1', + subItems: [ + {id: 1, subName: 'NewOne'}, + {id: 2, subName: 'NewTwo'} + ] + }, { + id: 2, + name: 'NewSub2', + subItems: [ + {id: 3, subName: 'NewThree'}, + {id: 4, subName: 'NewFour'} + ] + }] + }; + + var job = new Job(data, {parse: true}); + assert.equal(job.get('name'), 'JobName'); + assert.equal(job.items.at(0).get('name'), 'Sub1'); + assert.equal(job.items.length, 2); + assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'One'); + assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'Three'); + job.set(job.parse(newData, {parse: true})); + assert.equal(job.get('name'), 'NewJobName'); + assert.equal(job.items.at(0).get('name'), 'NewSub1'); + assert.equal(job.items.length, 2); + assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne'); + assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree'); + }); + + QUnit.test('_addReference binds all collection events & adds to the lookup hashes', function(assert) { + assert.expect(8); + + var calls = {add: 0, remove: 0}; + + var Collection = Backbone.Collection.extend({ + + _addReference: function(model) { + Backbone.Collection.prototype._addReference.apply(this, arguments); + calls.add++; + assert.equal(model, this._byId[model.id]); + assert.equal(model, this._byId[model.cid]); + assert.equal(model._events.all.length, 1); + }, + + _removeReference: function(model) { + Backbone.Collection.prototype._removeReference.apply(this, arguments); + calls.remove++; + assert.equal(this._byId[model.id], void 0); + assert.equal(this._byId[model.cid], void 0); + assert.equal(model.collection, void 0); + } + + }); + + var collection = new Collection(); + var model = collection.add({id: 1}); + collection.remove(model); + + assert.equal(calls.add, 1); + assert.equal(calls.remove, 1); + }); + + QUnit.test('Do not allow duplicate models to be `add`ed or `set`', function(assert) { + var collection = new Backbone.Collection(); + + collection.add([{id: 1}, {id: 1}]); + assert.equal(collection.length, 1); + assert.equal(collection.models.length, 1); + + collection.set([{id: 1}, {id: 1}]); + assert.equal(collection.length, 1); + assert.equal(collection.models.length, 1); + }); + + QUnit.test('#3020: #set with {add: false} should not throw.', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection; + collection.set([{id: 1}], {add: false}); + assert.strictEqual(collection.length, 0); + assert.strictEqual(collection.models.length, 0); + }); + + QUnit.test('create with wait, model instance, #3028', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection(); + var model = new Backbone.Model({id: 1}); + model.sync = function(){ + assert.equal(this.collection, collection); + }; + collection.create(model, {wait: true}); + }); + + QUnit.test('modelId', function(assert) { + var Stooge = Backbone.Model.extend(); + var StoogeCollection = Backbone.Collection.extend({model: Stooge}); + + // Default to using `Collection::model::idAttribute`. + assert.equal(StoogeCollection.prototype.modelId({id: 1}), 1); + Stooge.prototype.idAttribute = '_id'; + assert.equal(StoogeCollection.prototype.modelId({_id: 1}), 1); + }); + + QUnit.test('Polymorphic models work with "simple" constructors', function(assert) { + var A = Backbone.Model.extend(); + var B = Backbone.Model.extend(); + var C = Backbone.Collection.extend({ + model: function(attrs) { + return attrs.type === 'a' ? new A(attrs) : new B(attrs); + } + }); + var collection = new C([{id: 1, type: 'a'}, {id: 2, type: 'b'}]); + assert.equal(collection.length, 2); + assert.ok(collection.at(0) instanceof A); + assert.equal(collection.at(0).id, 1); + assert.ok(collection.at(1) instanceof B); + assert.equal(collection.at(1).id, 2); + }); + + QUnit.test('Polymorphic models work with "advanced" constructors', function(assert) { + var A = Backbone.Model.extend({idAttribute: '_id'}); + var B = Backbone.Model.extend({idAttribute: '_id'}); + var C = Backbone.Collection.extend({ + model: Backbone.Model.extend({ + constructor: function(attrs) { + return attrs.type === 'a' ? new A(attrs) : new B(attrs); + }, + + idAttribute: '_id' + }) + }); + var collection = new C([{_id: 1, type: 'a'}, {_id: 2, type: 'b'}]); + assert.equal(collection.length, 2); + assert.ok(collection.at(0) instanceof A); + assert.equal(collection.at(0), collection.get(1)); + assert.ok(collection.at(1) instanceof B); + assert.equal(collection.at(1), collection.get(2)); + + C = Backbone.Collection.extend({ + model: function(attrs) { + return attrs.type === 'a' ? new A(attrs) : new B(attrs); + }, + + modelId: function(attrs) { + return attrs.type + '-' + attrs.id; + } + }); + collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]); + assert.equal(collection.length, 2); + assert.ok(collection.at(0) instanceof A); + assert.equal(collection.at(0), collection.get('a-1')); + assert.ok(collection.at(1) instanceof B); + assert.equal(collection.at(1), collection.get('b-1')); + }); + + QUnit.test('Collection with polymorphic models receives default id from modelId', function(assert) { + assert.expect(6); + // When the polymorphic models use 'id' for the idAttribute, all is fine. + var C1 = Backbone.Collection.extend({ + model: function(attrs) { + return new Backbone.Model(attrs); + } + }); + var c1 = new C1({id: 1}); + assert.equal(c1.get(1).id, 1); + assert.equal(c1.modelId({id: 1}), 1); + + // If the polymorphic models define their own idAttribute, + // the modelId method should be overridden, for the reason below. + var M = Backbone.Model.extend({ + idAttribute: '_id' + }); + var C2 = Backbone.Collection.extend({ + model: function(attrs) { + return new M(attrs); + } + }); + var c2 = new C2({'_id': 1}); + assert.equal(c2.get(1), void 0); + assert.equal(c2.modelId(c2.at(0).attributes), void 0); + var m = new M({'_id': 2}); + c2.add(m); + assert.equal(c2.get(2), void 0); + assert.equal(c2.modelId(m.attributes), void 0); + }); + + QUnit.test('#3039 #3951: adding at index fires with correct at', function(assert) { + assert.expect(4); + var collection = new Backbone.Collection([{val: 0}, {val: 4}]); + collection.on('add', function(model, coll, options) { + assert.equal(model.get('val'), options.index); + }); + collection.add([{val: 1}, {val: 2}, {val: 3}], {at: 1}); + collection.add({val: 5}, {at: 10}); + }); + + QUnit.test('#3039: index is not sent when at is not specified', function(assert) { + assert.expect(2); + var collection = new Backbone.Collection([{at: 0}]); + collection.on('add', function(model, coll, options) { + assert.equal(undefined, options.index); + }); + collection.add([{at: 1}, {at: 2}]); + }); + + QUnit.test('#3199 - Order changing should trigger a sort', function(assert) { + assert.expect(1); + var one = new Backbone.Model({id: 1}); + var two = new Backbone.Model({id: 2}); + var three = new Backbone.Model({id: 3}); + var collection = new Backbone.Collection([one, two, three]); + collection.on('sort', function() { + assert.ok(true); + }); + collection.set([{id: 3}, {id: 2}, {id: 1}]); + }); + + QUnit.test('#3199 - Adding a model should trigger a sort', function(assert) { + assert.expect(1); + var one = new Backbone.Model({id: 1}); + var two = new Backbone.Model({id: 2}); + var three = new Backbone.Model({id: 3}); + var collection = new Backbone.Collection([one, two, three]); + collection.on('sort', function() { + assert.ok(true); + }); + collection.set([{id: 1}, {id: 2}, {id: 3}, {id: 0}]); + }); + + QUnit.test('#3199 - Order not changing should not trigger a sort', function(assert) { + assert.expect(0); + var one = new Backbone.Model({id: 1}); + var two = new Backbone.Model({id: 2}); + var three = new Backbone.Model({id: 3}); + var collection = new Backbone.Collection([one, two, three]); + collection.on('sort', function() { + assert.ok(false); + }); + collection.set([{id: 1}, {id: 2}, {id: 3}]); + }); + + QUnit.test('add supports negative indexes', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 1}]); + collection.add([{id: 2}, {id: 3}], {at: -1}); + collection.add([{id: 2.5}], {at: -2}); + collection.add([{id: 0.5}], {at: -6}); + assert.equal(collection.pluck('id').join(','), '0.5,1,2,2.5,3'); + }); + + QUnit.test('#set accepts options.at as a string', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 1}, {id: 2}]); + collection.add([{id: 3}], {at: '1'}); + assert.deepEqual(collection.pluck('id'), [1, 3, 2]); + }); + + QUnit.test('adding multiple models triggers `update` event once', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection; + collection.on('update', function() { assert.ok(true); }); + collection.add([{id: 1}, {id: 2}, {id: 3}]); + }); + + QUnit.test('removing models triggers `update` event once', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]); + collection.on('update', function() { assert.ok(true); }); + collection.remove([{id: 1}, {id: 2}]); + }); + + QUnit.test('remove does not trigger `update` when nothing removed', function(assert) { + assert.expect(0); + var collection = new Backbone.Collection([{id: 1}, {id: 2}]); + collection.on('update', function() { assert.ok(false); }); + collection.remove([{id: 3}]); + }); + + QUnit.test('set triggers `set` event once', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 1}, {id: 2}]); + collection.on('update', function() { assert.ok(true); }); + collection.set([{id: 1}, {id: 3}]); + }); + + QUnit.test('set does not trigger `update` event when nothing added nor removed', function(assert) { + var collection = new Backbone.Collection([{id: 1}, {id: 2}]); + collection.on('update', function(coll, options) { + assert.equal(options.changes.added.length, 0); + assert.equal(options.changes.removed.length, 0); + assert.equal(options.changes.merged.length, 2); + }); + collection.set([{id: 1}, {id: 2}]); + }); + + QUnit.test('#3610 - invoke collects arguments', function(assert) { + assert.expect(3); + var Model = Backbone.Model.extend({ + method: function(x, y, z) { + assert.equal(x, 1); + assert.equal(y, 2); + assert.equal(z, 3); + } + }); + var Collection = Backbone.Collection.extend({ + model: Model + }); + var collection = new Collection([{id: 1}]); + collection.invoke('method', 1, 2, 3); + }); + + QUnit.test('#3662 - triggering change without model will not error', function(assert) { + assert.expect(1); + var collection = new Backbone.Collection([{id: 1}]); + var model = collection.first(); + collection.on('change', function(m) { + assert.equal(m, undefined); + }); + model.trigger('change'); + }); + + QUnit.test('#3871 - falsy parse result creates empty collection', function(assert) { + var collection = new (Backbone.Collection.extend({ + parse: function(data, options) {} + })); + collection.set('', {parse: true}); + assert.equal(collection.length, 0); + }); + + QUnit.test("#3711 - remove's `update` event returns one removed model", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var collection = new Backbone.Collection([model]); + collection.on('update', function(context, options) { + var changed = options.changes; + assert.deepEqual(changed.added, []); + assert.deepEqual(changed.merged, []); + assert.strictEqual(changed.removed[0], model); + }); + collection.remove(model); + }); + + QUnit.test("#3711 - remove's `update` event returns multiple removed models", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); + var collection = new Backbone.Collection([model, model2]); + collection.on('update', function(context, options) { + var changed = options.changes; + assert.deepEqual(changed.added, []); + assert.deepEqual(changed.merged, []); + assert.ok(changed.removed.length === 2); + + assert.ok(_.indexOf(changed.removed, model) > -1 && _.indexOf(changed.removed, model2) > -1); + }); + collection.remove([model, model2]); + }); + + QUnit.test("#3711 - set's `update` event returns one added model", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var collection = new Backbone.Collection(); + collection.on('update', function(context, options) { + var addedModels = options.changes.added; + assert.ok(addedModels.length === 1); + assert.strictEqual(addedModels[0], model); + }); + collection.set(model); + }); + + QUnit.test("#3711 - set's `update` event returns multiple added models", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); + var collection = new Backbone.Collection(); + collection.on('update', function(context, options) { + var addedModels = options.changes.added; + assert.ok(addedModels.length === 2); + assert.strictEqual(addedModels[0], model); + assert.strictEqual(addedModels[1], model2); + }); + collection.set([model, model2]); + }); + + QUnit.test("#3711 - set's `update` event returns one removed model", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); + var model3 = new Backbone.Model({id: 3, title: 'My Last Post'}); + var collection = new Backbone.Collection([model]); + collection.on('update', function(context, options) { + var changed = options.changes; + assert.equal(changed.added.length, 2); + assert.equal(changed.merged.length, 0); + assert.ok(changed.removed.length === 1); + assert.strictEqual(changed.removed[0], model); + }); + collection.set([model2, model3]); + }); + + QUnit.test("#3711 - set's `update` event returns multiple removed models", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); + var model3 = new Backbone.Model({id: 3, title: 'My Last Post'}); + var collection = new Backbone.Collection([model, model2]); + collection.on('update', function(context, options) { + var removedModels = options.changes.removed; + assert.ok(removedModels.length === 2); + assert.strictEqual(removedModels[0], model); + assert.strictEqual(removedModels[1], model2); + }); + collection.set([model3]); + }); + + QUnit.test("#3711 - set's `update` event returns one merged model", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); + var model2Update = new Backbone.Model({id: 2, title: 'Second Post V2'}); + var collection = new Backbone.Collection([model, model2]); + collection.on('update', function(context, options) { + var mergedModels = options.changes.merged; + assert.ok(mergedModels.length === 1); + assert.strictEqual(mergedModels[0].get('title'), model2Update.get('title')); + }); + collection.set([model2Update]); + }); + + QUnit.test("#3711 - set's `update` event returns multiple merged models", function(assert) { + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var modelUpdate = new Backbone.Model({id: 1, title: 'First Post V2'}); + var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); + var model2Update = new Backbone.Model({id: 2, title: 'Second Post V2'}); + var collection = new Backbone.Collection([model, model2]); + collection.on('update', function(context, options) { + var mergedModels = options.changes.merged; + assert.ok(mergedModels.length === 2); + assert.strictEqual(mergedModels[0].get('title'), model2Update.get('title')); + assert.strictEqual(mergedModels[1].get('title'), modelUpdate.get('title')); + }); + collection.set([model2Update, modelUpdate]); + }); + + QUnit.test("#3711 - set's `update` event should not be triggered adding a model which already exists exactly alike", function(assert) { + var fired = false; + var model = new Backbone.Model({id: 1, title: 'First Post'}); + var collection = new Backbone.Collection([model]); + collection.on('update', function(context, options) { + fired = true; + }); + collection.set([model]); + assert.equal(fired, false); + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/events.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/events.js new file mode 100644 index 00000000..544b39a1 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/events.js @@ -0,0 +1,706 @@ +(function() { + + QUnit.module('Backbone.Events'); + + QUnit.test('on and trigger', function(assert) { + assert.expect(2); + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + obj.on('event', function() { obj.counter += 1; }); + obj.trigger('event'); + assert.equal(obj.counter, 1, 'counter should be incremented.'); + obj.trigger('event'); + obj.trigger('event'); + obj.trigger('event'); + obj.trigger('event'); + assert.equal(obj.counter, 5, 'counter should be incremented five times.'); + }); + + QUnit.test('binding and triggering multiple events', function(assert) { + assert.expect(4); + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + + obj.on('a b c', function() { obj.counter += 1; }); + + obj.trigger('a'); + assert.equal(obj.counter, 1); + + obj.trigger('a b'); + assert.equal(obj.counter, 3); + + obj.trigger('c'); + assert.equal(obj.counter, 4); + + obj.off('a c'); + obj.trigger('a b c'); + assert.equal(obj.counter, 5); + }); + + QUnit.test('binding and triggering with event maps', function(assert) { + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + + var increment = function() { + this.counter += 1; + }; + + obj.on({ + a: increment, + b: increment, + c: increment + }, obj); + + obj.trigger('a'); + assert.equal(obj.counter, 1); + + obj.trigger('a b'); + assert.equal(obj.counter, 3); + + obj.trigger('c'); + assert.equal(obj.counter, 4); + + obj.off({ + a: increment, + c: increment + }, obj); + obj.trigger('a b c'); + assert.equal(obj.counter, 5); + }); + + QUnit.test('binding and triggering multiple event names with event maps', function(assert) { + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + + var increment = function() { + this.counter += 1; + }; + + obj.on({ + 'a b c': increment + }); + + obj.trigger('a'); + assert.equal(obj.counter, 1); + + obj.trigger('a b'); + assert.equal(obj.counter, 3); + + obj.trigger('c'); + assert.equal(obj.counter, 4); + + obj.off({ + 'a c': increment + }); + obj.trigger('a b c'); + assert.equal(obj.counter, 5); + }); + + QUnit.test('binding and trigger with event maps context', function(assert) { + assert.expect(2); + var obj = {counter: 0}; + var context = {}; + _.extend(obj, Backbone.Events); + + obj.on({ + a: function() { + assert.strictEqual(this, context, 'defaults `context` to `callback` param'); + } + }, context).trigger('a'); + + obj.off().on({ + a: function() { + assert.strictEqual(this, context, 'will not override explicit `context` param'); + } + }, this, context).trigger('a'); + }); + + QUnit.test('listenTo and stopListening', function(assert) { + assert.expect(1); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenTo(b, 'all', function(){ assert.ok(true); }); + b.trigger('anything'); + a.listenTo(b, 'all', function(){ assert.ok(false); }); + a.stopListening(); + b.trigger('anything'); + }); + + QUnit.test('listenTo and stopListening with event maps', function(assert) { + assert.expect(4); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + var cb = function(){ assert.ok(true); }; + a.listenTo(b, {event: cb}); + b.trigger('event'); + a.listenTo(b, {event2: cb}); + b.on('event2', cb); + a.stopListening(b, {event2: cb}); + b.trigger('event event2'); + a.stopListening(); + b.trigger('event event2'); + }); + + QUnit.test('stopListening with omitted args', function(assert) { + assert.expect(2); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + var cb = function() { assert.ok(true); }; + a.listenTo(b, 'event', cb); + b.on('event', cb); + a.listenTo(b, 'event2', cb); + a.stopListening(null, {event: cb}); + b.trigger('event event2'); + b.off(); + a.listenTo(b, 'event event2', cb); + a.stopListening(null, 'event'); + a.stopListening(); + b.trigger('event2'); + }); + + QUnit.test('listenToOnce', function(assert) { + assert.expect(2); + // Same as the previous test, but we use once rather than having to explicitly unbind + var obj = {counterA: 0, counterB: 0}; + _.extend(obj, Backbone.Events); + var incrA = function(){ obj.counterA += 1; obj.trigger('event'); }; + var incrB = function(){ obj.counterB += 1; }; + obj.listenToOnce(obj, 'event', incrA); + obj.listenToOnce(obj, 'event', incrB); + obj.trigger('event'); + assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.'); + assert.equal(obj.counterB, 1, 'counterB should have only been incremented once.'); + }); + + QUnit.test('listenToOnce and stopListening', function(assert) { + assert.expect(1); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenToOnce(b, 'all', function() { assert.ok(true); }); + b.trigger('anything'); + b.trigger('anything'); + a.listenToOnce(b, 'all', function() { assert.ok(false); }); + a.stopListening(); + b.trigger('anything'); + }); + + QUnit.test('listenTo, listenToOnce and stopListening', function(assert) { + assert.expect(1); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenToOnce(b, 'all', function() { assert.ok(true); }); + b.trigger('anything'); + b.trigger('anything'); + a.listenTo(b, 'all', function() { assert.ok(false); }); + a.stopListening(); + b.trigger('anything'); + }); + + QUnit.test('listenTo and stopListening with event maps', function(assert) { + assert.expect(1); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenTo(b, {change: function(){ assert.ok(true); }}); + b.trigger('change'); + a.listenTo(b, {change: function(){ assert.ok(false); }}); + a.stopListening(); + b.trigger('change'); + }); + + QUnit.test('listenTo yourself', function(assert) { + assert.expect(1); + var e = _.extend({}, Backbone.Events); + e.listenTo(e, 'foo', function(){ assert.ok(true); }); + e.trigger('foo'); + }); + + QUnit.test('listenTo yourself cleans yourself up with stopListening', function(assert) { + assert.expect(1); + var e = _.extend({}, Backbone.Events); + e.listenTo(e, 'foo', function(){ assert.ok(true); }); + e.trigger('foo'); + e.stopListening(); + e.trigger('foo'); + }); + + QUnit.test('stopListening cleans up references', function(assert) { + assert.expect(12); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + var fn = function() {}; + b.on('event', fn); + a.listenTo(b, 'event', fn).stopListening(); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + a.listenTo(b, 'event', fn).stopListening(b); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + a.listenTo(b, 'event', fn).stopListening(b, 'event'); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + a.listenTo(b, 'event', fn).stopListening(b, 'event', fn); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + }); + + QUnit.test('stopListening cleans up references from listenToOnce', function(assert) { + assert.expect(12); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + var fn = function() {}; + b.on('event', fn); + a.listenToOnce(b, 'event', fn).stopListening(); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + a.listenToOnce(b, 'event', fn).stopListening(b); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + a.listenToOnce(b, 'event', fn).stopListening(b, 'event'); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + a.listenToOnce(b, 'event', fn).stopListening(b, 'event', fn); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._events.event), 1); + assert.equal(_.size(b._listeners), 0); + }); + + QUnit.test('listenTo and off cleaning up references', function(assert) { + assert.expect(8); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + var fn = function() {}; + a.listenTo(b, 'event', fn); + b.off(); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._listeners), 0); + a.listenTo(b, 'event', fn); + b.off('event'); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._listeners), 0); + a.listenTo(b, 'event', fn); + b.off(null, fn); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._listeners), 0); + a.listenTo(b, 'event', fn); + b.off(null, null, a); + assert.equal(_.size(a._listeningTo), 0); + assert.equal(_.size(b._listeners), 0); + }); + + QUnit.test('listenTo and stopListening cleaning up references', function(assert) { + assert.expect(2); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenTo(b, 'all', function(){ assert.ok(true); }); + b.trigger('anything'); + a.listenTo(b, 'other', function(){ assert.ok(false); }); + a.stopListening(b, 'other'); + a.stopListening(b, 'all'); + assert.equal(_.size(a._listeningTo), 0); + }); + + QUnit.test('listenToOnce without context cleans up references after the event has fired', function(assert) { + assert.expect(2); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenToOnce(b, 'all', function(){ assert.ok(true); }); + b.trigger('anything'); + assert.equal(_.size(a._listeningTo), 0); + }); + + QUnit.test('listenToOnce with event maps cleans up references', function(assert) { + assert.expect(2); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenToOnce(b, { + one: function() { assert.ok(true); }, + two: function() { assert.ok(false); } + }); + b.trigger('one'); + assert.equal(_.size(a._listeningTo), 1); + }); + + QUnit.test('listenToOnce with event maps binds the correct `this`', function(assert) { + assert.expect(1); + var a = _.extend({}, Backbone.Events); + var b = _.extend({}, Backbone.Events); + a.listenToOnce(b, { + one: function() { assert.ok(this === a); }, + two: function() { assert.ok(false); } + }); + b.trigger('one'); + }); + + QUnit.test("listenTo with empty callback doesn't throw an error", function(assert) { + assert.expect(1); + var e = _.extend({}, Backbone.Events); + e.listenTo(e, 'foo', null); + e.trigger('foo'); + assert.ok(true); + }); + + QUnit.test('trigger all for each event', function(assert) { + assert.expect(3); + var a, b, obj = {counter: 0}; + _.extend(obj, Backbone.Events); + obj.on('all', function(event) { + obj.counter++; + if (event === 'a') a = true; + if (event === 'b') b = true; + }) + .trigger('a b'); + assert.ok(a); + assert.ok(b); + assert.equal(obj.counter, 2); + }); + + QUnit.test('on, then unbind all functions', function(assert) { + assert.expect(1); + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + var callback = function() { obj.counter += 1; }; + obj.on('event', callback); + obj.trigger('event'); + obj.off('event'); + obj.trigger('event'); + assert.equal(obj.counter, 1, 'counter should have only been incremented once.'); + }); + + QUnit.test('bind two callbacks, unbind only one', function(assert) { + assert.expect(2); + var obj = {counterA: 0, counterB: 0}; + _.extend(obj, Backbone.Events); + var callback = function() { obj.counterA += 1; }; + obj.on('event', callback); + obj.on('event', function() { obj.counterB += 1; }); + obj.trigger('event'); + obj.off('event', callback); + obj.trigger('event'); + assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.'); + assert.equal(obj.counterB, 2, 'counterB should have been incremented twice.'); + }); + + QUnit.test('unbind a callback in the midst of it firing', function(assert) { + assert.expect(1); + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + var callback = function() { + obj.counter += 1; + obj.off('event', callback); + }; + obj.on('event', callback); + obj.trigger('event'); + obj.trigger('event'); + obj.trigger('event'); + assert.equal(obj.counter, 1, 'the callback should have been unbound.'); + }); + + QUnit.test('two binds that unbind themeselves', function(assert) { + assert.expect(2); + var obj = {counterA: 0, counterB: 0}; + _.extend(obj, Backbone.Events); + var incrA = function(){ obj.counterA += 1; obj.off('event', incrA); }; + var incrB = function(){ obj.counterB += 1; obj.off('event', incrB); }; + obj.on('event', incrA); + obj.on('event', incrB); + obj.trigger('event'); + obj.trigger('event'); + obj.trigger('event'); + assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.'); + assert.equal(obj.counterB, 1, 'counterB should have only been incremented once.'); + }); + + QUnit.test('bind a callback with a default context when none supplied', function(assert) { + assert.expect(1); + var obj = _.extend({ + assertTrue: function() { + assert.equal(this, obj, '`this` was bound to the callback'); + } + }, Backbone.Events); + + obj.once('event', obj.assertTrue); + obj.trigger('event'); + }); + + QUnit.test('bind a callback with a supplied context', function(assert) { + assert.expect(1); + var TestClass = function() { + return this; + }; + TestClass.prototype.assertTrue = function() { + assert.ok(true, '`this` was bound to the callback'); + }; + + var obj = _.extend({}, Backbone.Events); + obj.on('event', function() { this.assertTrue(); }, new TestClass); + obj.trigger('event'); + }); + + QUnit.test('nested trigger with unbind', function(assert) { + assert.expect(1); + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + var incr1 = function(){ obj.counter += 1; obj.off('event', incr1); obj.trigger('event'); }; + var incr2 = function(){ obj.counter += 1; }; + obj.on('event', incr1); + obj.on('event', incr2); + obj.trigger('event'); + assert.equal(obj.counter, 3, 'counter should have been incremented three times'); + }); + + QUnit.test('callback list is not altered during trigger', function(assert) { + assert.expect(2); + var counter = 0, obj = _.extend({}, Backbone.Events); + var incr = function(){ counter++; }; + var incrOn = function(){ obj.on('event all', incr); }; + var incrOff = function(){ obj.off('event all', incr); }; + + obj.on('event all', incrOn).trigger('event'); + assert.equal(counter, 0, 'on does not alter callback list'); + + obj.off().on('event', incrOff).on('event all', incr).trigger('event'); + assert.equal(counter, 2, 'off does not alter callback list'); + }); + + QUnit.test("#1282 - 'all' callback list is retrieved after each event.", function(assert) { + assert.expect(1); + var counter = 0; + var obj = _.extend({}, Backbone.Events); + var incr = function(){ counter++; }; + obj.on('x', function() { + obj.on('y', incr).on('all', incr); + }) + .trigger('x y'); + assert.strictEqual(counter, 2); + }); + + QUnit.test('if no callback is provided, `on` is a noop', function(assert) { + assert.expect(0); + _.extend({}, Backbone.Events).on('test').trigger('test'); + }); + + QUnit.test('if callback is truthy but not a function, `on` should throw an error just like jQuery', function(assert) { + assert.expect(1); + var view = _.extend({}, Backbone.Events).on('test', 'noop'); + assert.raises(function() { + view.trigger('test'); + }); + }); + + QUnit.test('remove all events for a specific context', function(assert) { + assert.expect(4); + var obj = _.extend({}, Backbone.Events); + obj.on('x y all', function() { assert.ok(true); }); + obj.on('x y all', function() { assert.ok(false); }, obj); + obj.off(null, null, obj); + obj.trigger('x y'); + }); + + QUnit.test('remove all events for a specific callback', function(assert) { + assert.expect(4); + var obj = _.extend({}, Backbone.Events); + var success = function() { assert.ok(true); }; + var fail = function() { assert.ok(false); }; + obj.on('x y all', success); + obj.on('x y all', fail); + obj.off(null, fail); + obj.trigger('x y'); + }); + + QUnit.test('#1310 - off does not skip consecutive events', function(assert) { + assert.expect(0); + var obj = _.extend({}, Backbone.Events); + obj.on('event', function() { assert.ok(false); }, obj); + obj.on('event', function() { assert.ok(false); }, obj); + obj.off(null, null, obj); + obj.trigger('event'); + }); + + QUnit.test('once', function(assert) { + assert.expect(2); + // Same as the previous test, but we use once rather than having to explicitly unbind + var obj = {counterA: 0, counterB: 0}; + _.extend(obj, Backbone.Events); + var incrA = function(){ obj.counterA += 1; obj.trigger('event'); }; + var incrB = function(){ obj.counterB += 1; }; + obj.once('event', incrA); + obj.once('event', incrB); + obj.trigger('event'); + assert.equal(obj.counterA, 1, 'counterA should have only been incremented once.'); + assert.equal(obj.counterB, 1, 'counterB should have only been incremented once.'); + }); + + QUnit.test('once variant one', function(assert) { + assert.expect(3); + var f = function(){ assert.ok(true); }; + + var a = _.extend({}, Backbone.Events).once('event', f); + var b = _.extend({}, Backbone.Events).on('event', f); + + a.trigger('event'); + + b.trigger('event'); + b.trigger('event'); + }); + + QUnit.test('once variant two', function(assert) { + assert.expect(3); + var f = function(){ assert.ok(true); }; + var obj = _.extend({}, Backbone.Events); + + obj + .once('event', f) + .on('event', f) + .trigger('event') + .trigger('event'); + }); + + QUnit.test('once with off', function(assert) { + assert.expect(0); + var f = function(){ assert.ok(true); }; + var obj = _.extend({}, Backbone.Events); + + obj.once('event', f); + obj.off('event', f); + obj.trigger('event'); + }); + + QUnit.test('once with event maps', function(assert) { + var obj = {counter: 0}; + _.extend(obj, Backbone.Events); + + var increment = function() { + this.counter += 1; + }; + + obj.once({ + a: increment, + b: increment, + c: increment + }, obj); + + obj.trigger('a'); + assert.equal(obj.counter, 1); + + obj.trigger('a b'); + assert.equal(obj.counter, 2); + + obj.trigger('c'); + assert.equal(obj.counter, 3); + + obj.trigger('a b c'); + assert.equal(obj.counter, 3); + }); + + QUnit.test('bind a callback with a supplied context using once with object notation', function(assert) { + assert.expect(1); + var obj = {counter: 0}; + var context = {}; + _.extend(obj, Backbone.Events); + + obj.once({ + a: function() { + assert.strictEqual(this, context, 'defaults `context` to `callback` param'); + } + }, context).trigger('a'); + }); + + QUnit.test('once with off only by context', function(assert) { + assert.expect(0); + var context = {}; + var obj = _.extend({}, Backbone.Events); + obj.once('event', function(){ assert.ok(false); }, context); + obj.off(null, null, context); + obj.trigger('event'); + }); + + QUnit.test('Backbone object inherits Events', function(assert) { + assert.ok(Backbone.on === Backbone.Events.on); + }); + + QUnit.test('once with asynchronous events', function(assert) { + var done = assert.async(); + assert.expect(1); + var func = _.debounce(function() { assert.ok(true); done(); }, 50); + var obj = _.extend({}, Backbone.Events).once('async', func); + + obj.trigger('async'); + obj.trigger('async'); + }); + + QUnit.test('once with multiple events.', function(assert) { + assert.expect(2); + var obj = _.extend({}, Backbone.Events); + obj.once('x y', function() { assert.ok(true); }); + obj.trigger('x y'); + }); + + QUnit.test('Off during iteration with once.', function(assert) { + assert.expect(2); + var obj = _.extend({}, Backbone.Events); + var f = function(){ this.off('event', f); }; + obj.on('event', f); + obj.once('event', function(){}); + obj.on('event', function(){ assert.ok(true); }); + + obj.trigger('event'); + obj.trigger('event'); + }); + + QUnit.test('`once` on `all` should work as expected', function(assert) { + assert.expect(1); + Backbone.once('all', function() { + assert.ok(true); + Backbone.trigger('all'); + }); + Backbone.trigger('all'); + }); + + QUnit.test('once without a callback is a noop', function(assert) { + assert.expect(0); + _.extend({}, Backbone.Events).once('event').trigger('event'); + }); + + QUnit.test('listenToOnce without a callback is a noop', function(assert) { + assert.expect(0); + var obj = _.extend({}, Backbone.Events); + obj.listenToOnce(obj, 'event').trigger('event'); + }); + + QUnit.test('event functions are chainable', function(assert) { + var obj = _.extend({}, Backbone.Events); + var obj2 = _.extend({}, Backbone.Events); + var fn = function() {}; + assert.equal(obj, obj.trigger('noeventssetyet')); + assert.equal(obj, obj.off('noeventssetyet')); + assert.equal(obj, obj.stopListening('noeventssetyet')); + assert.equal(obj, obj.on('a', fn)); + assert.equal(obj, obj.once('c', fn)); + assert.equal(obj, obj.trigger('a')); + assert.equal(obj, obj.listenTo(obj2, 'a', fn)); + assert.equal(obj, obj.listenToOnce(obj2, 'b', fn)); + assert.equal(obj, obj.off('a c')); + assert.equal(obj, obj.stopListening(obj2, 'a')); + assert.equal(obj, obj.stopListening()); + }); + + QUnit.test('#3448 - listenToOnce with space-separated events', function(assert) { + assert.expect(2); + var one = _.extend({}, Backbone.Events); + var two = _.extend({}, Backbone.Events); + var count = 1; + one.listenToOnce(two, 'x y', function(n) { assert.ok(n === count++); }); + two.trigger('x', 1); + two.trigger('x', 1); + two.trigger('y', 2); + two.trigger('y', 2); + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/model.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/model.js new file mode 100644 index 00000000..b73a1c79 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/model.js @@ -0,0 +1,1418 @@ +(function() { + + var ProxyModel = Backbone.Model.extend(); + var Klass = Backbone.Collection.extend({ + url: function() { return '/collection'; } + }); + var doc, collection; + + QUnit.module('Backbone.Model', { + + beforeEach: function(assert) { + doc = new ProxyModel({ + id: '1-the-tempest', + title: 'The Tempest', + author: 'Bill Shakespeare', + length: 123 + }); + collection = new Klass(); + collection.add(doc); + } + + }); + + QUnit.test('initialize', function(assert) { + assert.expect(3); + var Model = Backbone.Model.extend({ + initialize: function() { + this.one = 1; + assert.equal(this.collection, collection); + } + }); + var model = new Model({}, {collection: collection}); + assert.equal(model.one, 1); + assert.equal(model.collection, collection); + }); + + QUnit.test('Object.prototype properties are overridden by attributes', function(assert) { + assert.expect(1); + var model = new Backbone.Model({hasOwnProperty: true}); + assert.equal(model.get('hasOwnProperty'), true); + }); + + QUnit.test('initialize with attributes and options', function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + initialize: function(attributes, options) { + this.one = options.one; + } + }); + var model = new Model({}, {one: 1}); + assert.equal(model.one, 1); + }); + + QUnit.test('initialize with parsed attributes', function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + parse: function(attrs) { + attrs.value += 1; + return attrs; + } + }); + var model = new Model({value: 1}, {parse: true}); + assert.equal(model.get('value'), 2); + }); + + QUnit.test('parse can return null', function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + parse: function(attrs) { + attrs.value += 1; + return null; + } + }); + var model = new Model({value: 1}, {parse: true}); + assert.equal(JSON.stringify(model.toJSON()), '{}'); + }); + + QUnit.test('url', function(assert) { + assert.expect(3); + doc.urlRoot = null; + assert.equal(doc.url(), '/collection/1-the-tempest'); + doc.collection.url = '/collection/'; + assert.equal(doc.url(), '/collection/1-the-tempest'); + doc.collection = null; + assert.raises(function() { doc.url(); }); + doc.collection = collection; + }); + + QUnit.test('url when using urlRoot, and uri encoding', function(assert) { + assert.expect(2); + var Model = Backbone.Model.extend({ + urlRoot: '/collection' + }); + var model = new Model(); + assert.equal(model.url(), '/collection'); + model.set({id: '+1+'}); + assert.equal(model.url(), '/collection/%2B1%2B'); + }); + + QUnit.test('url when using urlRoot as a function to determine urlRoot at runtime', function(assert) { + assert.expect(2); + var Model = Backbone.Model.extend({ + urlRoot: function() { + return '/nested/' + this.get('parentId') + '/collection'; + } + }); + + var model = new Model({parentId: 1}); + assert.equal(model.url(), '/nested/1/collection'); + model.set({id: 2}); + assert.equal(model.url(), '/nested/1/collection/2'); + }); + + QUnit.test('underscore methods', function(assert) { + assert.expect(5); + var model = new Backbone.Model({foo: 'a', bar: 'b', baz: 'c'}); + var model2 = model.clone(); + assert.deepEqual(model.keys(), ['foo', 'bar', 'baz']); + assert.deepEqual(model.values(), ['a', 'b', 'c']); + assert.deepEqual(model.invert(), {a: 'foo', b: 'bar', c: 'baz'}); + assert.deepEqual(model.pick('foo', 'baz'), {foo: 'a', baz: 'c'}); + assert.deepEqual(model.omit('foo', 'bar'), {baz: 'c'}); + }); + + QUnit.test('chain', function(assert) { + var model = new Backbone.Model({a: 0, b: 1, c: 2}); + assert.deepEqual(model.chain().pick('a', 'b', 'c').values().compact().value(), [1, 2]); + }); + + QUnit.test('clone', function(assert) { + assert.expect(10); + var a = new Backbone.Model({foo: 1, bar: 2, baz: 3}); + var b = a.clone(); + assert.equal(a.get('foo'), 1); + assert.equal(a.get('bar'), 2); + assert.equal(a.get('baz'), 3); + assert.equal(b.get('foo'), a.get('foo'), 'Foo should be the same on the clone.'); + assert.equal(b.get('bar'), a.get('bar'), 'Bar should be the same on the clone.'); + assert.equal(b.get('baz'), a.get('baz'), 'Baz should be the same on the clone.'); + a.set({foo: 100}); + assert.equal(a.get('foo'), 100); + assert.equal(b.get('foo'), 1, 'Changing a parent attribute does not change the clone.'); + + var foo = new Backbone.Model({p: 1}); + var bar = new Backbone.Model({p: 2}); + bar.set(foo.clone().attributes, {unset: true}); + assert.equal(foo.get('p'), 1); + assert.equal(bar.get('p'), undefined); + }); + + QUnit.test('isNew', function(assert) { + assert.expect(6); + var a = new Backbone.Model({foo: 1, bar: 2, baz: 3}); + assert.ok(a.isNew(), 'it should be new'); + a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: -5}); + assert.ok(!a.isNew(), 'any defined ID is legal, negative or positive'); + a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: 0}); + assert.ok(!a.isNew(), 'any defined ID is legal, including zero'); + assert.ok(new Backbone.Model().isNew(), 'is true when there is no id'); + assert.ok(!new Backbone.Model({id: 2}).isNew(), 'is false for a positive integer'); + assert.ok(!new Backbone.Model({id: -5}).isNew(), 'is false for a negative integer'); + }); + + QUnit.test('get', function(assert) { + assert.expect(2); + assert.equal(doc.get('title'), 'The Tempest'); + assert.equal(doc.get('author'), 'Bill Shakespeare'); + }); + + QUnit.test('escape', function(assert) { + assert.expect(5); + assert.equal(doc.escape('title'), 'The Tempest'); + doc.set({audience: 'Bill & Bob'}); + assert.equal(doc.escape('audience'), 'Bill & Bob'); + doc.set({audience: 'Tim > Joan'}); + assert.equal(doc.escape('audience'), 'Tim > Joan'); + doc.set({audience: 10101}); + assert.equal(doc.escape('audience'), '10101'); + doc.unset('audience'); + assert.equal(doc.escape('audience'), ''); + }); + + QUnit.test('has', function(assert) { + assert.expect(10); + var model = new Backbone.Model(); + + assert.strictEqual(model.has('name'), false); + + model.set({ + '0': 0, + '1': 1, + 'true': true, + 'false': false, + 'empty': '', + 'name': 'name', + 'null': null, + 'undefined': undefined + }); + + assert.strictEqual(model.has('0'), true); + assert.strictEqual(model.has('1'), true); + assert.strictEqual(model.has('true'), true); + assert.strictEqual(model.has('false'), true); + assert.strictEqual(model.has('empty'), true); + assert.strictEqual(model.has('name'), true); + + model.unset('name'); + + assert.strictEqual(model.has('name'), false); + assert.strictEqual(model.has('null'), false); + assert.strictEqual(model.has('undefined'), false); + }); + + QUnit.test('matches', function(assert) { + assert.expect(4); + var model = new Backbone.Model(); + + assert.strictEqual(model.matches({name: 'Jonas', cool: true}), false); + + model.set({name: 'Jonas', cool: true}); + + assert.strictEqual(model.matches({name: 'Jonas'}), true); + assert.strictEqual(model.matches({name: 'Jonas', cool: true}), true); + assert.strictEqual(model.matches({name: 'Jonas', cool: false}), false); + }); + + QUnit.test('matches with predicate', function(assert) { + var model = new Backbone.Model({a: 0}); + + assert.strictEqual(model.matches(function(attr) { + return attr.a > 1 && attr.b != null; + }), false); + + model.set({a: 3, b: true}); + + assert.strictEqual(model.matches(function(attr) { + return attr.a > 1 && attr.b != null; + }), true); + }); + + QUnit.test('set and unset', function(assert) { + assert.expect(8); + var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3}); + var changeCount = 0; + a.on('change:foo', function() { changeCount += 1; }); + a.set({foo: 2}); + assert.equal(a.get('foo'), 2, 'Foo should have changed.'); + assert.equal(changeCount, 1, 'Change count should have incremented.'); + // set with value that is not new shouldn't fire change event + a.set({foo: 2}); + assert.equal(a.get('foo'), 2, 'Foo should NOT have changed, still 2'); + assert.equal(changeCount, 1, 'Change count should NOT have incremented.'); + + a.validate = function(attrs) { + assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting'); + }; + a.unset('foo', {validate: true}); + assert.equal(a.get('foo'), void 0, 'Foo should have changed'); + delete a.validate; + assert.equal(changeCount, 2, 'Change count should have incremented for unset.'); + + a.unset('id'); + assert.equal(a.id, undefined, 'Unsetting the id should remove the id property.'); + }); + + QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) { + var attr = 0, main = 0, error = 0; + var Model = Backbone.Model.extend({ + validate: function(attrs) { + if (attrs.x > 1) { + error++; + return 'this is an error'; + } + } + }); + var model = new Model({x: 0}); + model.on('change:x', function() { attr++; }); + model.on('change', function() { main++; }); + model.set({x: 2}, {validate: true}); + model.set({x: 1}, {validate: true}); + assert.deepEqual([attr, main, error], [1, 1, 1]); + }); + + QUnit.test('set triggers changes in the correct order', function(assert) { + var value = null; + var model = new Backbone.Model; + model.on('last', function(){ value = 'last'; }); + model.on('first', function(){ value = 'first'; }); + model.trigger('first'); + model.trigger('last'); + assert.equal(value, 'last'); + }); + + QUnit.test('set falsy values in the correct order', function(assert) { + assert.expect(2); + var model = new Backbone.Model({result: 'result'}); + model.on('change', function() { + assert.equal(model.changed.result, void 0); + assert.equal(model.previous('result'), false); + }); + model.set({result: void 0}, {silent: true}); + model.set({result: null}, {silent: true}); + model.set({result: false}, {silent: true}); + model.set({result: void 0}); + }); + + QUnit.test('nested set triggers with the correct options', function(assert) { + var model = new Backbone.Model(); + var o1 = {}; + var o2 = {}; + var o3 = {}; + model.on('change', function(__, options) { + switch (model.get('a')) { + case 1: + assert.equal(options, o1); + return model.set('a', 2, o2); + case 2: + assert.equal(options, o2); + return model.set('a', 3, o3); + case 3: + assert.equal(options, o3); + } + }); + model.set('a', 1, o1); + }); + + QUnit.test('multiple unsets', function(assert) { + assert.expect(1); + var i = 0; + var counter = function(){ i++; }; + var model = new Backbone.Model({a: 1}); + model.on('change:a', counter); + model.set({a: 2}); + model.unset('a'); + model.unset('a'); + assert.equal(i, 2, 'Unset does not fire an event for missing attributes.'); + }); + + QUnit.test('unset and changedAttributes', function(assert) { + assert.expect(1); + var model = new Backbone.Model({a: 1}); + model.on('change', function() { + assert.ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties'); + }); + model.unset('a'); + }); + + QUnit.test('using a non-default id attribute.', function(assert) { + assert.expect(5); + var MongoModel = Backbone.Model.extend({idAttribute: '_id'}); + var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'}); + assert.equal(model.get('id'), 'eye-dee'); + assert.equal(model.id, 25); + assert.equal(model.isNew(), false); + model.unset('_id'); + assert.equal(model.id, undefined); + assert.equal(model.isNew(), true); + }); + + QUnit.test('setting an alternative cid prefix', function(assert) { + assert.expect(4); + var Model = Backbone.Model.extend({ + cidPrefix: 'm' + }); + var model = new Model(); + + assert.equal(model.cid.charAt(0), 'm'); + + model = new Backbone.Model(); + assert.equal(model.cid.charAt(0), 'c'); + + var Collection = Backbone.Collection.extend({ + model: Model + }); + var col = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]); + + assert.equal(col.get('c6').cid.charAt(0), 'm'); + col.set([{id: 'c6', value: 'test'}], { + merge: true, + add: true, + remove: false + }); + assert.ok(col.get('c6').has('value')); + }); + + QUnit.test('set an empty string', function(assert) { + assert.expect(1); + var model = new Backbone.Model({name: 'Model'}); + model.set({name: ''}); + assert.equal(model.get('name'), ''); + }); + + QUnit.test('setting an object', function(assert) { + assert.expect(1); + var model = new Backbone.Model({ + custom: {foo: 1} + }); + model.on('change', function() { + assert.ok(1); + }); + model.set({ + custom: {foo: 1} // no change should be fired + }); + model.set({ + custom: {foo: 2} // change event should be fired + }); + }); + + QUnit.test('clear', function(assert) { + assert.expect(3); + var changed; + var model = new Backbone.Model({id: 1, name: 'Model'}); + model.on('change:name', function(){ changed = true; }); + model.on('change', function() { + var changedAttrs = model.changedAttributes(); + assert.ok('name' in changedAttrs); + }); + model.clear(); + assert.equal(changed, true); + assert.equal(model.get('name'), undefined); + }); + + QUnit.test('defaults', function(assert) { + assert.expect(9); + var Defaulted = Backbone.Model.extend({ + defaults: { + one: 1, + two: 2 + } + }); + var model = new Defaulted({two: undefined}); + assert.equal(model.get('one'), 1); + assert.equal(model.get('two'), 2); + model = new Defaulted({two: 3}); + assert.equal(model.get('one'), 1); + assert.equal(model.get('two'), 3); + Defaulted = Backbone.Model.extend({ + defaults: function() { + return { + one: 3, + two: 4 + }; + } + }); + model = new Defaulted({two: undefined}); + assert.equal(model.get('one'), 3); + assert.equal(model.get('two'), 4); + Defaulted = Backbone.Model.extend({ + defaults: {hasOwnProperty: true} + }); + model = new Defaulted(); + assert.equal(model.get('hasOwnProperty'), true); + model = new Defaulted({hasOwnProperty: undefined}); + assert.equal(model.get('hasOwnProperty'), true); + model = new Defaulted({hasOwnProperty: false}); + assert.equal(model.get('hasOwnProperty'), false); + }); + + QUnit.test('change, hasChanged, changedAttributes, previous, previousAttributes', function(assert) { + assert.expect(9); + var model = new Backbone.Model({name: 'Tim', age: 10}); + assert.deepEqual(model.changedAttributes(), false); + model.on('change', function() { + assert.ok(model.hasChanged('name'), 'name changed'); + assert.ok(!model.hasChanged('age'), 'age did not'); + assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAttributes returns the changed attrs'); + assert.equal(model.previous('name'), 'Tim'); + assert.ok(_.isEqual(model.previousAttributes(), {name: 'Tim', age: 10}), 'previousAttributes is correct'); + }); + assert.equal(model.hasChanged(), false); + assert.equal(model.hasChanged(undefined), false); + model.set({name: 'Rob'}); + assert.equal(model.get('name'), 'Rob'); + }); + + QUnit.test('changedAttributes', function(assert) { + assert.expect(3); + var model = new Backbone.Model({a: 'a', b: 'b'}); + assert.deepEqual(model.changedAttributes(), false); + assert.equal(model.changedAttributes({a: 'a'}), false); + assert.equal(model.changedAttributes({a: 'b'}).a, 'b'); + }); + + QUnit.test('change with options', function(assert) { + assert.expect(2); + var value; + var model = new Backbone.Model({name: 'Rob'}); + model.on('change', function(m, options) { + value = options.prefix + m.get('name'); + }); + model.set({name: 'Bob'}, {prefix: 'Mr. '}); + assert.equal(value, 'Mr. Bob'); + model.set({name: 'Sue'}, {prefix: 'Ms. '}); + assert.equal(value, 'Ms. Sue'); + }); + + QUnit.test('change after initialize', function(assert) { + assert.expect(1); + var changed = 0; + var attrs = {id: 1, label: 'c'}; + var obj = new Backbone.Model(attrs); + obj.on('change', function() { changed += 1; }); + obj.set(attrs); + assert.equal(changed, 0); + }); + + QUnit.test('save within change event', function(assert) { + assert.expect(1); + var env = this; + var model = new Backbone.Model({firstName: 'Taylor', lastName: 'Swift'}); + model.url = '/test'; + model.on('change', function() { + model.save(); + assert.ok(_.isEqual(env.syncArgs.model, model)); + }); + model.set({lastName: 'Hicks'}); + }); + + QUnit.test('validate after save', function(assert) { + assert.expect(2); + var lastError, model = new Backbone.Model(); + model.validate = function(attrs) { + if (attrs.admin) return "Can't change admin status."; + }; + model.sync = function(method, m, options) { + options.success.call(this, {admin: true}); + }; + model.on('invalid', function(m, error) { + lastError = error; + }); + model.save(null); + + assert.equal(lastError, "Can't change admin status."); + assert.equal(model.validationError, "Can't change admin status."); + }); + + QUnit.test('save', function(assert) { + assert.expect(2); + doc.save({title: 'Henry V'}); + assert.equal(this.syncArgs.method, 'update'); + assert.ok(_.isEqual(this.syncArgs.model, doc)); + }); + + QUnit.test('save, fetch, destroy triggers error event when an error occurs', function(assert) { + assert.expect(3); + var model = new Backbone.Model(); + model.on('error', function() { + assert.ok(true); + }); + model.sync = function(method, m, options) { + options.error(); + }; + model.save({data: 2, id: 1}); + model.fetch(); + model.destroy(); + }); + + QUnit.test('#3283 - save, fetch, destroy calls success with context', function(assert) { + assert.expect(3); + var model = new Backbone.Model(); + var obj = {}; + var options = { + context: obj, + success: function() { + assert.equal(this, obj); + } + }; + model.sync = function(method, m, opts) { + opts.success.call(opts.context); + }; + model.save({data: 2, id: 1}, options); + model.fetch(options); + model.destroy(options); + }); + + QUnit.test('#3283 - save, fetch, destroy calls error with context', function(assert) { + assert.expect(3); + var model = new Backbone.Model(); + var obj = {}; + var options = { + context: obj, + error: function() { + assert.equal(this, obj); + } + }; + model.sync = function(method, m, opts) { + opts.error.call(opts.context); + }; + model.save({data: 2, id: 1}, options); + model.fetch(options); + model.destroy(options); + }); + + QUnit.test('#3470 - save and fetch with parse false', function(assert) { + assert.expect(2); + var i = 0; + var model = new Backbone.Model(); + model.parse = function() { + assert.ok(false); + }; + model.sync = function(method, m, options) { + options.success({i: ++i}); + }; + model.fetch({parse: false}); + assert.equal(model.get('i'), i); + model.save(null, {parse: false}); + assert.equal(model.get('i'), i); + }); + + QUnit.test('save with PATCH', function(assert) { + doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4}); + doc.save(); + assert.equal(this.syncArgs.method, 'update'); + assert.equal(this.syncArgs.options.attrs, undefined); + + doc.save({b: 2, d: 4}, {patch: true}); + assert.equal(this.syncArgs.method, 'patch'); + assert.equal(_.size(this.syncArgs.options.attrs), 2); + assert.equal(this.syncArgs.options.attrs.d, 4); + assert.equal(this.syncArgs.options.attrs.a, undefined); + assert.equal(this.ajaxSettings.data, '{"b":2,"d":4}'); + }); + + QUnit.test('save with PATCH and different attrs', function(assert) { + doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}}); + assert.equal(this.syncArgs.options.attrs.D, 3); + assert.equal(this.syncArgs.options.attrs.d, undefined); + assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}'); + assert.deepEqual(doc.attributes, {b: 2, d: 4}); + }); + + QUnit.test('save in positional style', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.sync = function(method, m, options) { + options.success(); + }; + model.save('title', 'Twelfth Night'); + assert.equal(model.get('title'), 'Twelfth Night'); + }); + + QUnit.test('save with non-object success response', function(assert) { + assert.expect(2); + var model = new Backbone.Model(); + model.sync = function(method, m, options) { + options.success('', options); + options.success(null, options); + }; + model.save({testing: 'empty'}, { + success: function(m) { + assert.deepEqual(m.attributes, {testing: 'empty'}); + } + }); + }); + + QUnit.test('save with wait and supplied id', function(assert) { + var Model = Backbone.Model.extend({ + urlRoot: '/collection' + }); + var model = new Model(); + model.save({id: 42}, {wait: true}); + assert.equal(this.ajaxSettings.url, '/collection/42'); + }); + + QUnit.test('save will pass extra options to success callback', function(assert) { + assert.expect(1); + var SpecialSyncModel = Backbone.Model.extend({ + sync: function(method, m, options) { + _.extend(options, {specialSync: true}); + return Backbone.Model.prototype.sync.call(this, method, m, options); + }, + urlRoot: '/test' + }); + + var model = new SpecialSyncModel(); + + var onSuccess = function(m, response, options) { + assert.ok(options.specialSync, 'Options were passed correctly to callback'); + }; + + model.save(null, {success: onSuccess}); + this.ajaxSettings.success(); + }); + + QUnit.test('fetch', function(assert) { + assert.expect(2); + doc.fetch(); + assert.equal(this.syncArgs.method, 'read'); + assert.ok(_.isEqual(this.syncArgs.model, doc)); + }); + + QUnit.test('fetch will pass extra options to success callback', function(assert) { + assert.expect(1); + var SpecialSyncModel = Backbone.Model.extend({ + sync: function(method, m, options) { + _.extend(options, {specialSync: true}); + return Backbone.Model.prototype.sync.call(this, method, m, options); + }, + urlRoot: '/test' + }); + + var model = new SpecialSyncModel(); + + var onSuccess = function(m, response, options) { + assert.ok(options.specialSync, 'Options were passed correctly to callback'); + }; + + model.fetch({success: onSuccess}); + this.ajaxSettings.success(); + }); + + QUnit.test('destroy', function(assert) { + assert.expect(3); + doc.destroy(); + assert.equal(this.syncArgs.method, 'delete'); + assert.ok(_.isEqual(this.syncArgs.model, doc)); + + var newModel = new Backbone.Model; + assert.equal(newModel.destroy(), false); + }); + + QUnit.test('destroy will pass extra options to success callback', function(assert) { + assert.expect(1); + var SpecialSyncModel = Backbone.Model.extend({ + sync: function(method, m, options) { + _.extend(options, {specialSync: true}); + return Backbone.Model.prototype.sync.call(this, method, m, options); + }, + urlRoot: '/test' + }); + + var model = new SpecialSyncModel({id: 'id'}); + + var onSuccess = function(m, response, options) { + assert.ok(options.specialSync, 'Options were passed correctly to callback'); + }; + + model.destroy({success: onSuccess}); + this.ajaxSettings.success(); + }); + + QUnit.test('non-persisted destroy', function(assert) { + assert.expect(1); + var a = new Backbone.Model({foo: 1, bar: 2, baz: 3}); + a.sync = function() { throw 'should not be called'; }; + a.destroy(); + assert.ok(true, 'non-persisted model should not call sync'); + }); + + QUnit.test('validate', function(assert) { + var lastError; + var model = new Backbone.Model(); + model.validate = function(attrs) { + if (attrs.admin !== this.get('admin')) return "Can't change admin status."; + }; + model.on('invalid', function(m, error) { + lastError = error; + }); + var result = model.set({a: 100}); + assert.equal(result, model); + assert.equal(model.get('a'), 100); + assert.equal(lastError, undefined); + result = model.set({admin: true}); + assert.equal(model.get('admin'), true); + result = model.set({a: 200, admin: false}, {validate: true}); + assert.equal(lastError, "Can't change admin status."); + assert.equal(result, false); + assert.equal(model.get('a'), 100); + }); + + QUnit.test('validate on unset and clear', function(assert) { + assert.expect(6); + var error; + var model = new Backbone.Model({name: 'One'}); + model.validate = function(attrs) { + if (!attrs.name) { + error = true; + return 'No thanks.'; + } + }; + model.set({name: 'Two'}); + assert.equal(model.get('name'), 'Two'); + assert.equal(error, undefined); + model.unset('name', {validate: true}); + assert.equal(error, true); + assert.equal(model.get('name'), 'Two'); + model.clear({validate: true}); + assert.equal(model.get('name'), 'Two'); + delete model.validate; + model.clear(); + assert.equal(model.get('name'), undefined); + }); + + QUnit.test('validate with error callback', function(assert) { + assert.expect(8); + var lastError, boundError; + var model = new Backbone.Model(); + model.validate = function(attrs) { + if (attrs.admin) return "Can't change admin status."; + }; + model.on('invalid', function(m, error) { + boundError = true; + }); + var result = model.set({a: 100}, {validate: true}); + assert.equal(result, model); + assert.equal(model.get('a'), 100); + assert.equal(model.validationError, null); + assert.equal(boundError, undefined); + result = model.set({a: 200, admin: true}, {validate: true}); + assert.equal(result, false); + assert.equal(model.get('a'), 100); + assert.equal(model.validationError, "Can't change admin status."); + assert.equal(boundError, true); + }); + + QUnit.test('defaults always extend attrs (#459)', function(assert) { + assert.expect(2); + var Defaulted = Backbone.Model.extend({ + defaults: {one: 1}, + initialize: function(attrs, opts) { + assert.equal(this.attributes.one, 1); + } + }); + var providedattrs = new Defaulted({}); + var emptyattrs = new Defaulted(); + }); + + QUnit.test('Inherit class properties', function(assert) { + assert.expect(6); + var Parent = Backbone.Model.extend({ + instancePropSame: function() {}, + instancePropDiff: function() {} + }, { + classProp: function() {} + }); + var Child = Parent.extend({ + instancePropDiff: function() {} + }); + + var adult = new Parent; + var kid = new Child; + + assert.equal(Child.classProp, Parent.classProp); + assert.notEqual(Child.classProp, undefined); + + assert.equal(kid.instancePropSame, adult.instancePropSame); + assert.notEqual(kid.instancePropSame, undefined); + + assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff); + assert.notEqual(Child.prototype.instancePropDiff, undefined); + }); + + QUnit.test("Nested change events don't clobber previous attributes", function(assert) { + assert.expect(4); + new Backbone.Model() + .on('change:state', function(m, newState) { + assert.equal(m.previous('state'), undefined); + assert.equal(newState, 'hello'); + // Fire a nested change event. + m.set({other: 'whatever'}); + }) + .on('change:state', function(m, newState) { + assert.equal(m.previous('state'), undefined); + assert.equal(newState, 'hello'); + }) + .set({state: 'hello'}); + }); + + QUnit.test('hasChanged/set should use same comparison', function(assert) { + assert.expect(2); + var changed = 0, model = new Backbone.Model({a: null}); + model.on('change', function() { + assert.ok(this.hasChanged('a')); + }) + .on('change:a', function() { + changed++; + }) + .set({a: undefined}); + assert.equal(changed, 1); + }); + + QUnit.test('#582, #425, change:attribute callbacks should fire after all changes have occurred', function(assert) { + assert.expect(9); + var model = new Backbone.Model; + + var assertion = function() { + assert.equal(model.get('a'), 'a'); + assert.equal(model.get('b'), 'b'); + assert.equal(model.get('c'), 'c'); + }; + + model.on('change:a', assertion); + model.on('change:b', assertion); + model.on('change:c', assertion); + + model.set({a: 'a', b: 'b', c: 'c'}); + }); + + QUnit.test('#871, set with attributes property', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.set({attributes: true}); + assert.ok(model.has('attributes')); + }); + + QUnit.test('set value regardless of equality/change', function(assert) { + assert.expect(1); + var model = new Backbone.Model({x: []}); + var a = []; + model.set({x: a}); + assert.ok(model.get('x') === a); + }); + + QUnit.test('set same value does not trigger change', function(assert) { + assert.expect(0); + var model = new Backbone.Model({x: 1}); + model.on('change change:x', function() { assert.ok(false); }); + model.set({x: 1}); + model.set({x: 1}); + }); + + QUnit.test('unset does not fire a change for undefined attributes', function(assert) { + assert.expect(0); + var model = new Backbone.Model({x: undefined}); + model.on('change:x', function(){ assert.ok(false); }); + model.unset('x'); + }); + + QUnit.test('set: undefined values', function(assert) { + assert.expect(1); + var model = new Backbone.Model({x: undefined}); + assert.ok('x' in model.attributes); + }); + + QUnit.test('hasChanged works outside of change events, and true within', function(assert) { + assert.expect(6); + var model = new Backbone.Model({x: 1}); + model.on('change:x', function() { + assert.ok(model.hasChanged('x')); + assert.equal(model.get('x'), 1); + }); + model.set({x: 2}, {silent: true}); + assert.ok(model.hasChanged()); + assert.equal(model.hasChanged('x'), true); + model.set({x: 1}); + assert.ok(model.hasChanged()); + assert.equal(model.hasChanged('x'), true); + }); + + QUnit.test('hasChanged gets cleared on the following set', function(assert) { + assert.expect(4); + var model = new Backbone.Model; + model.set({x: 1}); + assert.ok(model.hasChanged()); + model.set({x: 1}); + assert.ok(!model.hasChanged()); + model.set({x: 2}); + assert.ok(model.hasChanged()); + model.set({}); + assert.ok(!model.hasChanged()); + }); + + QUnit.test('save with `wait` succeeds without `validate`', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.url = '/test'; + model.save({x: 1}, {wait: true}); + assert.ok(this.syncArgs.model === model); + }); + + QUnit.test("save without `wait` doesn't set invalid attributes", function(assert) { + var model = new Backbone.Model(); + model.validate = function() { return 1; }; + model.save({a: 1}); + assert.equal(model.get('a'), void 0); + }); + + QUnit.test("save doesn't validate twice", function(assert) { + var model = new Backbone.Model(); + var times = 0; + model.sync = function() {}; + model.validate = function() { ++times; }; + model.save({}); + assert.equal(times, 1); + }); + + QUnit.test('`hasChanged` for falsey keys', function(assert) { + assert.expect(2); + var model = new Backbone.Model(); + model.set({x: true}, {silent: true}); + assert.ok(!model.hasChanged(0)); + assert.ok(!model.hasChanged('')); + }); + + QUnit.test('`previous` for falsey keys', function(assert) { + assert.expect(2); + var model = new Backbone.Model({'0': true, '': true}); + model.set({'0': false, '': false}, {silent: true}); + assert.equal(model.previous(0), true); + assert.equal(model.previous(''), true); + }); + + QUnit.test('`save` with `wait` sends correct attributes', function(assert) { + assert.expect(5); + var changed = 0; + var model = new Backbone.Model({x: 1, y: 2}); + model.url = '/test'; + model.on('change:x', function() { changed++; }); + model.save({x: 3}, {wait: true}); + assert.deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2}); + assert.equal(model.get('x'), 1); + assert.equal(changed, 0); + this.syncArgs.options.success({}); + assert.equal(model.get('x'), 3); + assert.equal(changed, 1); + }); + + QUnit.test("a failed `save` with `wait` doesn't leave attributes behind", function(assert) { + assert.expect(1); + var model = new Backbone.Model; + model.url = '/test'; + model.save({x: 1}, {wait: true}); + assert.equal(model.get('x'), void 0); + }); + + QUnit.test('#1030 - `save` with `wait` results in correct attributes if success is called during sync', function(assert) { + assert.expect(2); + var model = new Backbone.Model({x: 1, y: 2}); + model.sync = function(method, m, options) { + options.success(); + }; + model.on('change:x', function() { assert.ok(true); }); + model.save({x: 3}, {wait: true}); + assert.equal(model.get('x'), 3); + }); + + QUnit.test('save with wait validates attributes', function(assert) { + var model = new Backbone.Model(); + model.url = '/test'; + model.validate = function() { assert.ok(true); }; + model.save({x: 1}, {wait: true}); + }); + + QUnit.test('save turns on parse flag', function(assert) { + var Model = Backbone.Model.extend({ + sync: function(method, m, options) { assert.ok(options.parse); } + }); + new Model().save(); + }); + + QUnit.test("nested `set` during `'change:attr'`", function(assert) { + assert.expect(2); + var events = []; + var model = new Backbone.Model(); + model.on('all', function(event) { events.push(event); }); + model.on('change', function() { + model.set({z: true}, {silent: true}); + }); + model.on('change:x', function() { + model.set({y: true}); + }); + model.set({x: true}); + assert.deepEqual(events, ['change:y', 'change:x', 'change']); + events = []; + model.set({z: true}); + assert.deepEqual(events, []); + }); + + QUnit.test('nested `change` only fires once', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.on('change', function() { + assert.ok(true); + model.set({x: true}); + }); + model.set({x: true}); + }); + + QUnit.test("nested `set` during `'change'`", function(assert) { + assert.expect(6); + var count = 0; + var model = new Backbone.Model(); + model.on('change', function() { + switch (count++) { + case 0: + assert.deepEqual(this.changedAttributes(), {x: true}); + assert.equal(model.previous('x'), undefined); + model.set({y: true}); + break; + case 1: + assert.deepEqual(this.changedAttributes(), {x: true, y: true}); + assert.equal(model.previous('x'), undefined); + model.set({z: true}); + break; + case 2: + assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true}); + assert.equal(model.previous('y'), undefined); + break; + default: + assert.ok(false); + } + }); + model.set({x: true}); + }); + + QUnit.test('nested `change` with silent', function(assert) { + assert.expect(3); + var count = 0; + var model = new Backbone.Model(); + model.on('change:y', function() { assert.ok(false); }); + model.on('change', function() { + switch (count++) { + case 0: + assert.deepEqual(this.changedAttributes(), {x: true}); + model.set({y: true}, {silent: true}); + model.set({z: true}); + break; + case 1: + assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true}); + break; + case 2: + assert.deepEqual(this.changedAttributes(), {z: false}); + break; + default: + assert.ok(false); + } + }); + model.set({x: true}); + model.set({z: false}); + }); + + QUnit.test('nested `change:attr` with silent', function(assert) { + assert.expect(0); + var model = new Backbone.Model(); + model.on('change:y', function(){ assert.ok(false); }); + model.on('change', function() { + model.set({y: true}, {silent: true}); + model.set({z: true}); + }); + model.set({x: true}); + }); + + QUnit.test('multiple nested changes with silent', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.on('change:x', function() { + model.set({y: 1}, {silent: true}); + model.set({y: 2}); + }); + model.on('change:y', function(m, val) { + assert.equal(val, 2); + }); + model.set({x: true}); + }); + + QUnit.test('multiple nested changes with silent', function(assert) { + assert.expect(1); + var changes = []; + var model = new Backbone.Model(); + model.on('change:b', function(m, val) { changes.push(val); }); + model.on('change', function() { + model.set({b: 1}); + }); + model.set({b: 0}); + assert.deepEqual(changes, [0, 1]); + }); + + QUnit.test('basic silent change semantics', function(assert) { + assert.expect(1); + var model = new Backbone.Model; + model.set({x: 1}); + model.on('change', function(){ assert.ok(true); }); + model.set({x: 2}, {silent: true}); + model.set({x: 1}); + }); + + QUnit.test('nested set multiple times', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.on('change:b', function() { + assert.ok(true); + }); + model.on('change:a', function() { + model.set({b: true}); + model.set({b: true}); + }); + model.set({a: true}); + }); + + QUnit.test('#1122 - clear does not alter options.', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + var options = {}; + model.clear(options); + assert.ok(!options.unset); + }); + + QUnit.test('#1122 - unset does not alter options.', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + var options = {}; + model.unset('x', options); + assert.ok(!options.unset); + }); + + QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) { + assert.expect(3); + var model = new Backbone.Model(); + var opts = { + success: function( m, resp, options ) { + assert.ok(options); + } + }; + model.sync = function(method, m, options) { + options.success(); + }; + model.save({id: 1}, opts); + model.fetch(opts); + model.destroy(opts); + }); + + QUnit.test("#1412 - Trigger 'sync' event.", function(assert) { + assert.expect(3); + var model = new Backbone.Model({id: 1}); + model.sync = function(method, m, options) { options.success(); }; + model.on('sync', function(){ assert.ok(true); }); + model.fetch(); + model.save(); + model.destroy(); + }); + + QUnit.test('#1365 - Destroy: New models execute success callback.', function(assert) { + var done = assert.async(); + assert.expect(2); + new Backbone.Model() + .on('sync', function() { assert.ok(false); }) + .on('destroy', function(){ assert.ok(true); }) + .destroy({success: function(){ + assert.ok(true); + done(); + }}); + }); + + QUnit.test('#1433 - Save: An invalid model cannot be persisted.', function(assert) { + assert.expect(1); + var model = new Backbone.Model; + model.validate = function(){ return 'invalid'; }; + model.sync = function(){ assert.ok(false); }; + assert.strictEqual(model.save(), false); + }); + + QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + url: '/test/', + sync: function(method, m, options){ options.success(); }, + validate: function(){ return 'invalid'; } + }); + var model = new Model({id: 1}); + model.on('invalid', function(){ assert.ok(true); }); + model.save(); + }); + + QUnit.test('#1545 - `undefined` can be passed to a model constructor without coersion', function(assert) { + var Model = Backbone.Model.extend({ + defaults: {one: 1}, + initialize: function(attrs, opts) { + assert.equal(attrs, undefined); + } + }); + var emptyattrs = new Model(); + var undefinedattrs = new Model(undefined); + }); + + QUnit.test('#1478 - Model `save` does not trigger change on unchanged attributes', function(assert) { + var done = assert.async(); + assert.expect(0); + var Model = Backbone.Model.extend({ + sync: function(method, m, options) { + setTimeout(function(){ + options.success(); + done(); + }, 0); + } + }); + new Model({x: true}) + .on('change:x', function(){ assert.ok(false); }) + .save(null, {wait: true}); + }); + + QUnit.test('#1664 - Changing from one value, silently to another, back to original triggers a change.', function(assert) { + assert.expect(1); + var model = new Backbone.Model({x: 1}); + model.on('change:x', function() { assert.ok(true); }); + model.set({x: 2}, {silent: true}); + model.set({x: 3}, {silent: true}); + model.set({x: 1}); + }); + + QUnit.test('#1664 - multiple silent changes nested inside a change event', function(assert) { + assert.expect(2); + var changes = []; + var model = new Backbone.Model(); + model.on('change', function() { + model.set({a: 'c'}, {silent: true}); + model.set({b: 2}, {silent: true}); + model.unset('c', {silent: true}); + }); + model.on('change:a change:b change:c', function(m, val) { changes.push(val); }); + model.set({a: 'a', b: 1, c: 'item'}); + assert.deepEqual(changes, ['a', 1, 'item']); + assert.deepEqual(model.attributes, {a: 'c', b: 2}); + }); + + QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) { + var Model = Backbone.Model.extend({ + parse: function() { this.has('a'); } // shouldn't throw an error + }); + var model = new Model(null, {parse: true}); + assert.expect(0); + }); + + QUnit.test('silent changes in last `change` event back to original triggers change', function(assert) { + assert.expect(2); + var changes = []; + var model = new Backbone.Model(); + model.on('change:a change:b change:c', function(m, val) { changes.push(val); }); + model.on('change', function() { + model.set({a: 'c'}, {silent: true}); + }); + model.set({a: 'a'}); + assert.deepEqual(changes, ['a']); + model.set({a: 'a'}); + assert.deepEqual(changes, ['a', 'a']); + }); + + QUnit.test('#1943 change calculations should use _.isEqual', function(assert) { + var model = new Backbone.Model({a: {key: 'value'}}); + model.set('a', {key: 'value'}, {silent: true}); + assert.equal(model.changedAttributes(), false); + }); + + QUnit.test('#1964 - final `change` event is always fired, regardless of interim changes', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.on('change:property', function() { + model.set('property', 'bar'); + }); + model.on('change', function() { + assert.ok(true); + }); + model.set('property', 'foo'); + }); + + QUnit.test('isValid', function(assert) { + var model = new Backbone.Model({valid: true}); + model.validate = function(attrs) { + if (!attrs.valid) return 'invalid'; + }; + assert.equal(model.isValid(), true); + assert.equal(model.set({valid: false}, {validate: true}), false); + assert.equal(model.isValid(), true); + model.set({valid: false}); + assert.equal(model.isValid(), false); + assert.ok(!model.set('valid', false, {validate: true})); + }); + + QUnit.test('#1179 - isValid returns true in the absence of validate.', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.validate = null; + assert.ok(model.isValid()); + }); + + QUnit.test('#1961 - Creating a model with {validate:true} will call validate and use the error callback', function(assert) { + var Model = Backbone.Model.extend({ + validate: function(attrs) { + if (attrs.id === 1) return "This shouldn't happen"; + } + }); + var model = new Model({id: 1}, {validate: true}); + assert.equal(model.validationError, "This shouldn't happen"); + }); + + QUnit.test('toJSON receives attrs during save(..., {wait: true})', function(assert) { + assert.expect(1); + var Model = Backbone.Model.extend({ + url: '/test', + toJSON: function() { + assert.strictEqual(this.attributes.x, 1); + return _.clone(this.attributes); + } + }); + var model = new Model; + model.save({x: 1}, {wait: true}); + }); + + QUnit.test('#2034 - nested set with silent only triggers one change', function(assert) { + assert.expect(1); + var model = new Backbone.Model(); + model.on('change', function() { + model.set({b: true}, {silent: true}); + assert.ok(true); + }); + model.set({a: true}); + }); + + QUnit.test('#3778 - id will only be updated if it is set', function(assert) { + assert.expect(2); + var model = new Backbone.Model({id: 1}); + model.id = 2; + model.set({foo: 'bar'}); + assert.equal(model.id, 2); + model.set({id: 3}); + assert.equal(model.id, 3); + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/noconflict.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/noconflict.js new file mode 100644 index 00000000..9968f688 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/noconflict.js @@ -0,0 +1,13 @@ +(function() { + + QUnit.module('Backbone.noConflict'); + + QUnit.test('noConflict', function(assert) { + assert.expect(2); + var noconflictBackbone = Backbone.noConflict(); + assert.equal(window.Backbone, undefined, 'Returned window.Backbone'); + window.Backbone = noconflictBackbone; + assert.equal(window.Backbone, noconflictBackbone, 'Backbone is still pointing to the original Backbone'); + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/router.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/router.js new file mode 100644 index 00000000..13110c45 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/router.js @@ -0,0 +1,1062 @@ +(function() { + + var router = null; + var location = null; + var lastRoute = null; + var lastArgs = []; + + var onRoute = function(routerParam, route, args) { + lastRoute = route; + lastArgs = args; + }; + + var Location = function(href) { + this.replace(href); + }; + + _.extend(Location.prototype, { + + parser: document.createElement('a'), + + replace: function(href) { + this.parser.href = href; + _.extend(this, _.pick(this.parser, + 'href', + 'hash', + 'host', + 'search', + 'fragment', + 'pathname', + 'protocol' + )); + // In IE, anchor.pathname does not contain a leading slash though + // window.location.pathname does. + if (!/^\//.test(this.pathname)) this.pathname = '/' + this.pathname; + }, + + toString: function() { + return this.href; + } + + }); + + QUnit.module('Backbone.Router', { + + setup: function() { + location = new Location('http://example.com'); + Backbone.history = _.extend(new Backbone.History, {location: location}); + router = new Router({testing: 101}); + Backbone.history.interval = 9; + Backbone.history.start({pushState: false}); + lastRoute = null; + lastArgs = []; + Backbone.history.on('route', onRoute); + }, + + teardown: function() { + Backbone.history.stop(); + Backbone.history.off('route', onRoute); + } + + }); + + var ExternalObject = { + value: 'unset', + + routingFunction: function(value) { + this.value = value; + } + }; + ExternalObject.routingFunction = _.bind(ExternalObject.routingFunction, ExternalObject); + + var Router = Backbone.Router.extend({ + + count: 0, + + routes: { + 'noCallback': 'noCallback', + 'counter': 'counter', + 'search/:query': 'search', + 'search/:query/p:page': 'search', + 'charñ': 'charUTF', + 'char%C3%B1': 'charEscaped', + 'contacts': 'contacts', + 'contacts/new': 'newContact', + 'contacts/:id': 'loadContact', + 'route-event/:arg': 'routeEvent', + 'optional(/:item)': 'optionalItem', + 'named/optional/(y:z)': 'namedOptional', + 'splat/*args/end': 'splat', + ':repo/compare/*from...*to': 'github', + 'decode/:named/*splat': 'decode', + '*first/complex-*part/*rest': 'complex', + 'query/:entity': 'query', + 'function/:value': ExternalObject.routingFunction, + '*anything': 'anything' + }, + + initialize: function(options) { + this.testing = options.testing; + this.route('implicit', 'implicit'); + }, + + counter: function() { + this.count++; + }, + + implicit: function() { + this.count++; + }, + + search: function(query, page) { + this.query = query; + this.page = page; + }, + + charUTF: function() { + this.charType = 'UTF'; + }, + + charEscaped: function() { + this.charType = 'escaped'; + }, + + contacts: function(){ + this.contact = 'index'; + }, + + newContact: function(){ + this.contact = 'new'; + }, + + loadContact: function(){ + this.contact = 'load'; + }, + + optionalItem: function(arg){ + this.arg = arg !== void 0 ? arg : null; + }, + + splat: function(args) { + this.args = args; + }, + + github: function(repo, from, to) { + this.repo = repo; + this.from = from; + this.to = to; + }, + + complex: function(first, part, rest) { + this.first = first; + this.part = part; + this.rest = rest; + }, + + query: function(entity, args) { + this.entity = entity; + this.queryArgs = args; + }, + + anything: function(whatever) { + this.anything = whatever; + }, + + namedOptional: function(z) { + this.z = z; + }, + + decode: function(named, path) { + this.named = named; + this.path = path; + }, + + routeEvent: function(arg) { + } + + }); + + QUnit.test('initialize', function(assert) { + assert.expect(1); + assert.equal(router.testing, 101); + }); + + QUnit.test('routes (simple)', function(assert) { + assert.expect(4); + location.replace('http://example.com#search/news'); + Backbone.history.checkUrl(); + assert.equal(router.query, 'news'); + assert.equal(router.page, void 0); + assert.equal(lastRoute, 'search'); + assert.equal(lastArgs[0], 'news'); + }); + + QUnit.test('routes (simple, but unicode)', function(assert) { + assert.expect(4); + location.replace('http://example.com#search/тест'); + Backbone.history.checkUrl(); + assert.equal(router.query, 'тест'); + assert.equal(router.page, void 0); + assert.equal(lastRoute, 'search'); + assert.equal(lastArgs[0], 'тест'); + }); + + QUnit.test('routes (two part)', function(assert) { + assert.expect(2); + location.replace('http://example.com#search/nyc/p10'); + Backbone.history.checkUrl(); + assert.equal(router.query, 'nyc'); + assert.equal(router.page, '10'); + }); + + QUnit.test('routes via navigate', function(assert) { + assert.expect(2); + Backbone.history.navigate('search/manhattan/p20', {trigger: true}); + assert.equal(router.query, 'manhattan'); + assert.equal(router.page, '20'); + }); + + QUnit.test('routes via navigate with params', function(assert) { + assert.expect(1); + Backbone.history.navigate('query/test?a=b', {trigger: true}); + assert.equal(router.queryArgs, 'a=b'); + }); + + QUnit.test('routes via navigate for backwards-compatibility', function(assert) { + assert.expect(2); + Backbone.history.navigate('search/manhattan/p20', true); + assert.equal(router.query, 'manhattan'); + assert.equal(router.page, '20'); + }); + + QUnit.test('reports matched route via nagivate', function(assert) { + assert.expect(1); + assert.ok(Backbone.history.navigate('search/manhattan/p20', true)); + }); + + QUnit.test('route precedence via navigate', function(assert){ + assert.expect(6); + // check both 0.9.x and backwards-compatibility options + _.each([{trigger: true}, true], function( options ){ + Backbone.history.navigate('contacts', options); + assert.equal(router.contact, 'index'); + Backbone.history.navigate('contacts/new', options); + assert.equal(router.contact, 'new'); + Backbone.history.navigate('contacts/foo', options); + assert.equal(router.contact, 'load'); + }); + }); + + QUnit.test('loadUrl is not called for identical routes.', function(assert) { + assert.expect(0); + Backbone.history.loadUrl = function(){ assert.ok(false); }; + location.replace('http://example.com#route'); + Backbone.history.navigate('route'); + Backbone.history.navigate('/route'); + Backbone.history.navigate('/route'); + }); + + QUnit.test('use implicit callback if none provided', function(assert) { + assert.expect(1); + router.count = 0; + router.navigate('implicit', {trigger: true}); + assert.equal(router.count, 1); + }); + + QUnit.test('routes via navigate with {replace: true}', function(assert) { + assert.expect(1); + location.replace('http://example.com#start_here'); + Backbone.history.checkUrl(); + location.replace = function(href) { + assert.strictEqual(href, new Location('http://example.com#end_here').href); + }; + Backbone.history.navigate('end_here', {replace: true}); + }); + + QUnit.test('routes (splats)', function(assert) { + assert.expect(1); + location.replace('http://example.com#splat/long-list/of/splatted_99args/end'); + Backbone.history.checkUrl(); + assert.equal(router.args, 'long-list/of/splatted_99args'); + }); + + QUnit.test('routes (github)', function(assert) { + assert.expect(3); + location.replace('http://example.com#backbone/compare/1.0...braddunbar:with/slash'); + Backbone.history.checkUrl(); + assert.equal(router.repo, 'backbone'); + assert.equal(router.from, '1.0'); + assert.equal(router.to, 'braddunbar:with/slash'); + }); + + QUnit.test('routes (optional)', function(assert) { + assert.expect(2); + location.replace('http://example.com#optional'); + Backbone.history.checkUrl(); + assert.ok(!router.arg); + location.replace('http://example.com#optional/thing'); + Backbone.history.checkUrl(); + assert.equal(router.arg, 'thing'); + }); + + QUnit.test('routes (complex)', function(assert) { + assert.expect(3); + location.replace('http://example.com#one/two/three/complex-part/four/five/six/seven'); + Backbone.history.checkUrl(); + assert.equal(router.first, 'one/two/three'); + assert.equal(router.part, 'part'); + assert.equal(router.rest, 'four/five/six/seven'); + }); + + QUnit.test('routes (query)', function(assert) { + assert.expect(5); + location.replace('http://example.com#query/mandel?a=b&c=d'); + Backbone.history.checkUrl(); + assert.equal(router.entity, 'mandel'); + assert.equal(router.queryArgs, 'a=b&c=d'); + assert.equal(lastRoute, 'query'); + assert.equal(lastArgs[0], 'mandel'); + assert.equal(lastArgs[1], 'a=b&c=d'); + }); + + QUnit.test('routes (anything)', function(assert) { + assert.expect(1); + location.replace('http://example.com#doesnt-match-a-route'); + Backbone.history.checkUrl(); + assert.equal(router.anything, 'doesnt-match-a-route'); + }); + + QUnit.test('routes (function)', function(assert) { + assert.expect(3); + router.on('route', function(name) { + assert.ok(name === ''); + }); + assert.equal(ExternalObject.value, 'unset'); + location.replace('http://example.com#function/set'); + Backbone.history.checkUrl(); + assert.equal(ExternalObject.value, 'set'); + }); + + QUnit.test('Decode named parameters, not splats.', function(assert) { + assert.expect(2); + location.replace('http://example.com#decode/a%2Fb/c%2Fd/e'); + Backbone.history.checkUrl(); + assert.strictEqual(router.named, 'a/b'); + assert.strictEqual(router.path, 'c/d/e'); + }); + + QUnit.test("fires event when router doesn't have callback on it", function(assert) { + assert.expect(1); + router.on('route:noCallback', function(){ assert.ok(true); }); + location.replace('http://example.com#noCallback'); + Backbone.history.checkUrl(); + }); + + QUnit.test('No events are triggered if #execute returns false.', function(assert) { + assert.expect(1); + var MyRouter = Backbone.Router.extend({ + + routes: { + foo: function() { + assert.ok(true); + } + }, + + execute: function(callback, args) { + callback.apply(this, args); + return false; + } + + }); + + var myRouter = new MyRouter; + + myRouter.on('route route:foo', function() { + assert.ok(false); + }); + + Backbone.history.on('route', function() { + assert.ok(false); + }); + + location.replace('http://example.com#foo'); + Backbone.history.checkUrl(); + }); + + QUnit.test('#933, #908 - leading slash', function(assert) { + assert.expect(2); + location.replace('http://example.com/root/foo'); + + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({root: '/root', hashChange: false, silent: true}); + assert.strictEqual(Backbone.history.getFragment(), 'foo'); + + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({root: '/root/', hashChange: false, silent: true}); + assert.strictEqual(Backbone.history.getFragment(), 'foo'); + }); + + QUnit.test('#967 - Route callback gets passed encoded values.', function(assert) { + assert.expect(3); + var route = 'has%2Fslash/complex-has%23hash/has%20space'; + Backbone.history.navigate(route, {trigger: true}); + assert.strictEqual(router.first, 'has/slash'); + assert.strictEqual(router.part, 'has#hash'); + assert.strictEqual(router.rest, 'has space'); + }); + + QUnit.test('correctly handles URLs with % (#868)', function(assert) { + assert.expect(3); + location.replace('http://example.com#search/fat%3A1.5%25'); + Backbone.history.checkUrl(); + location.replace('http://example.com#search/fat'); + Backbone.history.checkUrl(); + assert.equal(router.query, 'fat'); + assert.equal(router.page, void 0); + assert.equal(lastRoute, 'search'); + }); + + QUnit.test('#2666 - Hashes with UTF8 in them.', function(assert) { + assert.expect(2); + Backbone.history.navigate('charñ', {trigger: true}); + assert.equal(router.charType, 'UTF'); + Backbone.history.navigate('char%C3%B1', {trigger: true}); + assert.equal(router.charType, 'UTF'); + }); + + QUnit.test('#1185 - Use pathname when hashChange is not wanted.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/path/name#hash'); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({hashChange: false}); + var fragment = Backbone.history.getFragment(); + assert.strictEqual(fragment, location.pathname.replace(/^\//, '')); + }); + + QUnit.test('#1206 - Strip leading slash before location.assign.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root/'); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({hashChange: false, root: '/root/'}); + location.assign = function(pathname) { + assert.strictEqual(pathname, '/root/fragment'); + }; + Backbone.history.navigate('/fragment'); + }); + + QUnit.test('#1387 - Root fragment without trailing slash.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root'); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({hashChange: false, root: '/root/', silent: true}); + assert.strictEqual(Backbone.history.getFragment(), ''); + }); + + QUnit.test('#1366 - History does not prepend root to fragment.', function(assert) { + assert.expect(2); + Backbone.history.stop(); + location.replace('http://example.com/root/'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url) { + assert.strictEqual(url, '/root/x'); + } + } + }); + Backbone.history.start({ + root: '/root/', + pushState: true, + hashChange: false + }); + Backbone.history.navigate('x'); + assert.strictEqual(Backbone.history.fragment, 'x'); + }); + + QUnit.test('Normalize root.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url) { + assert.strictEqual(url, '/root/fragment'); + } + } + }); + Backbone.history.start({ + pushState: true, + root: '/root', + hashChange: false + }); + Backbone.history.navigate('fragment'); + }); + + QUnit.test('Normalize root.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root#fragment'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url) {}, + replaceState: function(state, title, url) { + assert.strictEqual(url, '/root/fragment'); + } + } + }); + Backbone.history.start({ + pushState: true, + root: '/root' + }); + }); + + QUnit.test('Normalize root.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root'); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.loadUrl = function() { assert.ok(true); }; + Backbone.history.start({ + pushState: true, + root: '/root' + }); + }); + + QUnit.test('Normalize root - leading slash.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){}, + replaceState: function(){} + } + }); + Backbone.history.start({root: 'root'}); + assert.strictEqual(Backbone.history.root, '/root/'); + }); + + QUnit.test('Transition from hashChange to pushState.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root#x/y'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){}, + replaceState: function(state, title, url){ + assert.strictEqual(url, '/root/x/y'); + } + } + }); + Backbone.history.start({ + root: 'root', + pushState: true + }); + }); + + QUnit.test('#1619: Router: Normalize empty root', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){}, + replaceState: function(){} + } + }); + Backbone.history.start({root: ''}); + assert.strictEqual(Backbone.history.root, '/'); + }); + + QUnit.test('#1619: Router: nagivate with empty root', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url) { + assert.strictEqual(url, '/fragment'); + } + } + }); + Backbone.history.start({ + pushState: true, + root: '', + hashChange: false + }); + Backbone.history.navigate('fragment'); + }); + + QUnit.test('Transition from pushState to hashChange.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root/x/y?a=b'); + location.replace = function(url) { + assert.strictEqual(url, '/root#x/y?a=b'); + }; + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: null, + replaceState: null + } + }); + Backbone.history.start({ + root: 'root', + pushState: true + }); + }); + + QUnit.test('#1695 - hashChange to pushState with search.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root#x/y?a=b'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){}, + replaceState: function(state, title, url){ + assert.strictEqual(url, '/root/x/y?a=b'); + } + } + }); + Backbone.history.start({ + root: 'root', + pushState: true + }); + }); + + QUnit.test('#1746 - Router allows empty route.', function(assert) { + assert.expect(1); + var MyRouter = Backbone.Router.extend({ + routes: {'': 'empty'}, + empty: function(){}, + route: function(route){ + assert.strictEqual(route, ''); + } + }); + new MyRouter; + }); + + QUnit.test('#1794 - Trailing space in fragments.', function(assert) { + assert.expect(1); + var history = new Backbone.History; + assert.strictEqual(history.getFragment('fragment '), 'fragment'); + }); + + QUnit.test('#1820 - Leading slash and trailing space.', 1, function(assert) { + var history = new Backbone.History; + assert.strictEqual(history.getFragment('/fragment '), 'fragment'); + }); + + QUnit.test('#1980 - Optional parameters.', function(assert) { + assert.expect(2); + location.replace('http://example.com#named/optional/y'); + Backbone.history.checkUrl(); + assert.strictEqual(router.z, undefined); + location.replace('http://example.com#named/optional/y123'); + Backbone.history.checkUrl(); + assert.strictEqual(router.z, '123'); + }); + + QUnit.test("#2062 - Trigger 'route' event on router instance.", function(assert) { + assert.expect(2); + router.on('route', function(name, args) { + assert.strictEqual(name, 'routeEvent'); + assert.deepEqual(args, ['x', null]); + }); + location.replace('http://example.com#route-event/x'); + Backbone.history.checkUrl(); + }); + + QUnit.test('#2255 - Extend routes by making routes a function.', function(assert) { + assert.expect(1); + var RouterBase = Backbone.Router.extend({ + routes: function() { + return { + home: 'root', + index: 'index.html' + }; + } + }); + + var RouterExtended = RouterBase.extend({ + routes: function() { + var _super = RouterExtended.__super__.routes; + return _.extend(_super(), {show: 'show', search: 'search'}); + } + }); + + var myRouter = new RouterExtended(); + assert.deepEqual({home: 'root', index: 'index.html', show: 'show', search: 'search'}, myRouter.routes); + }); + + QUnit.test('#2538 - hashChange to pushState only if both requested.', function(assert) { + assert.expect(0); + Backbone.history.stop(); + location.replace('http://example.com/root?a=b#x/y'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){}, + replaceState: function(){ assert.ok(false); } + } + }); + Backbone.history.start({ + root: 'root', + pushState: true, + hashChange: false + }); + }); + + QUnit.test('No hash fallback.', function(assert) { + assert.expect(0); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){}, + replaceState: function(){} + } + }); + + var MyRouter = Backbone.Router.extend({ + routes: { + hash: function() { assert.ok(false); } + } + }); + var myRouter = new MyRouter; + + location.replace('http://example.com/'); + Backbone.history.start({ + pushState: true, + hashChange: false + }); + location.replace('http://example.com/nomatch#hash'); + Backbone.history.checkUrl(); + }); + + QUnit.test('#2656 - No trailing slash on root.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url){ + assert.strictEqual(url, '/root'); + } + } + }); + location.replace('http://example.com/root/path'); + Backbone.history.start({pushState: true, hashChange: false, root: 'root'}); + Backbone.history.navigate(''); + }); + + QUnit.test('#2656 - No trailing slash on root.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url) { + assert.strictEqual(url, '/'); + } + } + }); + location.replace('http://example.com/path'); + Backbone.history.start({pushState: true, hashChange: false}); + Backbone.history.navigate(''); + }); + + QUnit.test('#2656 - No trailing slash on root.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url){ + assert.strictEqual(url, '/root?x=1'); + } + } + }); + location.replace('http://example.com/root/path'); + Backbone.history.start({pushState: true, hashChange: false, root: 'root'}); + Backbone.history.navigate('?x=1'); + }); + + QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) { + assert.expect(2); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(state, title, url) { + assert.strictEqual(url, '/path?query#hash'); + } + } + }); + + var MyRouter = Backbone.Router.extend({ + routes: { + path: function() { assert.ok(true); } + } + }); + var myRouter = new MyRouter; + + location.replace('http://example.com/'); + Backbone.history.start({pushState: true, hashChange: false}); + Backbone.history.navigate('path?query#hash', true); + }); + + QUnit.test('Do not decode the search params.', function(assert) { + assert.expect(1); + var MyRouter = Backbone.Router.extend({ + routes: { + path: function(params){ + assert.strictEqual(params, 'x=y%3Fz'); + } + } + }); + var myRouter = new MyRouter; + Backbone.history.navigate('path?x=y%3Fz', true); + }); + + QUnit.test('Navigate to a hash url.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({pushState: true}); + var MyRouter = Backbone.Router.extend({ + routes: { + path: function(params) { + assert.strictEqual(params, 'x=y'); + } + } + }); + var myRouter = new MyRouter; + location.replace('http://example.com/path?x=y#hash'); + Backbone.history.checkUrl(); + }); + + QUnit.test('#navigate to a hash url.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + Backbone.history.start({pushState: true}); + var MyRouter = Backbone.Router.extend({ + routes: { + path: function(params) { + assert.strictEqual(params, 'x=y'); + } + } + }); + var myRouter = new MyRouter; + Backbone.history.navigate('path?x=y#hash', true); + }); + + QUnit.test('unicode pathname', function(assert) { + assert.expect(1); + location.replace('http://example.com/myyjä'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: { + myyjä: function() { + assert.ok(true); + } + } + }); + new MyRouter; + Backbone.history.start({pushState: true}); + }); + + QUnit.test('unicode pathname with % in a parameter', function(assert) { + assert.expect(1); + location.replace('http://example.com/myyjä/foo%20%25%3F%2f%40%25%20bar'); + location.pathname = '/myyj%C3%A4/foo%20%25%3F%2f%40%25%20bar'; + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: { + 'myyjä/:query': function(query) { + assert.strictEqual(query, 'foo %?/@% bar'); + } + } + }); + new MyRouter; + Backbone.history.start({pushState: true}); + }); + + QUnit.test('newline in route', function(assert) { + assert.expect(1); + location.replace('http://example.com/stuff%0Anonsense?param=foo%0Abar'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: { + 'stuff\nnonsense': function() { + assert.ok(true); + } + } + }); + new MyRouter; + Backbone.history.start({pushState: true}); + }); + + QUnit.test('Router#execute receives callback, args, name.', function(assert) { + assert.expect(3); + location.replace('http://example.com#foo/123/bar?x=y'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: {'foo/:id/bar': 'foo'}, + foo: function(){}, + execute: function(callback, args, name) { + assert.strictEqual(callback, this.foo); + assert.deepEqual(args, ['123', 'x=y']); + assert.strictEqual(name, 'foo'); + } + }); + var myRouter = new MyRouter; + Backbone.history.start(); + }); + + QUnit.test('pushState to hashChange with only search params.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com?a=b'); + location.replace = function(url) { + assert.strictEqual(url, '/#?a=b'); + }; + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: null + }); + Backbone.history.start({pushState: true}); + }); + + QUnit.test('#3123 - History#navigate decodes before comparison.', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/shop/search?keyword=short%20dress'); + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: function(){ assert.ok(false); }, + replaceState: function(){ assert.ok(false); } + } + }); + Backbone.history.start({pushState: true}); + Backbone.history.navigate('shop/search?keyword=short%20dress', true); + assert.strictEqual(Backbone.history.fragment, 'shop/search?keyword=short dress'); + }); + + QUnit.test('#3175 - Urls in the params', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com#login?a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db'); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var myRouter = new Backbone.Router; + myRouter.route('login', function(params) { + assert.strictEqual(params, 'a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db'); + }); + Backbone.history.start(); + }); + + QUnit.test('#3358 - pushState to hashChange transition with search params', function(assert) { + assert.expect(1); + Backbone.history.stop(); + location.replace('http://example.com/root?foo=bar'); + location.replace = function(url) { + assert.strictEqual(url, '/root#?foo=bar'); + }; + Backbone.history = _.extend(new Backbone.History, { + location: location, + history: { + pushState: undefined, + replaceState: undefined + } + }); + Backbone.history.start({root: '/root', pushState: true}); + }); + + QUnit.test("Paths that don't match the root should not match no root", function(assert) { + assert.expect(0); + location.replace('http://example.com/foo'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: { + foo: function(){ + assert.ok(false, 'should not match unless root matches'); + } + } + }); + var myRouter = new MyRouter; + Backbone.history.start({root: 'root', pushState: true}); + }); + + QUnit.test("Paths that don't match the root should not match roots of the same length", function(assert) { + assert.expect(0); + location.replace('http://example.com/xxxx/foo'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: { + foo: function(){ + assert.ok(false, 'should not match unless root matches'); + } + } + }); + var myRouter = new MyRouter; + Backbone.history.start({root: 'root', pushState: true}); + }); + + QUnit.test('roots with regex characters', function(assert) { + assert.expect(1); + location.replace('http://example.com/x+y.z/foo'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: {foo: function(){ assert.ok(true); }} + }); + var myRouter = new MyRouter; + Backbone.history.start({root: 'x+y.z', pushState: true}); + }); + + QUnit.test('roots with unicode characters', function(assert) { + assert.expect(1); + location.replace('http://example.com/®ooτ/foo'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: {foo: function(){ assert.ok(true); }} + }); + var myRouter = new MyRouter; + Backbone.history.start({root: '®ooτ', pushState: true}); + }); + + QUnit.test('roots without slash', function(assert) { + assert.expect(1); + location.replace('http://example.com/®ooτ'); + Backbone.history.stop(); + Backbone.history = _.extend(new Backbone.History, {location: location}); + var MyRouter = Backbone.Router.extend({ + routes: {'': function(){ assert.ok(true); }} + }); + var myRouter = new MyRouter; + Backbone.history.start({root: '®ooτ', pushState: true}); + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/setup/dom-setup.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/setup/dom-setup.js new file mode 100644 index 00000000..f2242282 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/setup/dom-setup.js @@ -0,0 +1,4 @@ +$('body').append( + '<div id="qunit"></div>' + + '<div id="qunit-fixture"></div>' +); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/setup/environment.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/setup/environment.js new file mode 100644 index 00000000..c2441ac7 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/setup/environment.js @@ -0,0 +1,45 @@ +(function() { + + var sync = Backbone.sync; + var ajax = Backbone.ajax; + var emulateHTTP = Backbone.emulateHTTP; + var emulateJSON = Backbone.emulateJSON; + var history = window.history; + var pushState = history.pushState; + var replaceState = history.replaceState; + + QUnit.config.noglobals = true; + + QUnit.testStart(function() { + var env = QUnit.config.current.testEnvironment; + + // We never want to actually call these during tests. + history.pushState = history.replaceState = function(){}; + + // Capture ajax settings for comparison. + Backbone.ajax = function(settings) { + env.ajaxSettings = settings; + }; + + // Capture the arguments to Backbone.sync for comparison. + Backbone.sync = function(method, model, options) { + env.syncArgs = { + method: method, + model: model, + options: options + }; + sync.apply(this, arguments); + }; + + }); + + QUnit.testDone(function() { + Backbone.sync = sync; + Backbone.ajax = ajax; + Backbone.emulateHTTP = emulateHTTP; + Backbone.emulateJSON = emulateJSON; + history.pushState = pushState; + history.replaceState = replaceState; + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/sync.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/sync.js new file mode 100644 index 00000000..8813f158 --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/sync.js @@ -0,0 +1,239 @@ +(function() { + + var Library = Backbone.Collection.extend({ + url: function() { return '/library'; } + }); + var library; + + var attrs = { + title: 'The Tempest', + author: 'Bill Shakespeare', + length: 123 + }; + + QUnit.module('Backbone.sync', { + + beforeEach: function(assert) { + library = new Library; + library.create(attrs, {wait: false}); + }, + + afterEach: function(assert) { + Backbone.emulateHTTP = false; + } + + }); + + QUnit.test('read', function(assert) { + assert.expect(4); + library.fetch(); + assert.equal(this.ajaxSettings.url, '/library'); + assert.equal(this.ajaxSettings.type, 'GET'); + assert.equal(this.ajaxSettings.dataType, 'json'); + assert.ok(_.isEmpty(this.ajaxSettings.data)); + }); + + QUnit.test('passing data', function(assert) { + assert.expect(3); + library.fetch({data: {a: 'a', one: 1}}); + assert.equal(this.ajaxSettings.url, '/library'); + assert.equal(this.ajaxSettings.data.a, 'a'); + assert.equal(this.ajaxSettings.data.one, 1); + }); + + QUnit.test('create', function(assert) { + assert.expect(6); + assert.equal(this.ajaxSettings.url, '/library'); + assert.equal(this.ajaxSettings.type, 'POST'); + assert.equal(this.ajaxSettings.dataType, 'json'); + var data = JSON.parse(this.ajaxSettings.data); + assert.equal(data.title, 'The Tempest'); + assert.equal(data.author, 'Bill Shakespeare'); + assert.equal(data.length, 123); + }); + + QUnit.test('update', function(assert) { + assert.expect(7); + library.first().save({id: '1-the-tempest', author: 'William Shakespeare'}); + assert.equal(this.ajaxSettings.url, '/library/1-the-tempest'); + assert.equal(this.ajaxSettings.type, 'PUT'); + assert.equal(this.ajaxSettings.dataType, 'json'); + var data = JSON.parse(this.ajaxSettings.data); + assert.equal(data.id, '1-the-tempest'); + assert.equal(data.title, 'The Tempest'); + assert.equal(data.author, 'William Shakespeare'); + assert.equal(data.length, 123); + }); + + QUnit.test('update with emulateHTTP and emulateJSON', function(assert) { + assert.expect(7); + library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, { + emulateHTTP: true, + emulateJSON: true + }); + assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); + assert.equal(this.ajaxSettings.type, 'POST'); + assert.equal(this.ajaxSettings.dataType, 'json'); + assert.equal(this.ajaxSettings.data._method, 'PUT'); + var data = JSON.parse(this.ajaxSettings.data.model); + assert.equal(data.id, '2-the-tempest'); + assert.equal(data.author, 'Tim Shakespeare'); + assert.equal(data.length, 123); + }); + + QUnit.test('update with just emulateHTTP', function(assert) { + assert.expect(6); + library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, { + emulateHTTP: true + }); + assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); + assert.equal(this.ajaxSettings.type, 'POST'); + assert.equal(this.ajaxSettings.contentType, 'application/json'); + var data = JSON.parse(this.ajaxSettings.data); + assert.equal(data.id, '2-the-tempest'); + assert.equal(data.author, 'Tim Shakespeare'); + assert.equal(data.length, 123); + }); + + QUnit.test('update with just emulateJSON', function(assert) { + assert.expect(6); + library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, { + emulateJSON: true + }); + assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); + assert.equal(this.ajaxSettings.type, 'PUT'); + assert.equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded'); + var data = JSON.parse(this.ajaxSettings.data.model); + assert.equal(data.id, '2-the-tempest'); + assert.equal(data.author, 'Tim Shakespeare'); + assert.equal(data.length, 123); + }); + + QUnit.test('read model', function(assert) { + assert.expect(3); + library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); + library.first().fetch(); + assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); + assert.equal(this.ajaxSettings.type, 'GET'); + assert.ok(_.isEmpty(this.ajaxSettings.data)); + }); + + QUnit.test('destroy', function(assert) { + assert.expect(3); + library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); + library.first().destroy({wait: true}); + assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); + assert.equal(this.ajaxSettings.type, 'DELETE'); + assert.equal(this.ajaxSettings.data, null); + }); + + QUnit.test('destroy with emulateHTTP', function(assert) { + assert.expect(3); + library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); + library.first().destroy({ + emulateHTTP: true, + emulateJSON: true + }); + assert.equal(this.ajaxSettings.url, '/library/2-the-tempest'); + assert.equal(this.ajaxSettings.type, 'POST'); + assert.equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}'); + }); + + QUnit.test('urlError', function(assert) { + assert.expect(2); + var model = new Backbone.Model(); + assert.raises(function() { + model.fetch(); + }); + model.fetch({url: '/one/two'}); + assert.equal(this.ajaxSettings.url, '/one/two'); + }); + + QUnit.test('#1052 - `options` is optional.', function(assert) { + assert.expect(0); + var model = new Backbone.Model(); + model.url = '/test'; + Backbone.sync('create', model); + }); + + QUnit.test('Backbone.ajax', function(assert) { + assert.expect(1); + Backbone.ajax = function(settings){ + assert.strictEqual(settings.url, '/test'); + }; + var model = new Backbone.Model(); + model.url = '/test'; + Backbone.sync('create', model); + }); + + QUnit.test('Call provided error callback on error.', function(assert) { + assert.expect(1); + var model = new Backbone.Model; + model.url = '/test'; + Backbone.sync('read', model, { + error: function() { assert.ok(true); } + }); + this.ajaxSettings.error(); + }); + + QUnit.test('Use Backbone.emulateHTTP as default.', function(assert) { + assert.expect(2); + var model = new Backbone.Model; + model.url = '/test'; + + Backbone.emulateHTTP = true; + model.sync('create', model); + assert.strictEqual(this.ajaxSettings.emulateHTTP, true); + + Backbone.emulateHTTP = false; + model.sync('create', model); + assert.strictEqual(this.ajaxSettings.emulateHTTP, false); + }); + + QUnit.test('Use Backbone.emulateJSON as default.', function(assert) { + assert.expect(2); + var model = new Backbone.Model; + model.url = '/test'; + + Backbone.emulateJSON = true; + model.sync('create', model); + assert.strictEqual(this.ajaxSettings.emulateJSON, true); + + Backbone.emulateJSON = false; + model.sync('create', model); + assert.strictEqual(this.ajaxSettings.emulateJSON, false); + }); + + QUnit.test('#1756 - Call user provided beforeSend function.', function(assert) { + assert.expect(4); + Backbone.emulateHTTP = true; + var model = new Backbone.Model; + model.url = '/test'; + var xhr = { + setRequestHeader: function(header, value) { + assert.strictEqual(header, 'X-HTTP-Method-Override'); + assert.strictEqual(value, 'DELETE'); + } + }; + model.sync('delete', model, { + beforeSend: function(_xhr) { + assert.ok(_xhr === xhr); + return false; + } + }); + assert.strictEqual(this.ajaxSettings.beforeSend(xhr), false); + }); + + QUnit.test('#2928 - Pass along `textStatus` and `errorThrown`.', function(assert) { + assert.expect(2); + var model = new Backbone.Model; + model.url = '/test'; + model.on('error', function(m, xhr, options) { + assert.strictEqual(options.textStatus, 'textStatus'); + assert.strictEqual(options.errorThrown, 'errorThrown'); + }); + model.fetch(); + this.ajaxSettings.error({}, 'textStatus', 'errorThrown'); + }); + +})(); diff --git a/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/view.js b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/view.js new file mode 100644 index 00000000..faf3445b --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/lodash/vendor/backbone/test/view.js @@ -0,0 +1,495 @@ +(function() { + + var view; + + QUnit.module('Backbone.View', { + + beforeEach: function(assert) { + $('#qunit-fixture').append( + '<div id="testElement"><h1>Test</h1></div>' + ); + + view = new Backbone.View({ + id: 'test-view', + className: 'test-view', + other: 'non-special-option' + }); + }, + + afterEach: function() { + $('#testElement').remove(); + $('#test-view').remove(); + } + + }); + + QUnit.test('constructor', function(assert) { + assert.expect(3); + assert.equal(view.el.id, 'test-view'); + assert.equal(view.el.className, 'test-view'); + assert.equal(view.el.other, void 0); + }); + + QUnit.test('$', function(assert) { + assert.expect(2); + var myView = new Backbone.View; + myView.setElement('<p><a><b>test</b></a></p>'); + var result = myView.$('a b'); + + assert.strictEqual(result[0].innerHTML, 'test'); + assert.ok(result.length === +result.length); + }); + + QUnit.test('$el', function(assert) { + assert.expect(3); + var myView = new Backbone.View; + myView.setElement('<p><a><b>test</b></a></p>'); + assert.strictEqual(myView.el.nodeType, 1); + + assert.ok(myView.$el instanceof Backbone.$); + assert.strictEqual(myView.$el[0], myView.el); + }); + + QUnit.test('initialize', function(assert) { + assert.expect(1); + var View = Backbone.View.extend({ + initialize: function() { + this.one = 1; + } + }); + + assert.strictEqual(new View().one, 1); + }); + + QUnit.test('render', function(assert) { + assert.expect(1); + var myView = new Backbone.View; + assert.equal(myView.render(), myView, '#render returns the view instance'); + }); + + QUnit.test('delegateEvents', function(assert) { + assert.expect(6); + var counter1 = 0, counter2 = 0; + + var myView = new Backbone.View({el: '#testElement'}); + myView.increment = function(){ counter1++; }; + myView.$el.on('click', function(){ counter2++; }); + + var events = {'click h1': 'increment'}; + + myView.delegateEvents(events); + myView.$('h1').trigger('click'); + assert.equal(counter1, 1); + assert.equal(counter2, 1); + + myView.$('h1').trigger('click'); + assert.equal(counter1, 2); + assert.equal(counter2, 2); + + myView.delegateEvents(events); + myView.$('h1').trigger('click'); + assert.equal(counter1, 3); + assert.equal(counter2, 3); + }); + + QUnit.test('delegate', function(assert) { + assert.expect(3); + var myView = new Backbone.View({el: '#testElement'}); + myView.delegate('click', 'h1', function() { + assert.ok(true); + }); + myView.delegate('click', function() { + assert.ok(true); + }); + myView.$('h1').trigger('click'); + + assert.equal(myView.delegate(), myView, '#delegate returns the view instance'); + }); + + QUnit.test('delegateEvents allows functions for callbacks', function(assert) { + assert.expect(3); + var myView = new Backbone.View({el: '<p></p>'}); + myView.counter = 0; + + var events = { + click: function() { + this.counter++; + } + }; + + myView.delegateEvents(events); + myView.$el.trigger('click'); + assert.equal(myView.counter, 1); + + myView.$el.trigger('click'); + assert.equal(myView.counter, 2); + + myView.delegateEvents(events); + myView.$el.trigger('click'); + assert.equal(myView.counter, 3); + }); + + + QUnit.test('delegateEvents ignore undefined methods', function(assert) { + assert.expect(0); + var myView = new Backbone.View({el: '<p></p>'}); + myView.delegateEvents({'click': 'undefinedMethod'}); + myView.$el.trigger('click'); + }); + + QUnit.test('undelegateEvents', function(assert) { + assert.expect(7); + var counter1 = 0, counter2 = 0; + + var myView = new Backbone.View({el: '#testElement'}); + myView.increment = function(){ counter1++; }; + myView.$el.on('click', function(){ counter2++; }); + + var events = {'click h1': 'increment'}; + + myView.delegateEvents(events); + myView.$('h1').trigger('click'); + assert.equal(counter1, 1); + assert.equal(counter2, 1); + + myView.undelegateEvents(); + myView.$('h1').trigger('click'); + assert.equal(counter1, 1); + assert.equal(counter2, 2); + + myView.delegateEvents(events); + myView.$('h1').trigger('click'); + assert.equal(counter1, 2); + assert.equal(counter2, 3); + + assert.equal(myView.undelegateEvents(), myView, '#undelegateEvents returns the view instance'); + }); + + QUnit.test('undelegate', function(assert) { + assert.expect(1); + var myView = new Backbone.View({el: '#testElement'}); + myView.delegate('click', function() { assert.ok(false); }); + myView.delegate('click', 'h1', function() { assert.ok(false); }); + + myView.undelegate('click'); + + myView.$('h1').trigger('click'); + myView.$el.trigger('click'); + + assert.equal(myView.undelegate(), myView, '#undelegate returns the view instance'); + }); + + QUnit.test('undelegate with passed handler', function(assert) { + assert.expect(1); + var myView = new Backbone.View({el: '#testElement'}); + var listener = function() { assert.ok(false); }; + myView.delegate('click', listener); + myView.delegate('click', function() { assert.ok(true); }); + myView.undelegate('click', listener); + myView.$el.trigger('click'); + }); + + QUnit.test('undelegate with selector', function(assert) { + assert.expect(2); + var myView = new Backbone.View({el: '#testElement'}); + myView.delegate('click', function() { assert.ok(true); }); + myView.delegate('click', 'h1', function() { assert.ok(false); }); + myView.undelegate('click', 'h1'); + myView.$('h1').trigger('click'); + myView.$el.trigger('click'); + }); + + QUnit.test('undelegate with handler and selector', function(assert) { + assert.expect(2); + var myView = new Backbone.View({el: '#testElement'}); + myView.delegate('click', function() { assert.ok(true); }); + var handler = function(){ assert.ok(false); }; + myView.delegate('click', 'h1', handler); + myView.undelegate('click', 'h1', handler); + myView.$('h1').trigger('click'); + myView.$el.trigger('click'); + }); + + QUnit.test('tagName can be provided as a string', function(assert) { + assert.expect(1); + var View = Backbone.View.extend({ + tagName: 'span' + }); + + assert.equal(new View().el.tagName, 'SPAN'); + }); + + QUnit.test('tagName can be provided as a function', function(assert) { + assert.expect(1); + var View = Backbone.View.extend({ + tagName: function() { + return 'p'; + } + }); + + assert.ok(new View().$el.is('p')); + }); + + QUnit.test('_ensureElement with DOM node el', function(assert) { + assert.expect(1); + var View = Backbone.View.extend({ + el: document.body + }); + + assert.equal(new View().el, document.body); + }); + + QUnit.test('_ensureElement with string el', function(assert) { + assert.expect(3); + var View = Backbone.View.extend({ + el: 'body' + }); + assert.strictEqual(new View().el, document.body); + + View = Backbone.View.extend({ + el: '#testElement > h1' + }); + assert.strictEqual(new View().el, $('#testElement > h1').get(0)); + + View = Backbone.View.extend({ + el: '#nonexistent' + }); + assert.ok(!new View().el); + }); + + QUnit.test('with className and id functions', function(assert) { + assert.expect(2); + var View = Backbone.View.extend({ + className: function() { + return 'className'; + }, + id: function() { + return 'id'; + } + }); + + assert.strictEqual(new View().el.className, 'className'); + assert.strictEqual(new View().el.id, 'id'); + }); + + QUnit.test('with attributes', function(assert) { + assert.expect(2); + var View = Backbone.View.extend({ + attributes: { + 'id': 'id', + 'class': 'class' + } + }); + + assert.strictEqual(new View().el.className, 'class'); + assert.strictEqual(new View().el.id, 'id'); + }); + + QUnit.test('with attributes as a function', function(assert) { + assert.expect(1); + var View = Backbone.View.extend({ + attributes: function() { + return {'class': 'dynamic'}; + } + }); + + assert.strictEqual(new View().el.className, 'dynamic'); + }); + + QUnit.test('should default to className/id properties', function(assert) { + assert.expect(4); + var View = Backbone.View.extend({ + className: 'backboneClass', + id: 'backboneId', + attributes: { + 'class': 'attributeClass', + 'id': 'attributeId' + } + }); + + var myView = new View; + assert.strictEqual(myView.el.className, 'backboneClass'); + assert.strictEqual(myView.el.id, 'backboneId'); + assert.strictEqual(myView.$el.attr('class'), 'backboneClass'); + assert.strictEqual(myView.$el.attr('id'), 'backboneId'); + }); + + QUnit.test('multiple views per element', function(assert) { + assert.expect(3); + var count = 0; + var $el = $('<p></p>'); + + var View = Backbone.View.extend({ + el: $el, + events: { + click: function() { + count++; + } + } + }); + + var view1 = new View; + $el.trigger('click'); + assert.equal(1, count); + + var view2 = new View; + $el.trigger('click'); + assert.equal(3, count); + + view1.delegateEvents(); + $el.trigger('click'); + assert.equal(5, count); + }); + + QUnit.test('custom events', function(assert) { + assert.expect(2); + var View = Backbone.View.extend({ + el: $('body'), + events: { + fake$event: function() { assert.ok(true); } + } + }); + + var myView = new View; + $('body').trigger('fake$event').trigger('fake$event'); + + $('body').off('fake$event'); + $('body').trigger('fake$event'); + }); + + QUnit.test('#1048 - setElement uses provided object.', function(assert) { + assert.expect(2); + var $el = $('body'); + + var myView = new Backbone.View({el: $el}); + assert.ok(myView.$el === $el); + + myView.setElement($el = $($el)); + assert.ok(myView.$el === $el); + }); + + QUnit.test('#986 - Undelegate before changing element.', function(assert) { + assert.expect(1); + var button1 = $('<button></button>'); + var button2 = $('<button></button>'); + + var View = Backbone.View.extend({ + events: { + click: function(e) { + assert.ok(myView.el === e.target); + } + } + }); + + var myView = new View({el: button1}); + myView.setElement(button2); + + button1.trigger('click'); + button2.trigger('click'); + }); + + QUnit.test('#1172 - Clone attributes object', function(assert) { + assert.expect(2); + var View = Backbone.View.extend({ + attributes: {foo: 'bar'} + }); + + var view1 = new View({id: 'foo'}); + assert.strictEqual(view1.el.id, 'foo'); + + var view2 = new View(); + assert.ok(!view2.el.id); + }); + + QUnit.test('views stopListening', function(assert) { + assert.expect(0); + var View = Backbone.View.extend({ + initialize: function() { + this.listenTo(this.model, 'all x', function(){ assert.ok(false); }); + this.listenTo(this.collection, 'all x', function(){ assert.ok(false); }); + } + }); + + var myView = new View({ + model: new Backbone.Model, + collection: new Backbone.Collection + }); + + myView.stopListening(); + myView.model.trigger('x'); + myView.collection.trigger('x'); + }); + + QUnit.test('Provide function for el.', function(assert) { + assert.expect(2); + var View = Backbone.View.extend({ + el: function() { + return '<p><a></a></p>'; + } + }); + + var myView = new View; + assert.ok(myView.$el.is('p')); + assert.ok(myView.$el.has('a')); + }); + + QUnit.test('events passed in options', function(assert) { + assert.expect(1); + var counter = 0; + + var View = Backbone.View.extend({ + el: '#testElement', + increment: function() { + counter++; + } + }); + + var myView = new View({ + events: { + 'click h1': 'increment' + } + }); + + myView.$('h1').trigger('click').trigger('click'); + assert.equal(counter, 2); + }); + + QUnit.test('remove', function(assert) { + assert.expect(2); + var myView = new Backbone.View; + document.body.appendChild(view.el); + + myView.delegate('click', function() { assert.ok(false); }); + myView.listenTo(myView, 'all x', function() { assert.ok(false); }); + + assert.equal(myView.remove(), myView, '#remove returns the view instance'); + myView.$el.trigger('click'); + myView.trigger('x'); + + // In IE8 and below, parentNode still exists but is not document.body. + assert.notEqual(myView.el.parentNode, document.body); + }); + + QUnit.test('setElement', function(assert) { + assert.expect(3); + var myView = new Backbone.View({ + events: { + click: function() { assert.ok(false); } + } + }); + myView.events = { + click: function() { assert.ok(true); } + }; + var oldEl = myView.el; + var $oldEl = myView.$el; + + myView.setElement(document.createElement('div')); + + $oldEl.click(); + myView.$el.click(); + + assert.notEqual(oldEl, myView.el); + assert.notEqual($oldEl, myView.$el); + }); + +})(); |