Updated to modern standards.
This commit is contained in:
parent
e448dd503e
commit
7678aa3e16
|
@ -1,84 +0,0 @@
|
||||||
// A simple module to replace `Backbone.sync` with *localStorage*-based
|
|
||||||
// persistence. Models are given GUIDS, and saved into a JSON object. Simple
|
|
||||||
// as that.
|
|
||||||
|
|
||||||
// Generate four random hex digits.
|
|
||||||
function S4() {
|
|
||||||
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate a pseudo-GUID by concatenating random hexadecimal.
|
|
||||||
function guid() {
|
|
||||||
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
|
|
||||||
};
|
|
||||||
|
|
||||||
// Our Store is represented by a single JS object in *localStorage*. Create it
|
|
||||||
// with a meaningful name, like the name you'd give a table.
|
|
||||||
var Store = function(name) {
|
|
||||||
this.name = name;
|
|
||||||
var store = localStorage.getItem(this.name);
|
|
||||||
this.data = (store && JSON.parse(store)) || {};
|
|
||||||
};
|
|
||||||
|
|
||||||
_.extend(Store.prototype, {
|
|
||||||
|
|
||||||
// Save the current state of the **Store** to *localStorage*.
|
|
||||||
save: function() {
|
|
||||||
localStorage.setItem(this.name, JSON.stringify(this.data));
|
|
||||||
},
|
|
||||||
|
|
||||||
// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
|
|
||||||
// have an id of it's own.
|
|
||||||
create: function(model) {
|
|
||||||
if (!model.id) model.id = model.attributes.id = guid();
|
|
||||||
this.data[model.id] = model;
|
|
||||||
this.save();
|
|
||||||
return model;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Update a model by replacing its copy in `this.data`.
|
|
||||||
update: function(model) {
|
|
||||||
this.data[model.id] = model;
|
|
||||||
this.save();
|
|
||||||
return model;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Retrieve a model from `this.data` by id.
|
|
||||||
find: function(model) {
|
|
||||||
return this.data[model.id];
|
|
||||||
},
|
|
||||||
|
|
||||||
// Return the array of all models currently in storage.
|
|
||||||
findAll: function() {
|
|
||||||
return _.values(this.data);
|
|
||||||
},
|
|
||||||
|
|
||||||
// Delete a model from `this.data`, returning it.
|
|
||||||
destroy: function(model) {
|
|
||||||
delete this.data[model.id];
|
|
||||||
this.save();
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
// Override `Backbone.sync` to use delegate to the model or collection's
|
|
||||||
// *localStorage* property, which should be an instance of `Store`.
|
|
||||||
Backbone.sync = function(method, model, success, error) {
|
|
||||||
|
|
||||||
var resp;
|
|
||||||
var store = model.localStorage || model.collection.localStorage;
|
|
||||||
|
|
||||||
switch (method) {
|
|
||||||
case "read": resp = model.id ? store.find(model) : store.findAll(); break;
|
|
||||||
case "create": resp = store.create(model); break;
|
|
||||||
case "update": resp = store.update(model); break;
|
|
||||||
case "delete": resp = store.destroy(model); break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resp) {
|
|
||||||
success(resp);
|
|
||||||
} else {
|
|
||||||
error("Record not found");
|
|
||||||
}
|
|
||||||
};
|
|
505
backbone.js
505
backbone.js
|
@ -1,4 +1,4 @@
|
||||||
// Backbone.js 0.3.3
|
// Backbone.js 0.5.2
|
||||||
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
||||||
// Backbone may be freely distributed under the MIT license.
|
// Backbone may be freely distributed under the MIT license.
|
||||||
// For all details and documentation:
|
// For all details and documentation:
|
||||||
|
@ -9,24 +9,37 @@
|
||||||
// Initial Setup
|
// Initial Setup
|
||||||
// -------------
|
// -------------
|
||||||
|
|
||||||
|
// Save a reference to the global object.
|
||||||
|
var root = this;
|
||||||
|
|
||||||
|
// Save the previous value of the `Backbone` variable.
|
||||||
|
var previousBackbone = root.Backbone;
|
||||||
|
|
||||||
// The top-level namespace. All public Backbone classes and modules will
|
// The top-level namespace. All public Backbone classes and modules will
|
||||||
// be attached to this. Exported for both CommonJS and the browser.
|
// be attached to this. Exported for both CommonJS and the browser.
|
||||||
var Backbone;
|
var Backbone;
|
||||||
if (typeof exports !== 'undefined') {
|
if (typeof exports !== 'undefined') {
|
||||||
Backbone = exports;
|
Backbone = exports;
|
||||||
} else {
|
} else {
|
||||||
Backbone = this.Backbone = {};
|
Backbone = root.Backbone = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current version of the library. Keep in sync with `package.json`.
|
// Current version of the library. Keep in sync with `package.json`.
|
||||||
Backbone.VERSION = '0.3.3';
|
Backbone.VERSION = '0.5.2';
|
||||||
|
|
||||||
// Require Underscore, if we're on the server, and it's not already present.
|
// Require Underscore, if we're on the server, and it's not already present.
|
||||||
var _ = this._;
|
var _ = root._;
|
||||||
if (!_ && (typeof require !== 'undefined')) _ = require("underscore")._;
|
if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;
|
||||||
|
|
||||||
// For Backbone's purposes, either jQuery or Zepto owns the `$` variable.
|
// For Backbone's purposes, jQuery or Zepto owns the `$` variable.
|
||||||
var $ = this.jQuery || this.Zepto;
|
var $ = root.jQuery || root.Zepto;
|
||||||
|
|
||||||
|
// 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 use support legacy HTTP servers. Setting this option will
|
// Turn on `emulateHTTP` to use support legacy HTTP servers. Setting this option will
|
||||||
// fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
|
// fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
|
||||||
|
@ -55,10 +68,10 @@
|
||||||
|
|
||||||
// Bind an event, specified by a string name, `ev`, to a `callback` function.
|
// Bind an event, specified by a string name, `ev`, to a `callback` function.
|
||||||
// Passing `"all"` will bind the callback to all events fired.
|
// Passing `"all"` will bind the callback to all events fired.
|
||||||
bind : function(ev, callback) {
|
bind : function(ev, callback, context) {
|
||||||
var calls = this._callbacks || (this._callbacks = {});
|
var calls = this._callbacks || (this._callbacks = {});
|
||||||
var list = this._callbacks[ev] || (this._callbacks[ev] = []);
|
var list = calls[ev] || (calls[ev] = []);
|
||||||
list.push(callback);
|
list.push([callback, context]);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -76,8 +89,8 @@
|
||||||
var list = calls[ev];
|
var list = calls[ev];
|
||||||
if (!list) return this;
|
if (!list) return this;
|
||||||
for (var i = 0, l = list.length; i < l; i++) {
|
for (var i = 0, l = list.length; i < l; i++) {
|
||||||
if (callback === list[i]) {
|
if (list[i] && callback === list[i][0]) {
|
||||||
list.splice(i, 1);
|
list[i] = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,17 +102,21 @@
|
||||||
// Trigger an event, firing all bound callbacks. Callbacks are passed the
|
// Trigger an event, firing all bound callbacks. Callbacks are passed the
|
||||||
// same arguments as `trigger` is, apart from the event name.
|
// same arguments as `trigger` is, apart from the event name.
|
||||||
// Listening for `"all"` passes the true event name as the first argument.
|
// Listening for `"all"` passes the true event name as the first argument.
|
||||||
trigger : function(ev) {
|
trigger : function(eventName) {
|
||||||
var list, calls, i, l;
|
var list, calls, ev, callback, args;
|
||||||
|
var both = 2;
|
||||||
if (!(calls = this._callbacks)) return this;
|
if (!(calls = this._callbacks)) return this;
|
||||||
|
while (both--) {
|
||||||
|
ev = both ? eventName : 'all';
|
||||||
if (list = calls[ev]) {
|
if (list = calls[ev]) {
|
||||||
for (i = 0, l = list.length; i < l; i++) {
|
for (var i = 0, l = list.length; i < l; i++) {
|
||||||
list[i].apply(this, Array.prototype.slice.call(arguments, 1));
|
if (!(callback = list[i])) {
|
||||||
|
list.splice(i, 1); i--; l--;
|
||||||
|
} else {
|
||||||
|
args = both ? Array.prototype.slice.call(arguments, 1) : arguments;
|
||||||
|
callback[0].apply(callback[1] || this, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (list = calls['all']) {
|
|
||||||
for (i = 0, l = list.length; i < l; i++) {
|
|
||||||
list[i].apply(this, arguments);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
@ -113,12 +130,17 @@
|
||||||
// Create a new model, with defined attributes. A client id (`cid`)
|
// Create a new model, with defined attributes. A client id (`cid`)
|
||||||
// is automatically generated and assigned for you.
|
// is automatically generated and assigned for you.
|
||||||
Backbone.Model = function(attributes, options) {
|
Backbone.Model = function(attributes, options) {
|
||||||
|
var defaults;
|
||||||
attributes || (attributes = {});
|
attributes || (attributes = {});
|
||||||
if (this.defaults) attributes = _.extend({}, this.defaults, attributes);
|
if (defaults = this.defaults) {
|
||||||
|
if (_.isFunction(defaults)) defaults = defaults.call(this);
|
||||||
|
attributes = _.extend({}, defaults, attributes);
|
||||||
|
}
|
||||||
this.attributes = {};
|
this.attributes = {};
|
||||||
this._escapedAttributes = {};
|
this._escapedAttributes = {};
|
||||||
this.cid = _.uniqueId('c');
|
this.cid = _.uniqueId('c');
|
||||||
this.set(attributes, {silent : true});
|
this.set(attributes, {silent : true});
|
||||||
|
this._changed = false;
|
||||||
this._previousAttributes = _.clone(this.attributes);
|
this._previousAttributes = _.clone(this.attributes);
|
||||||
if (options && options.collection) this.collection = options.collection;
|
if (options && options.collection) this.collection = options.collection;
|
||||||
this.initialize(attributes, options);
|
this.initialize(attributes, options);
|
||||||
|
@ -134,6 +156,10 @@
|
||||||
// Has the item been changed since the last `"change"` event?
|
// Has the item been changed since the last `"change"` event?
|
||||||
_changed : false,
|
_changed : false,
|
||||||
|
|
||||||
|
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
||||||
|
// CouchDB users may want to set this to `"_id"`.
|
||||||
|
idAttribute : 'id',
|
||||||
|
|
||||||
// Initialize is an empty function by default. Override it with your own
|
// Initialize is an empty function by default. Override it with your own
|
||||||
// initialization logic.
|
// initialization logic.
|
||||||
initialize : function(){},
|
initialize : function(){},
|
||||||
|
@ -153,7 +179,13 @@
|
||||||
var html;
|
var html;
|
||||||
if (html = this._escapedAttributes[attr]) return html;
|
if (html = this._escapedAttributes[attr]) return html;
|
||||||
var val = this.attributes[attr];
|
var val = this.attributes[attr];
|
||||||
return this._escapedAttributes[attr] = escapeHTML(val == null ? '' : val);
|
return this._escapedAttributes[attr] = escapeHTML(val == null ? '' : '' + val);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Returns `true` if the attribute contains a value that is not null
|
||||||
|
// or undefined.
|
||||||
|
has : function(attr) {
|
||||||
|
return this.attributes[attr] != null;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Set a hash of model attributes on the object, firing `"change"` unless you
|
// Set a hash of model attributes on the object, firing `"change"` unless you
|
||||||
|
@ -170,7 +202,11 @@
|
||||||
if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
|
if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
|
||||||
|
|
||||||
// Check for changes of `id`.
|
// Check for changes of `id`.
|
||||||
if ('id' in attrs) this.id = attrs.id;
|
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
||||||
|
|
||||||
|
// We're about to start triggering change events.
|
||||||
|
var alreadyChanging = this._changing;
|
||||||
|
this._changing = true;
|
||||||
|
|
||||||
// Update attributes.
|
// Update attributes.
|
||||||
for (var attr in attrs) {
|
for (var attr in attrs) {
|
||||||
|
@ -178,21 +214,21 @@
|
||||||
if (!_.isEqual(now[attr], val)) {
|
if (!_.isEqual(now[attr], val)) {
|
||||||
now[attr] = val;
|
now[attr] = val;
|
||||||
delete escaped[attr];
|
delete escaped[attr];
|
||||||
if (!options.silent) {
|
|
||||||
this._changed = true;
|
this._changed = true;
|
||||||
this.trigger('change:' + attr, this, val, options);
|
if (!options.silent) this.trigger('change:' + attr, this, val, options);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fire the `"change"` event, if the model has been changed.
|
// Fire the `"change"` event, if the model has been changed.
|
||||||
if (!options.silent && this._changed) this.change(options);
|
if (!alreadyChanging && !options.silent && this._changed) this.change(options);
|
||||||
|
this._changing = false;
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Remove an attribute from the model, firing `"change"` unless you choose
|
// Remove an attribute from the model, firing `"change"` unless you choose
|
||||||
// to silence it.
|
// to silence it. `unset` is a noop if the attribute doesn't exist.
|
||||||
unset : function(attr, options) {
|
unset : function(attr, options) {
|
||||||
|
if (!(attr in this.attributes)) return this;
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
var value = this.attributes[attr];
|
var value = this.attributes[attr];
|
||||||
|
|
||||||
|
@ -204,8 +240,9 @@
|
||||||
// Remove the attribute.
|
// Remove the attribute.
|
||||||
delete this.attributes[attr];
|
delete this.attributes[attr];
|
||||||
delete this._escapedAttributes[attr];
|
delete this._escapedAttributes[attr];
|
||||||
if (!options.silent) {
|
if (attr == this.idAttribute) delete this.id;
|
||||||
this._changed = true;
|
this._changed = true;
|
||||||
|
if (!options.silent) {
|
||||||
this.trigger('change:' + attr, this, void 0, options);
|
this.trigger('change:' + attr, this, void 0, options);
|
||||||
this.change(options);
|
this.change(options);
|
||||||
}
|
}
|
||||||
|
@ -216,6 +253,7 @@
|
||||||
// to silence it.
|
// to silence it.
|
||||||
clear : function(options) {
|
clear : function(options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
|
var attr;
|
||||||
var old = this.attributes;
|
var old = this.attributes;
|
||||||
|
|
||||||
// Run validation.
|
// Run validation.
|
||||||
|
@ -225,8 +263,8 @@
|
||||||
|
|
||||||
this.attributes = {};
|
this.attributes = {};
|
||||||
this._escapedAttributes = {};
|
this._escapedAttributes = {};
|
||||||
if (!options.silent) {
|
|
||||||
this._changed = true;
|
this._changed = true;
|
||||||
|
if (!options.silent) {
|
||||||
for (attr in old) {
|
for (attr in old) {
|
||||||
this.trigger('change:' + attr, this, void 0, options);
|
this.trigger('change:' + attr, this, void 0, options);
|
||||||
}
|
}
|
||||||
|
@ -241,13 +279,13 @@
|
||||||
fetch : function(options) {
|
fetch : function(options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
var model = this;
|
var model = this;
|
||||||
var success = function(resp) {
|
var success = options.success;
|
||||||
if (!model.set(model.parse(resp), options)) return false;
|
options.success = function(resp, status, xhr) {
|
||||||
if (options.success) options.success(model, resp);
|
if (!model.set(model.parse(resp, xhr), options)) return false;
|
||||||
|
if (success) success(model, resp);
|
||||||
};
|
};
|
||||||
var error = wrapError(options.error, model, options);
|
options.error = wrapError(options.error, model, options);
|
||||||
(this.sync || Backbone.sync)('read', this, success, error);
|
return (this.sync || Backbone.sync).call(this, 'read', this, options);
|
||||||
return this;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Set a hash of model attributes, and sync the model to the server.
|
// Set a hash of model attributes, and sync the model to the server.
|
||||||
|
@ -257,42 +295,43 @@
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (attrs && !this.set(attrs, options)) return false;
|
if (attrs && !this.set(attrs, options)) return false;
|
||||||
var model = this;
|
var model = this;
|
||||||
var success = function(resp) {
|
var success = options.success;
|
||||||
if (!model.set(model.parse(resp), options)) return false;
|
options.success = function(resp, status, xhr) {
|
||||||
if (options.success) options.success(model, resp);
|
if (!model.set(model.parse(resp, xhr), options)) return false;
|
||||||
|
if (success) success(model, resp, xhr);
|
||||||
};
|
};
|
||||||
var error = wrapError(options.error, model, options);
|
options.error = wrapError(options.error, model, options);
|
||||||
var method = this.isNew() ? 'create' : 'update';
|
var method = this.isNew() ? 'create' : 'update';
|
||||||
(this.sync || Backbone.sync)(method, this, success, error);
|
return (this.sync || Backbone.sync).call(this, method, this, options);
|
||||||
return this;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Destroy this model on the server. Upon success, the model is removed
|
// Destroy this model on the server if it was already persisted. Upon success, the model is removed
|
||||||
// from its collection, if it has one.
|
// from its collection, if it has one.
|
||||||
destroy : function(options) {
|
destroy : function(options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
|
if (this.isNew()) return this.trigger('destroy', this, this.collection, options);
|
||||||
var model = this;
|
var model = this;
|
||||||
var success = function(resp) {
|
var success = options.success;
|
||||||
if (model.collection) model.collection.remove(model);
|
options.success = function(resp) {
|
||||||
if (options.success) options.success(model, resp);
|
model.trigger('destroy', model, model.collection, options);
|
||||||
|
if (success) success(model, resp);
|
||||||
};
|
};
|
||||||
var error = wrapError(options.error, model, options);
|
options.error = wrapError(options.error, model, options);
|
||||||
(this.sync || Backbone.sync)('delete', this, success, error);
|
return (this.sync || Backbone.sync).call(this, 'delete', this, options);
|
||||||
return this;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Default URL for the model's representation on the server -- if you're
|
// Default URL for the model's representation on the server -- if you're
|
||||||
// using Backbone's restful methods, override this to change the endpoint
|
// using Backbone's restful methods, override this to change the endpoint
|
||||||
// that will be called.
|
// that will be called.
|
||||||
url : function() {
|
url : function() {
|
||||||
var base = getUrl(this.collection);
|
var base = getUrl(this.collection) || this.urlRoot || urlError();
|
||||||
if (this.isNew()) return base;
|
if (this.isNew()) return base;
|
||||||
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
|
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
|
||||||
},
|
},
|
||||||
|
|
||||||
// **parse** converts a response into the hash of attributes to be `set` on
|
// **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.
|
// the model. The default implementation is just to pass the response along.
|
||||||
parse : function(resp) {
|
parse : function(resp, xhr) {
|
||||||
return resp;
|
return resp;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -301,10 +340,9 @@
|
||||||
return new this.constructor(this);
|
return new this.constructor(this);
|
||||||
},
|
},
|
||||||
|
|
||||||
// A model is new if it has never been saved to the server, and has a negative
|
// A model is new if it has never been saved to the server, and lacks an id.
|
||||||
// ID.
|
|
||||||
isNew : function() {
|
isNew : function() {
|
||||||
return !this.id;
|
return this.id == null;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Call this method to manually fire a `change` event for this model.
|
// Call this method to manually fire a `change` event for this model.
|
||||||
|
@ -359,7 +397,7 @@
|
||||||
var error = this.validate(attrs);
|
var error = this.validate(attrs);
|
||||||
if (error) {
|
if (error) {
|
||||||
if (options.error) {
|
if (options.error) {
|
||||||
options.error(this, error);
|
options.error(this, error, options);
|
||||||
} else {
|
} else {
|
||||||
this.trigger('error', this, error, options);
|
this.trigger('error', this, error, options);
|
||||||
}
|
}
|
||||||
|
@ -378,14 +416,11 @@
|
||||||
// its models in sort order, as they're added and removed.
|
// its models in sort order, as they're added and removed.
|
||||||
Backbone.Collection = function(models, options) {
|
Backbone.Collection = function(models, options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (options.comparator) {
|
if (options.comparator) this.comparator = options.comparator;
|
||||||
this.comparator = options.comparator;
|
_.bindAll(this, '_onModelEvent', '_removeReference');
|
||||||
delete options.comparator;
|
|
||||||
}
|
|
||||||
this._boundOnModelEvent = _.bind(this._onModelEvent, this);
|
|
||||||
this._reset();
|
this._reset();
|
||||||
if (models) this.refresh(models, {silent: true});
|
if (models) this.reset(models, {silent: true});
|
||||||
this.initialize(models, options);
|
this.initialize.apply(this, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Define the Collection's inheritable methods.
|
// Define the Collection's inheritable methods.
|
||||||
|
@ -453,7 +488,7 @@
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
|
if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
|
||||||
this.models = this.sortBy(this.comparator);
|
this.models = this.sortBy(this.comparator);
|
||||||
if (!options.silent) this.trigger('refresh', this, options);
|
if (!options.silent) this.trigger('reset', this, options);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -463,51 +498,53 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
// When you have more items than you want to add or remove individually,
|
// When you have more items than you want to add or remove individually,
|
||||||
// you can refresh the entire set with a new list of models, without firing
|
// you can reset the entire set with a new list of models, without firing
|
||||||
// any `added` or `removed` events. Fires `refresh` when finished.
|
// any `added` or `removed` events. Fires `reset` when finished.
|
||||||
refresh : function(models, options) {
|
reset : function(models, options) {
|
||||||
models || (models = []);
|
models || (models = []);
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
|
this.each(this._removeReference);
|
||||||
this._reset();
|
this._reset();
|
||||||
this.add(models, {silent: true});
|
this.add(models, {silent: true});
|
||||||
if (!options.silent) this.trigger('refresh', this, options);
|
if (!options.silent) this.trigger('reset', this, options);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Fetch the default set of models for this collection, refreshing the
|
// Fetch the default set of models for this collection, resetting the
|
||||||
// collection when they arrive.
|
// collection when they arrive. If `add: true` is passed, appends the
|
||||||
|
// models to the collection instead of resetting.
|
||||||
fetch : function(options) {
|
fetch : function(options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
var collection = this;
|
var collection = this;
|
||||||
var success = function(resp) {
|
var success = options.success;
|
||||||
collection.refresh(collection.parse(resp));
|
options.success = function(resp, status, xhr) {
|
||||||
if (options.success) options.success(collection, resp);
|
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
|
||||||
|
if (success) success(collection, resp);
|
||||||
};
|
};
|
||||||
var error = wrapError(options.error, collection, options);
|
options.error = wrapError(options.error, collection, options);
|
||||||
(this.sync || Backbone.sync)('read', this, success, error);
|
return (this.sync || Backbone.sync).call(this, 'read', this, options);
|
||||||
return this;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Create a new instance of a model in this collection. After the model
|
// Create a new instance of a model in this collection. After the model
|
||||||
// has been created on the server, it will be added to the collection.
|
// has been created on the server, it will be added to the collection.
|
||||||
|
// Returns the model, or 'false' if validation on a new model fails.
|
||||||
create : function(model, options) {
|
create : function(model, options) {
|
||||||
var coll = this;
|
var coll = this;
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (!(model instanceof Backbone.Model)) {
|
model = this._prepareModel(model, options);
|
||||||
model = new this.model(model, {collection: coll});
|
if (!model) return false;
|
||||||
} else {
|
var success = options.success;
|
||||||
model.collection = coll;
|
options.success = function(nextModel, resp, xhr) {
|
||||||
}
|
coll.add(nextModel, options);
|
||||||
var success = function(nextModel, resp) {
|
if (success) success(nextModel, resp, xhr);
|
||||||
coll.add(nextModel);
|
|
||||||
if (options.success) options.success(nextModel, resp);
|
|
||||||
};
|
};
|
||||||
return model.save(null, {success : success, error : options.error});
|
model.save(null, options);
|
||||||
|
return model;
|
||||||
},
|
},
|
||||||
|
|
||||||
// **parse** converts a response into a list of models to be added to the
|
// **parse** converts a response into a list of models to be added to the
|
||||||
// collection. The default implementation is just to pass it through.
|
// collection. The default implementation is just to pass it through.
|
||||||
parse : function(resp) {
|
parse : function(resp, xhr) {
|
||||||
return resp;
|
return resp;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -518,7 +555,7 @@
|
||||||
return _(this.models).chain();
|
return _(this.models).chain();
|
||||||
},
|
},
|
||||||
|
|
||||||
// Reset all internal state. Called when the collection is refreshed.
|
// Reset all internal state. Called when the collection is reset.
|
||||||
_reset : function(options) {
|
_reset : function(options) {
|
||||||
this.length = 0;
|
this.length = 0;
|
||||||
this.models = [];
|
this.models = [];
|
||||||
|
@ -526,21 +563,34 @@
|
||||||
this._byCid = {};
|
this._byCid = {};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Prepare a model to be added to this collection
|
||||||
|
_prepareModel: function(model, options) {
|
||||||
|
if (!(model instanceof Backbone.Model)) {
|
||||||
|
var attrs = model;
|
||||||
|
model = new this.model(attrs, {collection: this});
|
||||||
|
if (model.validate && !model._performValidation(attrs, options)) model = false;
|
||||||
|
} else if (!model.collection) {
|
||||||
|
model.collection = this;
|
||||||
|
}
|
||||||
|
return model;
|
||||||
|
},
|
||||||
|
|
||||||
// Internal implementation of adding a single model to the set, updating
|
// Internal implementation of adding a single model to the set, updating
|
||||||
// hash indexes for `id` and `cid` lookups.
|
// hash indexes for `id` and `cid` lookups.
|
||||||
|
// Returns the model, or 'false' if validation on a new model fails.
|
||||||
_add : function(model, options) {
|
_add : function(model, options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (!(model instanceof Backbone.Model)) {
|
model = this._prepareModel(model, options);
|
||||||
model = new this.model(model, {collection: this});
|
if (!model) return false;
|
||||||
}
|
|
||||||
var already = this.getByCid(model);
|
var already = this.getByCid(model);
|
||||||
if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
|
if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
|
||||||
this._byId[model.id] = model;
|
this._byId[model.id] = model;
|
||||||
this._byCid[model.cid] = model;
|
this._byCid[model.cid] = model;
|
||||||
model.collection = this;
|
var index = options.at != null ? options.at :
|
||||||
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
|
this.comparator ? this.sortedIndex(model, this.comparator) :
|
||||||
|
this.length;
|
||||||
this.models.splice(index, 0, model);
|
this.models.splice(index, 0, model);
|
||||||
model.bind('all', this._boundOnModelEvent);
|
model.bind('all', this._onModelEvent);
|
||||||
this.length++;
|
this.length++;
|
||||||
if (!options.silent) model.trigger('add', model, this, options);
|
if (!options.silent) model.trigger('add', model, this, options);
|
||||||
return model;
|
return model;
|
||||||
|
@ -554,20 +604,32 @@
|
||||||
if (!model) return null;
|
if (!model) return null;
|
||||||
delete this._byId[model.id];
|
delete this._byId[model.id];
|
||||||
delete this._byCid[model.cid];
|
delete this._byCid[model.cid];
|
||||||
delete model.collection;
|
|
||||||
this.models.splice(this.indexOf(model), 1);
|
this.models.splice(this.indexOf(model), 1);
|
||||||
this.length--;
|
this.length--;
|
||||||
if (!options.silent) model.trigger('remove', model, this, options);
|
if (!options.silent) model.trigger('remove', model, this, options);
|
||||||
model.unbind('all', this._boundOnModelEvent);
|
this._removeReference(model);
|
||||||
return model;
|
return model;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Internal method to remove a model's ties to a collection.
|
||||||
|
_removeReference : function(model) {
|
||||||
|
if (this == model.collection) {
|
||||||
|
delete model.collection;
|
||||||
|
}
|
||||||
|
model.unbind('all', this._onModelEvent);
|
||||||
|
},
|
||||||
|
|
||||||
// Internal method called every time a model in the set fires an event.
|
// 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
|
// Sets need to update their indexes when models change ids. All other
|
||||||
// events simply proxy through.
|
// events simply proxy through. "add" and "remove" events that originate
|
||||||
_onModelEvent : function(ev, model) {
|
// in other collections are ignored.
|
||||||
if (ev === 'change:id') {
|
_onModelEvent : function(ev, model, collection, options) {
|
||||||
delete this._byId[model.previous('id')];
|
if ((ev == 'add' || ev == 'remove') && collection != this) return;
|
||||||
|
if (ev == 'destroy') {
|
||||||
|
this._remove(model, options);
|
||||||
|
}
|
||||||
|
if (model && ev === 'change:' + model.idAttribute) {
|
||||||
|
delete this._byId[model.previous(model.idAttribute)];
|
||||||
this._byId[model.id] = model;
|
this._byId[model.id] = model;
|
||||||
}
|
}
|
||||||
this.trigger.apply(this, arguments);
|
this.trigger.apply(this, arguments);
|
||||||
|
@ -578,7 +640,7 @@
|
||||||
// Underscore methods that we want to implement on the Collection.
|
// Underscore methods that we want to implement on the Collection.
|
||||||
var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 'detect',
|
var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 'detect',
|
||||||
'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include',
|
'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include',
|
||||||
'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size',
|
'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size',
|
||||||
'first', 'rest', 'last', 'without', 'indexOf', 'lastIndexOf', 'isEmpty'];
|
'first', 'rest', 'last', 'without', 'indexOf', 'lastIndexOf', 'isEmpty'];
|
||||||
|
|
||||||
// Mix in each Underscore method as a proxy to `Collection#models`.
|
// Mix in each Underscore method as a proxy to `Collection#models`.
|
||||||
|
@ -588,25 +650,26 @@
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Backbone.Controller
|
// Backbone.Router
|
||||||
// -------------------
|
// -------------------
|
||||||
|
|
||||||
// Controllers map faux-URLs to actions, and fire events when routes are
|
// 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.
|
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
||||||
Backbone.Controller = function(options) {
|
Backbone.Router = function(options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (options.routes) this.routes = options.routes;
|
if (options.routes) this.routes = options.routes;
|
||||||
this._bindRoutes();
|
this._bindRoutes();
|
||||||
this.initialize(options);
|
this.initialize.apply(this, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cached regular expressions for matching named param parts and splatted
|
// Cached regular expressions for matching named param parts and splatted
|
||||||
// parts of route strings.
|
// parts of route strings.
|
||||||
var namedParam = /:([\w\d]+)/g;
|
var namedParam = /:([\w\d]+)/g;
|
||||||
var splatParam = /\*([\w\d]+)/g;
|
var splatParam = /\*([\w\d]+)/g;
|
||||||
|
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
|
||||||
|
|
||||||
// Set up all inheritable **Backbone.Controller** properties and methods.
|
// Set up all inheritable **Backbone.Router** properties and methods.
|
||||||
_.extend(Backbone.Controller.prototype, Backbone.Events, {
|
_.extend(Backbone.Router.prototype, Backbone.Events, {
|
||||||
|
|
||||||
// Initialize is an empty function by default. Override it with your own
|
// Initialize is an empty function by default. Override it with your own
|
||||||
// initialization logic.
|
// initialization logic.
|
||||||
|
@ -628,25 +691,31 @@
|
||||||
}, this));
|
}, this));
|
||||||
},
|
},
|
||||||
|
|
||||||
// Simple proxy to `Backbone.history` to save a fragment into the history,
|
// Simple proxy to `Backbone.history` to save a fragment into the history.
|
||||||
// without triggering routes.
|
navigate : function(fragment, triggerRoute) {
|
||||||
saveLocation : function(fragment) {
|
Backbone.history.navigate(fragment, triggerRoute);
|
||||||
Backbone.history.saveLocation(fragment);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Bind all defined routes to `Backbone.history`.
|
// 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() {
|
_bindRoutes : function() {
|
||||||
if (!this.routes) return;
|
if (!this.routes) return;
|
||||||
|
var routes = [];
|
||||||
for (var route in this.routes) {
|
for (var route in this.routes) {
|
||||||
var name = this.routes[route];
|
routes.unshift([route, this.routes[route]]);
|
||||||
this.route(route, name, this[name]);
|
}
|
||||||
|
for (var i = 0, l = routes.length; i < l; i++) {
|
||||||
|
this.route(routes[i][0], routes[i][1], this[routes[i][1]]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Convert a route string into a regular expression, suitable for matching
|
// Convert a route string into a regular expression, suitable for matching
|
||||||
// against the current location fragment.
|
// against the current location hash.
|
||||||
_routeToRegExp : function(route) {
|
_routeToRegExp : function(route) {
|
||||||
route = route.replace(namedParam, "([^\/]*)").replace(splatParam, "(.*?)");
|
route = route.replace(escapeRegExp, "\\$&")
|
||||||
|
.replace(namedParam, "([^\/]*)")
|
||||||
|
.replace(splatParam, "(.*?)");
|
||||||
return new RegExp('^' + route + '$');
|
return new RegExp('^' + route + '$');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -661,17 +730,22 @@
|
||||||
// Backbone.History
|
// Backbone.History
|
||||||
// ----------------
|
// ----------------
|
||||||
|
|
||||||
// Handles cross-browser history management, based on URL hashes. If the
|
// Handles cross-browser history management, based on URL fragments. If the
|
||||||
// browser does not support `onhashchange`, falls back to polling.
|
// browser does not support `onhashchange`, falls back to polling.
|
||||||
Backbone.History = function() {
|
Backbone.History = function() {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
this.fragment = this.getFragment();
|
|
||||||
_.bindAll(this, 'checkUrl');
|
_.bindAll(this, 'checkUrl');
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cached regex for cleaning hashes.
|
// Cached regex for cleaning hashes.
|
||||||
var hashStrip = /^#*/;
|
var hashStrip = /^#*/;
|
||||||
|
|
||||||
|
// Cached regex for detecting MSIE.
|
||||||
|
var isExplorer = /msie [\w.]+/;
|
||||||
|
|
||||||
|
// Has the history handling already been started?
|
||||||
|
var historyStarted = false;
|
||||||
|
|
||||||
// Set up all inheritable **Backbone.History** properties and methods.
|
// Set up all inheritable **Backbone.History** properties and methods.
|
||||||
_.extend(Backbone.History.prototype, {
|
_.extend(Backbone.History.prototype, {
|
||||||
|
|
||||||
|
@ -679,53 +753,89 @@
|
||||||
// twenty times a second.
|
// twenty times a second.
|
||||||
interval: 50,
|
interval: 50,
|
||||||
|
|
||||||
// Get the cross-browser normalized URL fragment.
|
// Get the cross-browser normalized URL fragment, either from the URL,
|
||||||
getFragment : function(loc) {
|
// the hash, or the override.
|
||||||
return (loc || window.location).hash.replace(hashStrip, '');
|
getFragment : function(fragment, forcePushState) {
|
||||||
|
if (fragment == null) {
|
||||||
|
if (this._hasPushState || forcePushState) {
|
||||||
|
fragment = window.location.pathname;
|
||||||
|
var search = window.location.search;
|
||||||
|
if (search) fragment += search;
|
||||||
|
if (fragment.indexOf(this.options.root) == 0) fragment = fragment.substr(this.options.root.length);
|
||||||
|
} else {
|
||||||
|
fragment = window.location.hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fragment.replace(hashStrip, '');
|
||||||
},
|
},
|
||||||
|
|
||||||
// Start the hash change handling, returning `true` if the current URL matches
|
// Start the hash change handling, returning `true` if the current URL matches
|
||||||
// an existing route, and `false` otherwise.
|
// an existing route, and `false` otherwise.
|
||||||
start : function() {
|
start : function(options) {
|
||||||
|
|
||||||
|
// Figure out the initial configuration. Do we need an iframe?
|
||||||
|
// Is pushState desired ... is it available?
|
||||||
|
if (historyStarted) throw new Error("Backbone.history has already been started");
|
||||||
|
this.options = _.extend({}, {root: '/'}, this.options, options);
|
||||||
|
this._wantsPushState = !!this.options.pushState;
|
||||||
|
this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState);
|
||||||
|
var fragment = this.getFragment();
|
||||||
var docMode = document.documentMode;
|
var docMode = document.documentMode;
|
||||||
var oldIE = ($.browser.msie && (!docMode || docMode <= 7));
|
var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));
|
||||||
if (oldIE) {
|
if (oldIE) {
|
||||||
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
|
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
|
||||||
|
this.navigate(fragment);
|
||||||
}
|
}
|
||||||
if ('onhashchange' in window && !oldIE) {
|
|
||||||
|
// Depending on whether we're using pushState or hashes, and whether
|
||||||
|
// 'onhashchange' is supported, determine how we check the URL state.
|
||||||
|
if (this._hasPushState) {
|
||||||
|
$(window).bind('popstate', this.checkUrl);
|
||||||
|
} else if ('onhashchange' in window && !oldIE) {
|
||||||
$(window).bind('hashchange', this.checkUrl);
|
$(window).bind('hashchange', this.checkUrl);
|
||||||
} else {
|
} else {
|
||||||
setInterval(this.checkUrl, this.interval);
|
setInterval(this.checkUrl, this.interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine if we need to change the base url, for a pushState link
|
||||||
|
// opened by a non-pushState browser.
|
||||||
|
this.fragment = fragment;
|
||||||
|
historyStarted = true;
|
||||||
|
var loc = window.location;
|
||||||
|
var atRoot = loc.pathname == this.options.root;
|
||||||
|
if (this._wantsPushState && !this._hasPushState && !atRoot) {
|
||||||
|
this.fragment = this.getFragment(null, true);
|
||||||
|
window.location.replace(this.options.root + '#' + this.fragment);
|
||||||
|
// Return immediately as browser will do redirect to new url
|
||||||
|
return true;
|
||||||
|
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
|
||||||
|
this.fragment = loc.hash.replace(hashStrip, '');
|
||||||
|
window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
|
||||||
|
}
|
||||||
return this.loadUrl();
|
return this.loadUrl();
|
||||||
},
|
},
|
||||||
|
|
||||||
// Add a route to be tested when the hash changes. Routes are matched in the
|
// Add a route to be tested when the fragment changes. Routes added later may
|
||||||
// order they are added.
|
// override previous routes.
|
||||||
route : function(route, callback) {
|
route : function(route, callback) {
|
||||||
this.handlers.push({route : route, callback : callback});
|
this.handlers.unshift({route : route, callback : callback});
|
||||||
},
|
},
|
||||||
|
|
||||||
// Checks the current URL to see if it has changed, and if it has,
|
// Checks the current URL to see if it has changed, and if it has,
|
||||||
// calls `loadUrl`, normalizing across the hidden iframe.
|
// calls `loadUrl`, normalizing across the hidden iframe.
|
||||||
checkUrl : function() {
|
checkUrl : function(e) {
|
||||||
var current = this.getFragment();
|
var current = this.getFragment();
|
||||||
if (current == this.fragment && this.iframe) {
|
if (current == this.fragment && this.iframe) current = this.getFragment(this.iframe.location.hash);
|
||||||
current = this.getFragment(this.iframe.location);
|
if (current == this.fragment || current == decodeURIComponent(this.fragment)) return false;
|
||||||
}
|
if (this.iframe) this.navigate(current);
|
||||||
if (current == this.fragment ||
|
this.loadUrl() || this.loadUrl(window.location.hash);
|
||||||
current == decodeURIComponent(this.fragment)) return false;
|
|
||||||
if (this.iframe) {
|
|
||||||
window.location.hash = this.iframe.location.hash = current;
|
|
||||||
}
|
|
||||||
this.loadUrl();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Attempt to load the current URL fragment. If a route succeeds with a
|
// Attempt to load the current URL fragment. If a route succeeds with a
|
||||||
// match, returns `true`. If no defined routes matches the fragment,
|
// match, returns `true`. If no defined routes matches the fragment,
|
||||||
// returns `false`.
|
// returns `false`.
|
||||||
loadUrl : function() {
|
loadUrl : function(fragmentOverride) {
|
||||||
var fragment = this.fragment = this.getFragment();
|
var fragment = this.fragment = this.getFragment(fragmentOverride);
|
||||||
var matched = _.any(this.handlers, function(handler) {
|
var matched = _.any(this.handlers, function(handler) {
|
||||||
if (handler.route.test(fragment)) {
|
if (handler.route.test(fragment)) {
|
||||||
handler.callback(fragment);
|
handler.callback(fragment);
|
||||||
|
@ -738,15 +848,23 @@
|
||||||
// Save a fragment into the hash history. You are responsible for properly
|
// Save a fragment into the hash history. You are responsible for properly
|
||||||
// URL-encoding the fragment in advance. This does not trigger
|
// URL-encoding the fragment in advance. This does not trigger
|
||||||
// a `hashchange` event.
|
// a `hashchange` event.
|
||||||
saveLocation : function(fragment) {
|
navigate : function(fragment, triggerRoute) {
|
||||||
fragment = (fragment || '').replace(hashStrip, '');
|
var frag = (fragment || '').replace(hashStrip, '');
|
||||||
if (this.fragment == fragment) return;
|
if (this.fragment == frag || this.fragment == decodeURIComponent(frag)) return;
|
||||||
window.location.hash = this.fragment = fragment;
|
if (this._hasPushState) {
|
||||||
if (this.iframe && (fragment != this.getFragment(this.iframe.location))) {
|
var loc = window.location;
|
||||||
|
if (frag.indexOf(this.options.root) != 0) frag = this.options.root + frag;
|
||||||
|
this.fragment = frag;
|
||||||
|
window.history.pushState({}, document.title, loc.protocol + '//' + loc.host + frag);
|
||||||
|
} else {
|
||||||
|
window.location.hash = this.fragment = frag;
|
||||||
|
if (this.iframe && (frag != this.getFragment(this.iframe.location.hash))) {
|
||||||
this.iframe.document.open().close();
|
this.iframe.document.open().close();
|
||||||
this.iframe.location.hash = fragment;
|
this.iframe.location.hash = frag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (triggerRoute) this.loadUrl(fragment);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -756,10 +874,11 @@
|
||||||
// Creating a Backbone.View creates its initial element outside of the DOM,
|
// Creating a Backbone.View creates its initial element outside of the DOM,
|
||||||
// if an existing element is not provided...
|
// if an existing element is not provided...
|
||||||
Backbone.View = function(options) {
|
Backbone.View = function(options) {
|
||||||
|
this.cid = _.uniqueId('view');
|
||||||
this._configure(options || {});
|
this._configure(options || {});
|
||||||
this._ensureElement();
|
this._ensureElement();
|
||||||
this.delegateEvents();
|
this.delegateEvents();
|
||||||
this.initialize(options);
|
this.initialize.apply(this, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Element lookup, scoped to DOM elements within the current view.
|
// Element lookup, scoped to DOM elements within the current view.
|
||||||
|
@ -770,7 +889,10 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cached regex to split keys for `delegate`.
|
// Cached regex to split keys for `delegate`.
|
||||||
var eventSplitter = /^(\w+)\s*(.*)$/;
|
var eventSplitter = /^(\S+)\s*(.*)$/;
|
||||||
|
|
||||||
|
// List of view options to be merged as properties.
|
||||||
|
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
|
||||||
|
|
||||||
// Set up all inheritable **Backbone.View** properties and methods.
|
// Set up all inheritable **Backbone.View** properties and methods.
|
||||||
_.extend(Backbone.View.prototype, Backbone.Events, {
|
_.extend(Backbone.View.prototype, Backbone.Events, {
|
||||||
|
@ -802,7 +924,7 @@
|
||||||
// For small amounts of DOM Elements, where a full-blown template isn't
|
// For small amounts of DOM Elements, where a full-blown template isn't
|
||||||
// needed, use **make** to manufacture elements, one at a time.
|
// needed, use **make** to manufacture elements, one at a time.
|
||||||
//
|
//
|
||||||
// var el = this.make('li', {'class': 'row'}, this.model.get('title'));
|
// var el = this.make('li', {'class': 'row'}, this.model.escape('title'));
|
||||||
//
|
//
|
||||||
make : function(tagName, attributes, content) {
|
make : function(tagName, attributes, content) {
|
||||||
var el = document.createElement(tagName);
|
var el = document.createElement(tagName);
|
||||||
|
@ -827,12 +949,14 @@
|
||||||
// not `change`, `submit`, and `reset` in Internet Explorer.
|
// not `change`, `submit`, and `reset` in Internet Explorer.
|
||||||
delegateEvents : function(events) {
|
delegateEvents : function(events) {
|
||||||
if (!(events || (events = this.events))) return;
|
if (!(events || (events = this.events))) return;
|
||||||
$(this.el).unbind();
|
$(this.el).unbind('.delegateEvents' + this.cid);
|
||||||
for (var key in events) {
|
for (var key in events) {
|
||||||
var methodName = events[key];
|
var method = this[events[key]];
|
||||||
|
if (!method) throw new Error('Event "' + events[key] + '" does not exist');
|
||||||
var match = key.match(eventSplitter);
|
var match = key.match(eventSplitter);
|
||||||
var eventName = match[1], selector = match[2];
|
var eventName = match[1], selector = match[2];
|
||||||
var method = _.bind(this[methodName], this);
|
method = _.bind(method, this);
|
||||||
|
eventName += '.delegateEvents' + this.cid;
|
||||||
if (selector === '') {
|
if (selector === '') {
|
||||||
$(this.el).bind(eventName, method);
|
$(this.el).bind(eventName, method);
|
||||||
} else {
|
} else {
|
||||||
|
@ -846,22 +970,26 @@
|
||||||
// attached directly to the view.
|
// attached directly to the view.
|
||||||
_configure : function(options) {
|
_configure : function(options) {
|
||||||
if (this.options) options = _.extend({}, this.options, options);
|
if (this.options) options = _.extend({}, this.options, options);
|
||||||
if (options.model) this.model = options.model;
|
for (var i = 0, l = viewOptions.length; i < l; i++) {
|
||||||
if (options.collection) this.collection = options.collection;
|
var attr = viewOptions[i];
|
||||||
if (options.el) this.el = options.el;
|
if (options[attr]) this[attr] = options[attr];
|
||||||
if (options.id) this.id = options.id;
|
}
|
||||||
if (options.className) this.className = options.className;
|
|
||||||
if (options.tagName) this.tagName = options.tagName;
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Ensure that the View has a DOM element to render into.
|
// 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` proeprties.
|
||||||
_ensureElement : function() {
|
_ensureElement : function() {
|
||||||
if (this.el) return;
|
if (!this.el) {
|
||||||
var attrs = {};
|
var attrs = this.attributes || {};
|
||||||
if (this.id) attrs.id = this.id;
|
if (this.id) attrs.id = this.id;
|
||||||
if (this.className) attrs["class"] = this.className;
|
if (this.className) attrs['class'] = this.className;
|
||||||
this.el = this.make(this.tagName, attrs);
|
this.el = this.make(this.tagName, attrs);
|
||||||
|
} else if (_.isString(this.el)) {
|
||||||
|
this.el = $(this.el).get(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -869,13 +997,13 @@
|
||||||
// The self-propagating extend function that Backbone classes use.
|
// The self-propagating extend function that Backbone classes use.
|
||||||
var extend = function (protoProps, classProps) {
|
var extend = function (protoProps, classProps) {
|
||||||
var child = inherits(this, protoProps, classProps);
|
var child = inherits(this, protoProps, classProps);
|
||||||
child.extend = extend;
|
child.extend = this.extend;
|
||||||
return child;
|
return child;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set up inheritance for the model, collection, and view.
|
// Set up inheritance for the model, collection, and view.
|
||||||
Backbone.Model.extend = Backbone.Collection.extend =
|
Backbone.Model.extend = Backbone.Collection.extend =
|
||||||
Backbone.Controller.extend = Backbone.View.extend = extend;
|
Backbone.Router.extend = Backbone.View.extend = extend;
|
||||||
|
|
||||||
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
|
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
|
||||||
var methodMap = {
|
var methodMap = {
|
||||||
|
@ -903,28 +1031,30 @@
|
||||||
// `application/json` with the model in a param named `model`.
|
// `application/json` with the model in a param named `model`.
|
||||||
// Useful when interfacing with server-side languages like **PHP** that make
|
// Useful when interfacing with server-side languages like **PHP** that make
|
||||||
// it difficult to read the body of `PUT` requests.
|
// it difficult to read the body of `PUT` requests.
|
||||||
Backbone.sync = function(method, model, success, error) {
|
Backbone.sync = function(method, model, options) {
|
||||||
var type = methodMap[method];
|
var type = methodMap[method];
|
||||||
var modelJSON = (method === 'create' || method === 'update') ?
|
|
||||||
JSON.stringify(model.toJSON()) : null;
|
|
||||||
|
|
||||||
// Default JSON-request options.
|
// Default JSON-request options.
|
||||||
var params = {
|
var params = _.extend({
|
||||||
url: getUrl(model),
|
|
||||||
type: type,
|
type: type,
|
||||||
contentType: 'application/json',
|
dataType: 'json'
|
||||||
data: modelJSON,
|
}, options);
|
||||||
dataType: 'json',
|
|
||||||
processData: false,
|
// Ensure that we have a URL.
|
||||||
success: success,
|
if (!params.url) {
|
||||||
error: error
|
params.url = getUrl(model) || urlError();
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// Ensure that we have the appropriate request data.
|
||||||
|
if (!params.data && model && (method == 'create' || method == 'update')) {
|
||||||
|
params.contentType = 'application/json';
|
||||||
|
params.data = JSON.stringify(model.toJSON());
|
||||||
|
}
|
||||||
|
|
||||||
// For older servers, emulate JSON by encoding the request into an HTML-form.
|
// For older servers, emulate JSON by encoding the request into an HTML-form.
|
||||||
if (Backbone.emulateJSON) {
|
if (Backbone.emulateJSON) {
|
||||||
params.contentType = 'application/x-www-form-urlencoded';
|
params.contentType = 'application/x-www-form-urlencoded';
|
||||||
params.processData = true;
|
params.data = params.data ? {model : params.data} : {};
|
||||||
params.data = modelJSON ? {model : modelJSON} : {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
|
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
|
||||||
|
@ -934,13 +1064,18 @@
|
||||||
if (Backbone.emulateJSON) params.data._method = type;
|
if (Backbone.emulateJSON) params.data._method = type;
|
||||||
params.type = 'POST';
|
params.type = 'POST';
|
||||||
params.beforeSend = function(xhr) {
|
params.beforeSend = function(xhr) {
|
||||||
xhr.setRequestHeader("X-HTTP-Method-Override", type);
|
xhr.setRequestHeader('X-HTTP-Method-Override', type);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't process data on a non-GET request.
|
||||||
|
if (params.type !== 'GET') {
|
||||||
|
params.processData = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Make the request.
|
// Make the request.
|
||||||
$.ajax(params);
|
return $.ajax(params);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
|
@ -964,6 +1099,9 @@
|
||||||
child = function(){ return parent.apply(this, arguments); };
|
child = function(){ return parent.apply(this, arguments); };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inherit class (static) properties from parent.
|
||||||
|
_.extend(child, parent);
|
||||||
|
|
||||||
// Set the prototype chain to inherit from `parent`, without calling
|
// Set the prototype chain to inherit from `parent`, without calling
|
||||||
// `parent`'s constructor function.
|
// `parent`'s constructor function.
|
||||||
ctor.prototype = parent.prototype;
|
ctor.prototype = parent.prototype;
|
||||||
|
@ -976,7 +1114,7 @@
|
||||||
// Add static properties to the constructor function, if supplied.
|
// Add static properties to the constructor function, if supplied.
|
||||||
if (staticProps) _.extend(child, staticProps);
|
if (staticProps) _.extend(child, staticProps);
|
||||||
|
|
||||||
// Correctly set child's `prototype.constructor`, for `instanceof`.
|
// Correctly set child's `prototype.constructor`.
|
||||||
child.prototype.constructor = child;
|
child.prototype.constructor = child;
|
||||||
|
|
||||||
// Set a convenience property in case the parent's prototype is needed later.
|
// Set a convenience property in case the parent's prototype is needed later.
|
||||||
|
@ -988,15 +1126,20 @@
|
||||||
// Helper function to get a URL from a Model or Collection as a property
|
// Helper function to get a URL from a Model or Collection as a property
|
||||||
// or as a function.
|
// or as a function.
|
||||||
var getUrl = function(object) {
|
var getUrl = function(object) {
|
||||||
if (!(object && object.url)) throw new Error("A 'url' property or function must be specified");
|
if (!(object && object.url)) return null;
|
||||||
return _.isFunction(object.url) ? object.url() : object.url;
|
return _.isFunction(object.url) ? object.url() : object.url;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 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.
|
// Wrap an optional error callback with a fallback error event.
|
||||||
var wrapError = function(onError, model, options) {
|
var wrapError = function(onError, model, options) {
|
||||||
return function(resp) {
|
return function(resp) {
|
||||||
if (onError) {
|
if (onError) {
|
||||||
onError(model, resp);
|
onError(model, resp, options);
|
||||||
} else {
|
} else {
|
||||||
model.trigger('error', model, resp, options);
|
model.trigger('error', model, resp, options);
|
||||||
}
|
}
|
||||||
|
@ -1005,7 +1148,7 @@
|
||||||
|
|
||||||
// Helper function to escape a string for HTML rendering.
|
// Helper function to escape a string for HTML rendering.
|
||||||
var escapeHTML = function(string) {
|
var escapeHTML = function(string) {
|
||||||
return string.replace(/&(?!\w+;)/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
return string.replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
}).call(this);
|
||||||
|
|
538
backbonestore.nw
538
backbonestore.nw
|
@ -1,6 +1,7 @@
|
||||||
% -*- Mode: noweb; noweb-code-mode: javascript-mode ; noweb-doc-mode: latex-mode -*-
|
% -*- Mode: noweb; noweb-code-mode: javascript-mode ; noweb-doc-mode: latex-mode -*-
|
||||||
\documentclass{article}
|
\documentclass{article}
|
||||||
\usepackage{noweb}
|
\usepackage{noweb}
|
||||||
|
\usepackage[T1]{fontenc}
|
||||||
\usepackage{hyperref}
|
\usepackage{hyperref}
|
||||||
\begin{document}
|
\begin{document}
|
||||||
|
|
||||||
|
@ -12,24 +13,26 @@
|
||||||
|
|
||||||
\section{Introduction}
|
\section{Introduction}
|
||||||
|
|
||||||
I've been playing with
|
\nwanchorto{http://documentcloud.github.com/backbone/}{Backbone.js} is
|
||||||
\nwanchorto{http://documentcloud.github.com/backbone/}{Backbone.js}, a
|
a popular Model-View-Controller (MVC) library that provides a
|
||||||
small but nifty Javascript library that provides a small
|
framework by which models generate events and views reflect those
|
||||||
Model-View-Controller framework where Models can generate events that
|
events. The models represent data and ways in which that data can be
|
||||||
trigger View changes, and vice versa, along with a Collections models
|
chnaged. The nifty features of backbone are (1) its event-driven
|
||||||
so groups of models can cause view-level events, and a Sync library
|
architecture, which separate a complex, working model of
|
||||||
that provides a basic REST architecture for propagating client-made
|
\textbf{objects} and their relationships, and the way those things and
|
||||||
changes back to the server.
|
their relationships are presented to the viewer, and (2) its router,
|
||||||
|
which allows developers to create bookmark-ready URLs for specialized
|
||||||
|
views. Backbone also provides a Sync library which will RESTfully
|
||||||
|
shuttle objects back and forth between the browser and the client.
|
||||||
|
|
||||||
There are a number of good tutorials for Backbone (See:
|
There are a number of good tutorials for Backbone (See:
|
||||||
\nwanchorto{http://www.plexical.com/blog/2010/11/18/backbone-js-tutorial/}{Meta
|
\nwanchorto{http://www.plexical.com/blog/2010/11/18/backbone-js-tutorial/}{Meta
|
||||||
Cloud},
|
Cloud},
|
||||||
\nwanchorto{http://andyet.net/blog/2010/oct/29/building-a-single-page-app-with-backbonejs-undersc/?utm_source=twitterfeed&utm_medium=twitter}{&Yet's
|
\nwanchorto{http://andyet.net/blog/2010/oct/29/building-a-single-page-app-with-backbonejs-undersc/?utm_source=twitterfeed&utm_medium=twitter}{\&Yet's Tutorial},
|
||||||
Tutorial},
|
\nwanchorto{http://bennolan.com/2010/11/24/backbone-jquery-demo.html}{Backbone
|
||||||
\nwanchor{http://bennolan.com/2010/11/24/backbone-jquery-demo.html}{Backbone
|
|
||||||
Mobile} (which is written in
|
Mobile} (which is written in
|
||||||
\nwanchorto{http://jashkenas.github.com/coffee-script/}{Coffee}), and
|
\nwanchorto{http://jashkenas.github.com/coffee-script/}{Coffee}), and
|
||||||
\nwanchor{http://joshbohde.com/2010/11/25/backbonejs-and-django/}{Backbone
|
\nwanchorto{http://joshbohde.com/2010/11/25/backbonejs-and-django/}{Backbone
|
||||||
and Django}. However, a couple of months ago I was attempting to
|
and Django}. However, a couple of months ago I was attempting to
|
||||||
learn Sammy.js, a library very similar to Backbone, and they had a
|
learn Sammy.js, a library very similar to Backbone, and they had a
|
||||||
nifty tutorial called
|
nifty tutorial called
|
||||||
|
@ -44,18 +47,19 @@ A note: this article was written with the
|
||||||
\nwanchorto{http://en.wikipedia.org/wiki/Literate_programming}{Literate
|
\nwanchorto{http://en.wikipedia.org/wiki/Literate_programming}{Literate
|
||||||
Programming} toolkit
|
Programming} toolkit
|
||||||
\nwanchorto{http://www.cs.tufts.edu/~nr/noweb/}{Noweb}. Where you see
|
\nwanchorto{http://www.cs.tufts.edu/~nr/noweb/}{Noweb}. Where you see
|
||||||
something that looks like \<\<this\>\>, it's a placeholder for code
|
something that looks like \textless \textless this \textgreater \textgreater, it's a placeholder for code
|
||||||
described elsewhere in the document. Placeholders with an equal sign
|
described elsewhere in the document. Placeholders with an equal sign
|
||||||
at the end of them indicate the place where that code is defined. The
|
at the end of them indicate the place where that code is defined. The
|
||||||
link (U->) indicates that the code you're seeing is used later in the
|
link (U-\textgreater) indicates that the code you're seeing is used later in the
|
||||||
document, and (<-U) indicates it was used earlier but is being defined
|
document, and (\textless-U) indicates it was used earlier but is being defined
|
||||||
here.
|
here.
|
||||||
|
|
||||||
\subsection{Revision}
|
\subsection{Revision}
|
||||||
|
|
||||||
This is version 1.2 of \textit{The Backbone Store}. It includes
|
This is version 2.0 of \textit{The Backbone Store}. It includes
|
||||||
changes to the store based upon a better understanding of what
|
changes to the store based upon a better understanding of what
|
||||||
Backbone.js can do. This version uses jQuery 1.5 and Backbone 0.3.3.
|
Backbone.js can do. This version uses jQuery 1.6.2 and Backbone
|
||||||
|
0.5.2.
|
||||||
|
|
||||||
\subsection{The Store}
|
\subsection{The Store}
|
||||||
|
|
||||||
|
@ -65,29 +69,267 @@ number of products total that you might wish to order. The main
|
||||||
viewport flips between a list of products and a product detail; the
|
viewport flips between a list of products and a product detail; the
|
||||||
shopping cart quantity tally is always visible.
|
shopping cart quantity tally is always visible.
|
||||||
|
|
||||||
Let's start by showing you the HTML that we're going to be
|
We will be creating a store for music albums. There will be: (1) The
|
||||||
exploiting. As you can see, the shopping cart's primary display is
|
catalog of products, (2) A detail page for a specific product from the
|
||||||
already present, with zero items shoving. DOM ID ``main'' is empty.
|
catalog, (3) A ``checkout page'' where users can add/change/delete
|
||||||
We'll fill it with templated data later.
|
items from their shopping cart, and (4) a shopping cart ``widget''
|
||||||
|
that is visible on every page, and allows the user to see how many
|
||||||
|
items are in the cart and how much money those items cost.
|
||||||
|
|
||||||
\subsection{HTML}
|
This is taken, more or less, straight from The JSON Store. We will be
|
||||||
|
getting our data from a simplified JSON file that comes in the
|
||||||
|
download; it contains six record albums that the store sells. (Unlike
|
||||||
|
the JSON store, these albums do not exist; the covers were generated
|
||||||
|
during a round of
|
||||||
|
\nwanchorto{http://elfs.livejournal.com/756709.html}{The Album Cover
|
||||||
|
Game}, a meme one popular with graphic designers.)
|
||||||
|
|
||||||
|
Under the covers, we have two essential objects: a \textbf{Product}
|
||||||
|
that we're selling, and a shopping cart \textbf{Item} into which we
|
||||||
|
put a reference to a Product and a count of the number of that product
|
||||||
|
that we're selling. In the Backbone idiom, we will be callling the
|
||||||
|
cart an \textbf{ItemCollection} that the user wants to buy, and the
|
||||||
|
Products will be kept in a \textbf{ProductCollection}
|
||||||
|
|
||||||
|
In backbone's parlance, Product and Item are \textbf{Models}, and Cart
|
||||||
|
and Catalog are \textbf{Collections}. The idiom is that models are
|
||||||
|
named for what they represent, and collections are model names
|
||||||
|
suffixed with the word ``collection.'' The pages ``catalog,''
|
||||||
|
``product detail,'' and ``checkout'' are \textbf{Routable Views},
|
||||||
|
while the shopping cart widget is just a \textbf{View}. There's no
|
||||||
|
programmatic difference internally between the two kinds of views;
|
||||||
|
instead, the difference is in how they're accessed.
|
||||||
|
|
||||||
|
\subsection{Models}
|
||||||
|
|
||||||
|
The first version of this tutorial concentrated on the HTML. In this
|
||||||
|
version, we're going to start logically, with the models. The first
|
||||||
|
model is \textbf{Product}, that is, the thing we're selling in our
|
||||||
|
store. We will create Products by inheriting from Backbone's
|
||||||
|
\textbf{Model}.
|
||||||
|
|
||||||
|
Backbone models use the methods [[get()]] and [[set()]] to access the
|
||||||
|
attributes of the model. When you want to change a model's attribute,
|
||||||
|
you must do so through those methods. Any other object that has even
|
||||||
|
a fleeting reference to the model can then subscribe to the
|
||||||
|
\textbf{change} event on that model, and whenever [[set()]] is called,
|
||||||
|
those other objects can react in some way. This is one of the most
|
||||||
|
important features of Backbone, and you'll see why shortly.
|
||||||
|
|
||||||
|
Because a Backbone model maintains its attributes as a javascript
|
||||||
|
object, it is schema-free. So the Product model is ridiculously
|
||||||
|
simple:
|
||||||
|
|
||||||
|
<<product models>>=
|
||||||
|
var Product = Backbone.Model.extend({})
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
And we said before, the products are kept in a catalog. Backbone's
|
||||||
|
``list of models'' feature is called a \textbf{Collection}, and to
|
||||||
|
stay in Backbone's idioms, rather than call it ``Catalog'', we'll call
|
||||||
|
it a \textbf{ProductCollection}:
|
||||||
|
|
||||||
|
<<product models>>=
|
||||||
|
var ProductCollection = Backbone.Collection.extend({
|
||||||
|
model: Product,
|
||||||
|
|
||||||
|
comparator: function(item) {
|
||||||
|
return item.get('title');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
Collections have a reference to the Product constructor; if you call
|
||||||
|
[[Collection.add()]] with a JSON object, it will use that
|
||||||
|
constructor to create the associated Backbone model object.
|
||||||
|
|
||||||
|
The other novel thing here is the comparator; Backbone uses it define
|
||||||
|
the default ordering for the collection. If not defined, calling
|
||||||
|
[[sort()]] on the collection raises an exception.
|
||||||
|
|
||||||
|
Shopping carts have always seemed a bit strange to me, because each
|
||||||
|
item isn't a one-to-one with a product, but a reference to the product
|
||||||
|
and a quantity. For our (simple) purpose, I'm just going to have an
|
||||||
|
item that you can add amounts to, that get stored as a 'quantity'.
|
||||||
|
|
||||||
|
<<shopping cart models>>=
|
||||||
|
var Item = Backbone.Model.extend({
|
||||||
|
update: function(amount) {
|
||||||
|
this.set({'quantity': this.get('quantity') + amount});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
The other feature is that, for the collection, I will want to find the
|
||||||
|
CartItem not by its ID, but by the product it contains, and I want the
|
||||||
|
Cart to be able to host any product, even it it has none of those, So
|
||||||
|
I have added the method [[getOrCreateItemForProduct]]. The
|
||||||
|
[[detect()]] and [[reduce()]] methods ares provided by Backbone's one
|
||||||
|
major dependency, a wonderful utility library called
|
||||||
|
\texttt{Underscore}. [[detect()]] returns the first object for which
|
||||||
|
the anonymous function return [[true]]. The [[reduce()]] functions
|
||||||
|
take an intitial value and a means of calculating a per-object value,
|
||||||
|
and reduce all that to a final value for all objects in the
|
||||||
|
collection.
|
||||||
|
|
||||||
|
<<shopping cart models>>=
|
||||||
|
var ItemCollection = Backbone.Collection.extend({
|
||||||
|
model: Item,
|
||||||
|
getOrCreateItemForProduct: function(product) {
|
||||||
|
var i,
|
||||||
|
pid = product.get('id'),
|
||||||
|
o = this.detect(function(obj) {
|
||||||
|
return (obj.get('product').get('id') == pid);
|
||||||
|
});
|
||||||
|
if (o) {
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
i = new Item({'product': product, 'quantity': 0})
|
||||||
|
this.add(i, {silent: true})
|
||||||
|
return i;
|
||||||
|
},
|
||||||
|
getTotalCount: function() {
|
||||||
|
return this.reduce(function(memo, obj) {
|
||||||
|
return obj.get('quantity') + memo; }, 0);
|
||||||
|
}
|
||||||
|
getTotalCost: function() {
|
||||||
|
return this.reduce(function(memo, obj) {
|
||||||
|
return (obj.get('product').get('price') *
|
||||||
|
obj.get('quantity')) + memo; }, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
\subsection {Views}
|
||||||
|
|
||||||
|
Now that we have the structure for our catalog and our shopping cart
|
||||||
|
laid out, let's show you how those are organized visually. I'd like
|
||||||
|
to say that it's possible to completely separate View and their
|
||||||
|
descriptions of how to interact with the DOM with DOM development, but
|
||||||
|
we must have some preliminary plans for dealing with the display.
|
||||||
|
|
||||||
|
The plan is to have a one-page display for everything. We will have
|
||||||
|
an area of the screen allocated for our major, routable views (the
|
||||||
|
product list display, the product detail display, and the checkout
|
||||||
|
display), and a small area of the screen allocated for our shopping
|
||||||
|
cart. Let's put the shopping cart link in the upper-right-hand
|
||||||
|
corner; everybody does.
|
||||||
|
|
||||||
|
As an additional feature, we want the views to transition elegantly,
|
||||||
|
using the jQuery [[fadeIn()]] and [[fadeOut()]] animations.
|
||||||
|
|
||||||
|
Backbone Views are simple policy objects. They often have a root
|
||||||
|
element, the contents of which they manipulate, a model or collection
|
||||||
|
they represent within that root element, events that may occur within
|
||||||
|
that root element that they monitor and consequently act on. Views
|
||||||
|
are not rigid; it's just Javascript and the DOM, and you can hook
|
||||||
|
external events as needed. (This can be useful, for example, when
|
||||||
|
doing drag-and-drop with jQueryUI to highlight valid drop zones.)
|
||||||
|
More importantly, it is sensitive to events \textit{within its model
|
||||||
|
or collection}, and can respond to changes automatically, without
|
||||||
|
having to manually invoke the view.
|
||||||
|
|
||||||
|
A Backbone view can be either an existing DOM element, or it can
|
||||||
|
generate one automatically at (client-side) run time. In the previous
|
||||||
|
version of the tutorial, I used existing DOM elements, but for this
|
||||||
|
one, almost everything will be generated at run time.
|
||||||
|
|
||||||
|
To achieve the animations and enforce consistency, we're going to
|
||||||
|
engage in classic object-oriented programming. We're going to create
|
||||||
|
a base class that contains knowledge about the main area into which
|
||||||
|
all views are rendered, and that manages these transitions. With this
|
||||||
|
technique, you can do lots of navigation-related tricks: you can
|
||||||
|
highlight where the user is in breadcrumb-style navigation; you can
|
||||||
|
change the class and highlight an entry on a nav bar; you can add and
|
||||||
|
remove tabs from the top of the viewport as needed.
|
||||||
|
|
||||||
|
<<base view>>=
|
||||||
|
var _BaseView = Backbone.View.extend({
|
||||||
|
parent: '#main',
|
||||||
|
className: 'viewport',
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
The above says that we're creating a class called \texttt{BaseView}
|
||||||
|
and defining two fields. The first, 'parent', will be used by all
|
||||||
|
child views to identify in which DOM object the view will be rendered.
|
||||||
|
The second defines a common class we will use for the purpose of
|
||||||
|
identifying these views to jQuery. Backbone automatically creates a
|
||||||
|
new [[DIV]] object with the class 'viewport' when a view
|
||||||
|
constructor is called. It will be our job to attach that [[DIV]]
|
||||||
|
to the DOM.
|
||||||
|
|
||||||
|
<<base view>>=
|
||||||
|
initialize: function() {
|
||||||
|
this.el = $(this.el); //$
|
||||||
|
this.el.hide();
|
||||||
|
this.parent.append(this.el);
|
||||||
|
return this.
|
||||||
|
},
|
||||||
|
@
|
||||||
|
|
||||||
|
The method above ensures that the element is rendered, but not
|
||||||
|
visible, and contained within the [[DIV#main]]. Note also that
|
||||||
|
the element is not a sacrosanct object; the Backbone.View is more a
|
||||||
|
collection of standards than a mechanism of enforcement, and so
|
||||||
|
defining it from a raw DOM object to a jQuery object will not break
|
||||||
|
anything.
|
||||||
|
|
||||||
|
Next, we will define the hide and show functions:
|
||||||
|
|
||||||
|
<<base view>>=
|
||||||
|
hide: function() {
|
||||||
|
if (this.el.is(":visible") === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
promise = $.Deferred(function(dfd) { //$
|
||||||
|
this.el.fadeOut('fast', dfd.resolve)
|
||||||
|
}).promise();
|
||||||
|
this.trigger('hide', this);
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
|
||||||
|
show: function() {
|
||||||
|
if (this.el.is(':visible')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
promise = $.Deferred(function(dfd) { //$
|
||||||
|
this.el.fadeIn('fast', dfd.resolve)
|
||||||
|
}).promise();
|
||||||
|
|
||||||
|
this.trigger('show', this);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
@
|
||||||
|
|
||||||
|
\textbf{Deferred} is a new feature of jQuery. It is a different
|
||||||
|
mechanism for invoking callbacks by attaching attributes and behavior
|
||||||
|
to the callback function. By using this, we can say thing like
|
||||||
|
``\textit{When} everything is hidden (when every deferred returned
|
||||||
|
from \textbf{hide} has been resolved), \textit{then} show the
|
||||||
|
appropriate viewport.'' Deferreds are incredibly powerful, and this
|
||||||
|
is a small taste of what can be done with them.
|
||||||
|
|
||||||
|
Before we move on, let's take a look at the HTML we're going to use
|
||||||
|
for our one-page application:
|
||||||
|
|
||||||
<<index.html>>=
|
<<index.html>>=
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
<!DOCTYPE html>
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
||||||
<head>
|
<head>
|
||||||
<title>
|
<title>
|
||||||
The Backbone Store
|
The Backbone Store
|
||||||
</title>
|
</title>
|
||||||
<link rel="stylesheet" href="jsonstore.css" type="text/css" media="screen" charset="utf-8" />
|
<link rel="stylesheet" href="jsonstore.css" type="text/css">
|
||||||
|
|
||||||
<<product list template>>
|
<<product list template>>
|
||||||
|
|
||||||
<<product template>>
|
<<product template>>
|
||||||
|
<<checkout template>>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div id="container">
|
<div id="container">
|
||||||
<div id="header">
|
<div id="header">
|
||||||
|
@ -96,14 +338,12 @@ We'll fill it with templated data later.
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<div class="cart-info">
|
<div class="cart-info">
|
||||||
My Cart (<span class="cart-items">0</span> items)
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="main"> </div>
|
<div id="main"> </div>
|
||||||
</div>
|
</div>
|
||||||
<script src="jquery-1.5.js" type="text/javascript"></script>
|
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
|
||||||
<script src="jquery.tmpl.min.js" type="text/javascript"></script>
|
|
||||||
<script src="underscore.js" type="text/javascript"></script>
|
<script src="underscore.js" type="text/javascript"></script>
|
||||||
<script src="backbone.js" type="text/javascript"></script>
|
<script src="backbone.js" type="text/javascript"></script>
|
||||||
<script src="store.js" type="text/javascript"></script>
|
<script src="store.js" type="text/javascript"></script>
|
||||||
|
@ -111,15 +351,146 @@ We'll fill it with templated data later.
|
||||||
</html>
|
</html>
|
||||||
@
|
@
|
||||||
|
|
||||||
This is taken, more or less, straight from The JSON Store. I've
|
It's not much to look at, but already you can see where that
|
||||||
included one extra thing, aside from jQuery and Backbone, and that's
|
[[DIV#main]] goes, as well as where we are putting our templates.
|
||||||
the \nwanchorto{https://github.com/jquery/jquery-tmpl}{jQuery
|
The [[DIV#main]] will host a number of viewports, only one of
|
||||||
Templates kit}. There is also a simplified JSON file that comes in
|
which will be visible at any given time.
|
||||||
the download; it contains six record albums that the store sells.
|
|
||||||
(Unlike the JSON store, these albums do not exist; the covers were
|
Our first view is going to be the product list view, named, well,
|
||||||
generated during a round of
|
guess. Or just look down a few lines.
|
||||||
\nwanchorto{http://elfs.livejournal.com/756709.html}{The Album Cover
|
|
||||||
Game}.)
|
This gives us a chance to discuss one of the big confusions new
|
||||||
|
Backbone users frequently have: \textit{What is \texttt{render()}
|
||||||
|
for?}. Render is not there to show or hide the view.
|
||||||
|
\texttt{Render()} is there to \textit{change the view when the
|
||||||
|
underlying data changes}. It renders the data into a view. In our
|
||||||
|
functionality, we use the parent class's \texttt{show()} and
|
||||||
|
\texttt{hide()} methods to actually show the view.
|
||||||
|
|
||||||
|
<<product list view>>=
|
||||||
|
var ProductListView = _BaseView.extend({
|
||||||
|
id: 'productlistview',
|
||||||
|
indexTemplate: $("#store_index_template").template(), //$
|
||||||
|
|
||||||
|
render: function() {
|
||||||
|
self.el.html(_.template(this.template, {'products': this.model.toJSON()}))
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
That \texttt{\_.template()} method is provided by undescore.js, and is
|
||||||
|
a fairly powerful templating method. It's not the fastest or the most
|
||||||
|
feature-complete, but it is more than adequate for our purposes and it
|
||||||
|
means we don't have to import another library. It vaguely resembles
|
||||||
|
ERB from Rails, so if you are familiar with that, you should
|
||||||
|
understand this fairly easily.
|
||||||
|
|
||||||
|
And here is the HTML:
|
||||||
|
|
||||||
|
<<product list template>>=
|
||||||
|
<script id="store_index_template" type="text/x-jquery-tmpl">
|
||||||
|
<h1>Product Catalog</h1>
|
||||||
|
<ul>
|
||||||
|
<% for(i=0,l=products.length;i<l;++i) { p = products[i]; %>
|
||||||
|
<li class="item">
|
||||||
|
<div class="item-image">
|
||||||
|
<a href="#item/<%= p.cid%>">
|
||||||
|
<img src="<%= p.image %>" alt="<%= p.title %>" /></a>
|
||||||
|
</div>
|
||||||
|
<div class="item-artist"><%= p.artist %></div>
|
||||||
|
<div class="item-title"><%= p.title %></div>
|
||||||
|
<div class="item-price">$<%= p.price %></div>
|
||||||
|
</li>
|
||||||
|
<% } %>
|
||||||
|
</ul>
|
||||||
|
</script>
|
||||||
|
@
|
||||||
|
%$
|
||||||
|
|
||||||
|
One of the most complicated objects in our ecosystem is the product
|
||||||
|
view. It actually does something! The prefix ought to be familiar,
|
||||||
|
but note that we are again using [[#main]] as our target; we will be
|
||||||
|
showing and hiding the various [[DIV]] objects in [[#main]] again and
|
||||||
|
again.
|
||||||
|
|
||||||
|
The only trickiness here is twofold: the (rather hideous) means by
|
||||||
|
which one calls the method of a parnt class from a child class via
|
||||||
|
Backbone's class heirarchy (this is most definitely \textbf{not}
|
||||||
|
Javascript standard), and keeping track of the itemcollection object,
|
||||||
|
so we can add and change items as needed.
|
||||||
|
|
||||||
|
<<product detail view>>=
|
||||||
|
var ProductListView = _BaseView.extend({
|
||||||
|
id: 'productlistview',
|
||||||
|
indexTemplate: $("#store_item_template").template(), //$
|
||||||
|
initialize: function(options) {
|
||||||
|
this.constructor.__super__.initialize.apply(this, [options])
|
||||||
|
this.itemcollection = options.itemcollection;
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
We want to update the cart as needed. Remember the way Backbone is
|
||||||
|
supposed to work: when we update the cart, it will send out a signal
|
||||||
|
automatically, and subscribers (in this case, that little widget in
|
||||||
|
the upper right hand corner we mentioned earlier) will show the
|
||||||
|
changes.
|
||||||
|
|
||||||
|
These are the events in which we're interested: keypresses and clicks
|
||||||
|
on the update button and the quantity form. (Okay, ``UQ'' isn't the
|
||||||
|
best for ``update quantity''. I admit that.) Note the peculiar
|
||||||
|
syntax of ``EVENT SELECTOR'': ``methodByName'' for each event.
|
||||||
|
|
||||||
|
Backbone tells us that the only events it can track by itself are
|
||||||
|
those that jQuery's ``delegate'' understands. As of 1.5, that seems
|
||||||
|
to be just about all of them.
|
||||||
|
|
||||||
|
<<product view>>=
|
||||||
|
events: {
|
||||||
|
"keypress .uqf" : "updateOnEnter",
|
||||||
|
"click .uq" : "update",
|
||||||
|
},
|
||||||
|
|
||||||
|
@
|
||||||
|
|
||||||
|
And now we will deal with the update. This code ought to be fairly
|
||||||
|
readable: the only specialness is that it's receiving an event, and
|
||||||
|
we're ``silencing'' the call to [[cart.add()]], which means that the
|
||||||
|
cart collection will not publish any events. There are only events
|
||||||
|
when the item has more than zero, and that gets called on
|
||||||
|
[[cart_item.update()]].
|
||||||
|
|
||||||
|
In the original tutorial, this code had a lot of responsibility for
|
||||||
|
managing the shopping cart, looking into it and seeing if it had an
|
||||||
|
item for this product, and there was lots of accessing the model to
|
||||||
|
get its id and so forth. All of that has been put into the shopping
|
||||||
|
cart model, which is where it belongs: \textit{knowledge about items
|
||||||
|
and each item's relationship to its collection belongs in the
|
||||||
|
collection}.
|
||||||
|
%'
|
||||||
|
|
||||||
|
<<product view>>=
|
||||||
|
update: function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var item = this.itemcollection.getOrCreateItemProduct(this.model);
|
||||||
|
item.update(parseInt($('.uqf').val()));
|
||||||
|
},
|
||||||
|
|
||||||
|
updateOnEnter: function(e) {
|
||||||
|
if (e.keyCode == 13) {
|
||||||
|
return this.update(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
@
|
||||||
|
%$
|
||||||
|
|
||||||
|
So, let's talk about that shopping cart thing. We've been making the
|
||||||
|
point that when it changes, automatically you should see just how many
|
||||||
|
|
||||||
|
|
||||||
\section{The Program}
|
\section{The Program}
|
||||||
|
|
||||||
|
@ -145,84 +516,6 @@ And here's the skeleton of the program we're going to be writing:
|
||||||
}).call(this);
|
}).call(this);
|
||||||
@
|
@
|
||||||
|
|
||||||
\section{Models and Collections for the Store}
|
|
||||||
|
|
||||||
Products are basically what we're selling. In Backbone, a product
|
|
||||||
maps to a Backbone Model. Backbone's Model class provides a full
|
|
||||||
suite of methods for setting and deleting attributes.
|
|
||||||
|
|
||||||
One of things I've found useful is to expose the CID (an internal and
|
|
||||||
locally unique ``client ID'' generated by Backbone) and use it to
|
|
||||||
decorate DOM id's and classes. So, here, I override the Models's
|
|
||||||
[[toJSON()]] and add the CID to the representation. In production,
|
|
||||||
I've typically created a parent class for all of my classes, overriden
|
|
||||||
[[toJSON()]], and extended that instead.
|
|
||||||
|
|
||||||
<<product models>>=
|
|
||||||
var Product = Backbone.Model.extend({
|
|
||||||
toJSON: function() {
|
|
||||||
return _.extend(_.clone(this.attributes), {cid: this.cid})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
@
|
|
||||||
|
|
||||||
A store has a lot of products, so we use a Backbone Collection to keep
|
|
||||||
track of them. A Collection always has a Model object associated with
|
|
||||||
it; if you attempt to add an object to the Collection that is not an
|
|
||||||
instance of [[Backbone.Model]], the Collection will attempt to coerce
|
|
||||||
that object into it [[model]] type.
|
|
||||||
|
|
||||||
Both Models and Collections have a [[toJSON()]] method. The Model
|
|
||||||
creates a JSON representation of its attributes, and the collection
|
|
||||||
creates a JSON representation of an array, calling [[Model.toJSON()]]
|
|
||||||
for each model in contains.
|
|
||||||
|
|
||||||
|
|
||||||
The other novel thing here is the comparator; Backbone uses it define
|
|
||||||
the default ordering for the collection. If not defined, calling
|
|
||||||
[[sort()]] on the collection raises an exception.
|
|
||||||
|
|
||||||
<<product models>>=
|
|
||||||
var ProductCollection = Backbone.Collection.extend({
|
|
||||||
model: Product,
|
|
||||||
|
|
||||||
comparator: function(item) {
|
|
||||||
return item.get('title');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
@
|
|
||||||
|
|
||||||
Shopping carts are a bit strange. The typical cart has items: each
|
|
||||||
item represents one product and the quantity the user wants to buy.
|
|
||||||
So a cart may have two items, but the user may be buying five things:
|
|
||||||
one of the first item, and four of the second, and so forth.
|
|
||||||
|
|
||||||
For our (simple) purpose, I'm just going to have an item that you can
|
|
||||||
add amounts to, that get stored as a 'quantity'.
|
|
||||||
|
|
||||||
<<shopping cart models>>=
|
|
||||||
var CartItem = Backbone.Model.extend({
|
|
||||||
update: function(amount) {
|
|
||||||
this.set({'quantity': this.get('quantity') + amount});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
@
|
|
||||||
|
|
||||||
The other feature is that, for the collection, I will want to find the
|
|
||||||
CartItem not by its ID, but by the ID of the product it contains. So
|
|
||||||
I have added the method [[getByProductId]].
|
|
||||||
|
|
||||||
<<shopping cart models>>=
|
|
||||||
var Cart = Backbone.Collection.extend({
|
|
||||||
model: CartItem,
|
|
||||||
getByProductId: function(pid) {
|
|
||||||
return this.detect(function(obj) { return (obj.get('product').cid == pid); });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
@
|
|
||||||
|
|
||||||
\section{Views}
|
\section{Views}
|
||||||
|
|
||||||
|
@ -320,10 +613,7 @@ until the array is exhausted. Note the presence of [[\${cid}]].
|
||||||
@
|
@
|
||||||
%$
|
%$
|
||||||
|
|
||||||
The most complicated object in our ecosystem is the product view. It
|
The most complicated object .
|
||||||
actually does something! The prefix ought to be familiar, but note
|
|
||||||
that we're again using [[#main]] as our target; we'll be replacing
|
|
||||||
these contents over and over.
|
|
||||||
|
|
||||||
<<product view>>=
|
<<product view>>=
|
||||||
var ProductView = Backbone.View.extend({
|
var ProductView = Backbone.View.extend({
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
|
"id": "unless",
|
||||||
"title": "Unless You Have Been Drinking",
|
"title": "Unless You Have Been Drinking",
|
||||||
"artist": "Adventures in Odyssey",
|
"artist": "Adventures in Odyssey",
|
||||||
"image": "images/AdventuresInOdyssey_t.jpg",
|
"image": "images/AdventuresInOdyssey_t.jpg",
|
||||||
|
@ -8,6 +9,7 @@
|
||||||
"url": "http://www.amazon.com/Door-Religious-Knives/dp/B001FGW0UQ/?tag=quirkey-20"
|
"url": "http://www.amazon.com/Door-Religious-Knives/dp/B001FGW0UQ/?tag=quirkey-20"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id": "utmost",
|
||||||
"title": "Leave To Do My Utmost",
|
"title": "Leave To Do My Utmost",
|
||||||
"artist": "American Attorneys",
|
"artist": "American Attorneys",
|
||||||
"image": "images/AmericanAttorneys_t.jpg",
|
"image": "images/AmericanAttorneys_t.jpg",
|
||||||
|
@ -16,6 +18,7 @@
|
||||||
"url": "http://www.amazon.com/gp/product/B002GNOMJE?ie=UTF8&tag=quirkeycom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=B002GNOMJE"
|
"url": "http://www.amazon.com/gp/product/B002GNOMJE?ie=UTF8&tag=quirkeycom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=B002GNOMJE"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id": "encircled",
|
||||||
"title": "The Dead Sleep Encircled by The Living",
|
"title": "The Dead Sleep Encircled by The Living",
|
||||||
"artist": "British Civil Light Transport",
|
"artist": "British Civil Light Transport",
|
||||||
"image": "images/BritishCivilLightTransport_t.jpg",
|
"image": "images/BritishCivilLightTransport_t.jpg",
|
||||||
|
@ -24,6 +27,7 @@
|
||||||
"url": "http://www.amazon.com/Bitte-Orca-Dirty-Projectors/dp/B0026T4RTI/ref=pd_sim_m_12?tag=quirkey-20"
|
"url": "http://www.amazon.com/Bitte-Orca-Dirty-Projectors/dp/B0026T4RTI/ref=pd_sim_m_12?tag=quirkey-20"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id": "assimilation",
|
||||||
"title": "Periods of Mental Assimilation",
|
"title": "Periods of Mental Assimilation",
|
||||||
"artist": "Grigory Szondy",
|
"artist": "Grigory Szondy",
|
||||||
"image": "images/PeriodsofMentalAssimilation_t.jpg",
|
"image": "images/PeriodsofMentalAssimilation_t.jpg",
|
||||||
|
@ -32,6 +36,7 @@
|
||||||
"url": "http://www.amazon.com/Pains-Being-Pure-Heart/dp/B001LGXIDS/ref=pd_sim_m_44?tag=quirkey-20"
|
"url": "http://www.amazon.com/Pains-Being-Pure-Heart/dp/B001LGXIDS/ref=pd_sim_m_44?tag=quirkey-20"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id": "bankruptcy",
|
||||||
"title": "Keenly Developed Moral Bankruptcy",
|
"title": "Keenly Developed Moral Bankruptcy",
|
||||||
"artist": "Stealth Monkey Virus",
|
"artist": "Stealth Monkey Virus",
|
||||||
"image": "images/StealthMonkeyVirus_t.jpg",
|
"image": "images/StealthMonkeyVirus_t.jpg",
|
||||||
|
@ -40,6 +45,7 @@
|
||||||
"url": "http://www.amazon.com/Pains-Being-Pure-Heart/dp/B001LGXIDS/ref=pd_sim_m_44?tag=quirkey-20"
|
"url": "http://www.amazon.com/Pains-Being-Pure-Heart/dp/B001LGXIDS/ref=pd_sim_m_44?tag=quirkey-20"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id": "sparrow",
|
||||||
"title": "My Mistress's Sparrow is Dead",
|
"title": "My Mistress's Sparrow is Dead",
|
||||||
"artist": "Sums of Mongolia",
|
"artist": "Sums of Mongolia",
|
||||||
"image": "images/SumsofMagnolia_t.jpg",
|
"image": "images/SumsofMagnolia_t.jpg",
|
||||||
|
|
82
index.html
82
index.html
|
@ -1,64 +1,66 @@
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
<!DOCTYPE html>
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
||||||
<head>
|
<head>
|
||||||
<title>
|
<title>The Backbone Store</title>
|
||||||
The Backbone Store
|
<link charset="utf-8" href="jsonstore.css" rel="stylesheet" type="text/css" />
|
||||||
</title>
|
<script id="store_index_template" type="text/x-underscore-tmplate">
|
||||||
<link rel="stylesheet" href="jsonstore.css" type="text/css" media="screen" charset="utf-8" />
|
<h1>Product Catalog</h1>
|
||||||
|
<ul>
|
||||||
<script id="indexTmpl" type="text/x-jquery-tmpl">
|
<% for(i=0,l=products.length;i<l;++i) { p = products[i]; %>
|
||||||
<div class="item">
|
<li class="item">
|
||||||
<div class="item-image">
|
<div class="item-image">
|
||||||
<a href="#item/${cid}"><img src="${image}" alt="${title}" /></a>
|
<a href="#item/<%= p.id %>">
|
||||||
</div>
|
<img alt="<%= p.title %>" src="<%= p.image %>" />
|
||||||
<div class="item-artist">${artist}</div>
|
</a>
|
||||||
<div class="item-title">${title}</div>
|
|
||||||
<div class="item-price">$${price}</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
</li>
|
||||||
|
<div class="item-artist"><%= p.artist %></div>
|
||||||
|
<div class="item-title"><%= p.title %></div>
|
||||||
|
<div class="item-price">$<%= p.price %></div>
|
||||||
|
<% } %>
|
||||||
|
</ul>
|
||||||
</script>
|
</script>
|
||||||
|
<script id="store_item_template" type="text/x-underscore-template">
|
||||||
<script id="itemTmpl" type="text/x-jquery-tmpl">
|
<div class="item-detail"></div>
|
||||||
<div class="item-detail">
|
<div class="item-image">
|
||||||
<div class="item-image"><img src="${large_image}" alt="${title}" /></div>
|
<img alt="<%= title %>" src="<%= large_image %>" />
|
||||||
<div class="item-info">
|
</div>
|
||||||
<div class="item-artist">${artist}</div>
|
<div class="item-info"></div>
|
||||||
<div class="item-title">${title}</div>
|
<div class="item-artist"><%= artist %></div>
|
||||||
<div class="item-price">$${price}</div>
|
<div class="item-title"><%= title %></div>
|
||||||
<div class="item-form">
|
<div class="item-price">$<%= price %></div>
|
||||||
|
<div class="item-form"></div>
|
||||||
<form action="#/cart" method="post">
|
<form action="#/cart" method="post">
|
||||||
<input type="hidden" name="item_id" value="${cid}" />
|
|
||||||
<p>
|
<p>
|
||||||
<label>Quantity:</label>
|
<label>Quantity:</label>
|
||||||
<input type="text" size="2" name="quantity" value="1" class="uqf" />
|
<input class="uqf" name="quantity" size="2" type="text" value="1" />
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<input class="uq" type="submit" value="Add to Cart" />
|
||||||
</p>
|
</p>
|
||||||
<p><input type="submit" value="Add to Cart" class="uq" /></p>
|
|
||||||
</form>
|
</form>
|
||||||
|
<div class="item-link">
|
||||||
|
<a href="<%= url %>">Buy this item on Amazon</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="item-link"><a href="${url}">Buy this item on Amazon</a></div>
|
<div class="back-link">
|
||||||
<div class="back-link"><a href="#">« Back to Items</a></div>
|
<a href="#">« Back to Items</a>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
|
<script id="store_cart_template" type="text/x-underscore-template">
|
||||||
|
<p>Items: <%= count %> ($<%= cost %>)</p>
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div id="container">
|
<div id="container">
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<h1>
|
<h1>
|
||||||
The Backbone Store
|
The Backbone Store
|
||||||
</h1>
|
</h1>
|
||||||
|
<div class="cart-info"></div>
|
||||||
<div class="cart-info">
|
|
||||||
My Cart (<span class="cart-items">0</span> items)
|
|
||||||
</div>
|
</div>
|
||||||
|
<div id="main"></div>
|
||||||
</div>
|
</div>
|
||||||
|
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
|
||||||
<div id="main"> </div>
|
|
||||||
</div>
|
|
||||||
<script src="jquery-1.5.js" type="text/javascript"></script>
|
|
||||||
<script src="jquery.tmpl.min.js" type="text/javascript"></script>
|
|
||||||
<script src="underscore.js" type="text/javascript"></script>
|
<script src="underscore.js" type="text/javascript"></script>
|
||||||
<script src="backbone.js" type="text/javascript"></script>
|
<script src="backbone.js" type="text/javascript"></script>
|
||||||
<script src="store.js" type="text/javascript"></script>
|
<script src="store.js" type="text/javascript"></script>
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,283 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright © 2008
|
|
||||||
Rob Manson <roBman@MobileOnlineBusiness.com.au>,
|
|
||||||
Sean McCarthy <sean@MobileOnlineBusiness.com.au>
|
|
||||||
and http://SOAPjr.org
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
|
|
||||||
Acknoweldgements
|
|
||||||
----------------
|
|
||||||
This jQuery plugin utilises and requires the JSONSchema Validator
|
|
||||||
which is created by and Copyright (c) 2007 Kris Zyp SitePen (www.sitepen.com)
|
|
||||||
Licensed under the MIT (MIT-LICENSE.txt) licence.
|
|
||||||
|
|
||||||
JSONSchema Validator - Validates JavaScript objects using JSON Schemas
|
|
||||||
- http://jsonschema.googlecode.com/files/jsonschema-b2.js
|
|
||||||
(but check for the latest release)
|
|
||||||
|
|
||||||
For more information visit:
|
|
||||||
- http://www.json.com/json-schema-proposal/
|
|
||||||
- http://code.google.com/p/jsonschema/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Revision history
|
|
||||||
----------------
|
|
||||||
v1.0.1 - 4 Dec 2008
|
|
||||||
- Extended $.getValidJSON(params) API so you can define
|
|
||||||
{ schema : { send : xxx, receive : zzz } } so you can
|
|
||||||
optionally validate data on the way out and on the way in.
|
|
||||||
- Added tests to ensure that the schema key defined does really
|
|
||||||
exist as a typeof "object" in validJSONSchemas.
|
|
||||||
|
|
||||||
v1.0.0 - 3 Dec 2008
|
|
||||||
- initial release
|
|
||||||
|
|
||||||
|
|
||||||
Support
|
|
||||||
-------
|
|
||||||
For support, suggestions or general discussion please visit the #SOAPjr irc
|
|
||||||
channel on freenode.net or visit http://SOAPjr.org
|
|
||||||
|
|
||||||
|
|
||||||
Implementation overview
|
|
||||||
-----------------------
|
|
||||||
NOTE: This work contributes to the SOAPjr Data Model Definition project
|
|
||||||
that is designed to encourage the adoption and use of common JSON
|
|
||||||
data models with common SOAPjr APIs.
|
|
||||||
|
|
||||||
See http://soapjr.org/specs.html#dmd for more information
|
|
||||||
|
|
||||||
1. Include the jquery.js, jsonschema-b2.js and jquery.ValidJSON.js src files
|
|
||||||
into your html page - optionally include the jquery.SOAPjr.js plugin too
|
|
||||||
|
|
||||||
2. Add a call to the $(document).ready(function() { ... }) block to load
|
|
||||||
the relevant JSON Schema definition files you require
|
|
||||||
|
|
||||||
e.g.
|
|
||||||
- http://soapjr.org/schemas/SOAPjr_request
|
|
||||||
- see http://json-schema.org/shared.html for common formats
|
|
||||||
|
|
||||||
3. Get the JSON files/objects you require using the $.getValidJSON() API
|
|
||||||
|
|
||||||
4. Check the "result.valid" boolean and "result.errors" array for any
|
|
||||||
validation errors and if all is good then use "json" like normal.
|
|
||||||
|
|
||||||
5. Sleep well knowing your data is cleanly validated and conforms to your
|
|
||||||
chosen schema.
|
|
||||||
|
|
||||||
|
|
||||||
Code usage examples
|
|
||||||
-------------------
|
|
||||||
1. Include src files
|
|
||||||
|
|
||||||
<script type="text/javascript" src="jquery-1.2.6.js"></script>
|
|
||||||
<script type="text/javascript" src="jsonschema-b2.js"></script>
|
|
||||||
<script type="text/javascript" src="jquery.ValidJSON.js"></script>
|
|
||||||
|
|
||||||
|
|
||||||
2. Add calls to load schema files
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
$(document).ready(function() {
|
|
||||||
//load them from a URL
|
|
||||||
$.getValidJSONSchemas({
|
|
||||||
//id //url
|
|
||||||
"card" : "http://json-schema.org/card",
|
|
||||||
"calendar" : "http://json-schema.org/calendar"
|
|
||||||
});
|
|
||||||
|
|
||||||
//import them from a local var
|
|
||||||
$.setValidJSONSchemas({
|
|
||||||
//id //pre-populated vars (possibly included in .js files)
|
|
||||||
"geo" : geoschema_var,
|
|
||||||
"address" : addressschema_var
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
NOTE: cross-domain xhr restrictions apply - look at JSONP for cross domain requests
|
|
||||||
|
|
||||||
|
|
||||||
3. Get the JSON files/objects
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
function do_your_stuff() {
|
|
||||||
//collect data from a form or setup a stub
|
|
||||||
var params = { ... };
|
|
||||||
|
|
||||||
//get your data and validate it in one call
|
|
||||||
var xhr = $.getValidJSON({
|
|
||||||
"url" : url, //required
|
|
||||||
"data" : params, //optional
|
|
||||||
"callback" : "my_callback", //required (see point 4 below)
|
|
||||||
"schema" : { "send":"schema_id1", "receive":"schema_id2" } //optional - can be defined within the response JSON object using $schema
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
4. Check the validation results/errors
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
//setup your callback to handle the json data and any validation errors
|
|
||||||
function my_callback(result, json) {
|
|
||||||
if (result.valid) {
|
|
||||||
//json contains a valid XXX data object
|
|
||||||
} else {
|
|
||||||
for (var e in result.errors) {
|
|
||||||
//e.property tells you which value is invalid
|
|
||||||
//e.message is a human readable error message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
5. Relax
|
|
||||||
|
|
||||||
This plugin will let you do field level schema based validation.
|
|
||||||
You can also connect this to your form validation for standardised form data.
|
|
||||||
And you can use it in combination with the SOAPjr plugin to make sure
|
|
||||||
your apps handle metadata and complex/multiple errors seamlessly.
|
|
||||||
|
|
||||||
Isn't technology great! 8)
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
//very weak example location script 8) for instant gratification
|
|
||||||
//javascript:function f(j, v) { alert(v.valid+" : "+v.errors[0].message) };$.getValidJSON({ "url":"test.json", "data":{"longitude":333, "latitude":123},"callback":"f", "schema": {"send":"geothingy","receive":"geothingy"}});void(0)
|
|
||||||
|
|
||||||
var validJSONSchemas = {};
|
|
||||||
|
|
||||||
// Example simple JSON Schema - see http://json-schema.org/geo for latest version
|
|
||||||
var geoschema = {
|
|
||||||
"description":"A geographical coordinate",
|
|
||||||
"type":"object",
|
|
||||||
"properties":{
|
|
||||||
"latitude":{"type":"number"},
|
|
||||||
"longitude":{"type":"number"}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
jQuery.setValidJSONSchemas = function(input) {
|
|
||||||
for (var id in input) {
|
|
||||||
if (typeof input[id] == "object") {
|
|
||||||
validJSONSchemas[id] = input[id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jQuery.getValidJSONSchemas = function(input) {
|
|
||||||
for (var id in input) {
|
|
||||||
var url = input[id];
|
|
||||||
if (!url) {
|
|
||||||
throw("Not even a URL or filename was supplied!");
|
|
||||||
}
|
|
||||||
var xhr = $.ajax({
|
|
||||||
"url" : url,
|
|
||||||
"complete" : gotValidJSONSchema
|
|
||||||
});
|
|
||||||
xhr.getValidJSONSchemasConfig = {
|
|
||||||
"id" : id,
|
|
||||||
"url" : input[id]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function gotValidJSONSchema(xhr, status) {
|
|
||||||
var json = eval("("+xhr.responseText+")");
|
|
||||||
if (xhr.getValidJSONSchemasConfig != null) {
|
|
||||||
if (xhr.getValidJSONSchemasConfig.id) {
|
|
||||||
//this assumes one schema per json returned
|
|
||||||
var id = xhr.getValidJSONSchemasConfig.id;
|
|
||||||
var tmp = {};
|
|
||||||
tmp[id] = json;
|
|
||||||
$.setValidJSONSchemas(tmp);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw("No config data available for this schema");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jQuery.getValidJSON = function(input) { //url, data, callback and schema
|
|
||||||
//TODO: add a queue for xhr's
|
|
||||||
//var req = new Date().valueOf();
|
|
||||||
|
|
||||||
var url = input.url;
|
|
||||||
if (!url) {
|
|
||||||
throw("Not even a URL or filename was supplied!");
|
|
||||||
}
|
|
||||||
var data = input.data || null;
|
|
||||||
var callback = input.callback;
|
|
||||||
if (!callback) {
|
|
||||||
throw("No callback provided");
|
|
||||||
}
|
|
||||||
var schema = input.schema || null;
|
|
||||||
|
|
||||||
var config = {
|
|
||||||
"callback" : callback,
|
|
||||||
"schema" : schema
|
|
||||||
}
|
|
||||||
|
|
||||||
if (schema.send && data) {
|
|
||||||
if (typeof validJSONSchemas[schema.send] == "object") {
|
|
||||||
var result = JSONSchema.validate(data, validJSONSchemas[schema.send]);
|
|
||||||
if (!result.valid) {
|
|
||||||
var errors = [];
|
|
||||||
for (var e in result.errors) {
|
|
||||||
var tmp = result.errors[e].property+" : "+result.errors[e].message;
|
|
||||||
errors.push(tmp);
|
|
||||||
}
|
|
||||||
throw(errors.join(", \n"));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw("Invalid send schema defined - "+schema.send);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (schema.receive && typeof validJSONSchemas[schema.receive] != "object") {
|
|
||||||
throw("Invalid receive schema defined - "+schema.receive);
|
|
||||||
}
|
|
||||||
|
|
||||||
var ajax_options = {
|
|
||||||
"url" : url,
|
|
||||||
"complete" : gotValidJSON,
|
|
||||||
};
|
|
||||||
if (data) {
|
|
||||||
ajax_options.data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
var xhr = $.ajax(ajax_options);
|
|
||||||
|
|
||||||
xhr.getValidJSONConfig = config;
|
|
||||||
return xhr;
|
|
||||||
};
|
|
||||||
|
|
||||||
function gotValidJSON (xhr, status) {
|
|
||||||
var json = eval("("+xhr.responseText+")");
|
|
||||||
if (xhr.getValidJSONConfig != null) {
|
|
||||||
if (xhr.getValidJSONConfig.schema.receive) {
|
|
||||||
var valid = JSONSchema.validate(json, validJSONSchemas[xhr.getValidJSONConfig.schema.receive]);
|
|
||||||
} else {
|
|
||||||
var valid = JSONSchema.validate(json);
|
|
||||||
}
|
|
||||||
eval(xhr.getValidJSONConfig.callback+"(json,valid)");
|
|
||||||
} else {
|
|
||||||
throw("No data object was attached to the json response so no callback could be found");
|
|
||||||
}
|
|
||||||
}
|
|
223
store.js
223
store.js
|
@ -1,82 +1,119 @@
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
var Product = Backbone.Model.extend({
|
var Product = Backbone.Model.extend({})
|
||||||
toJSON: function() {
|
|
||||||
return _.extend(_.clone(this.attributes), {cid: this.cid})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var ProductCollection = Backbone.Collection.extend({
|
var ProductCollection = Backbone.Collection.extend({
|
||||||
model: Product,
|
model: Product,
|
||||||
|
|
||||||
|
initialize: function(models, options) {
|
||||||
|
this.url = options.url;
|
||||||
|
},
|
||||||
|
|
||||||
comparator: function(item) {
|
comparator: function(item) {
|
||||||
return item.get('title');
|
return item.get('title');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var CartItem = Backbone.Model.extend({
|
var Item = Backbone.Model.extend({
|
||||||
update: function(amount) {
|
update: function(amount) {
|
||||||
this.set({'quantity': this.get('quantity') + amount});
|
this.set({'quantity': this.get('quantity') + amount});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var Cart = Backbone.Collection.extend({
|
var ItemCollection = Backbone.Collection.extend({
|
||||||
model: CartItem,
|
model: Item,
|
||||||
getByProductId: function(pid) {
|
getOrCreateItemForProduct: function(product) {
|
||||||
return this.detect(function(obj) { return (obj.get('product').cid == pid); });
|
var i,
|
||||||
|
pid = product.get('id'),
|
||||||
|
o = this.detect(function(obj) {
|
||||||
|
return (obj.get('product').get('id') == pid);
|
||||||
|
});
|
||||||
|
if (o) {
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
i = new Item({'product': product, 'quantity': 0})
|
||||||
|
this.add(i, {silent: true})
|
||||||
|
return i;
|
||||||
},
|
},
|
||||||
|
getTotalCount: function() {
|
||||||
|
return this.reduce(function(memo, obj) {
|
||||||
|
return obj.get('quantity') + memo; }, 0);
|
||||||
|
},
|
||||||
|
getTotalCost: function() {
|
||||||
|
return this.reduce(function(memo, obj) {
|
||||||
|
return (obj.get('product').get('price') *
|
||||||
|
obj.get('quantity')) + memo; }, 0);
|
||||||
|
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var _BaseView = Backbone.View.extend({
|
||||||
var CartView = Backbone.View.extend({
|
parent: $('#main'),
|
||||||
el: $('.cart-info'),
|
className: 'viewport',
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.collection.bind('change', _.bind(this.render, this));
|
this.el = $(this.el);
|
||||||
|
this.el.hide();
|
||||||
|
this.parent.append(this.el);
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
|
hide: function() {
|
||||||
|
if (this.el.is(":visible") === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
promise = $.Deferred(_.bind(function(dfd) {
|
||||||
|
this.el.fadeOut('fast', dfd.resolve)}, this)).promise();
|
||||||
|
this.trigger('hide', this);
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
|
||||||
|
show: function() {
|
||||||
|
if (this.el.is(':visible')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
promise = $.Deferred(_.bind(function(dfd) {
|
||||||
|
this.el.fadeIn('fast', dfd.resolve) }, this)).promise();
|
||||||
|
|
||||||
|
this.trigger('show', this);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var ProductListView = _BaseView.extend({
|
||||||
|
id: 'productlistview',
|
||||||
|
template: $("#store_index_template").html(),
|
||||||
|
|
||||||
|
initialize: function(options) {
|
||||||
|
this.constructor.__super__.initialize.apply(this, [options])
|
||||||
|
this.collection.bind('reset', _.bind(this.render, this));
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
var sum = this.collection.reduce(function(m, n) { return m + n.get('quantity'); }, 0);
|
this.el.html(_.template(this.template,
|
||||||
this.el
|
{'products': this.collection.toJSON()}))
|
||||||
.find('.cart-items').text(sum).end()
|
|
||||||
.animate({paddingTop: '30px'})
|
|
||||||
.animate({paddingTop: '10px'});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
var ProductListView = Backbone.View.extend({
|
|
||||||
el: $('#main'),
|
|
||||||
indexTemplate: $("#indexTmpl").template(),
|
|
||||||
|
|
||||||
render: function() {
|
|
||||||
var self = this;
|
|
||||||
this.el.fadeOut('fast', function() {
|
|
||||||
self.el.html($.tmpl(self.indexTemplate, self.model.toJSON()));
|
|
||||||
self.el.fadeIn('fast');
|
|
||||||
});
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var ProductView = _BaseView.extend({
|
||||||
var ProductView = Backbone.View.extend({
|
id: 'productitemview',
|
||||||
el: $('#main'),
|
template: $("#store_item_template").html(),
|
||||||
itemTemplate: $("#itemTmpl").template(),
|
|
||||||
|
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.cart = options.cart;
|
this.constructor.__super__.initialize.apply(this, [options])
|
||||||
|
this.itemcollection = options.itemcollection;
|
||||||
|
this.item = this.itemcollection.getOrCreateItemForProduct(this.model);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
events: {
|
||||||
|
"keypress .uqf" : "updateOnEnter",
|
||||||
|
"click .uq" : "update",
|
||||||
|
},
|
||||||
|
|
||||||
update: function(e) {
|
update: function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var cart_item = this.cart.getByProductId(this.model.cid);
|
this.item.update(parseInt($('.uqf').val()));
|
||||||
if (_.isUndefined(cart_item)) {
|
|
||||||
cart_item = new CartItem({product: this.model, quantity: 0});
|
|
||||||
this.cart.add(cart_item, {silent: true});
|
|
||||||
}
|
|
||||||
cart_item.update(parseInt($('.uqf').val()));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
updateOnEnter: function(e) {
|
updateOnEnter: function(e) {
|
||||||
|
@ -85,68 +122,82 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
events: {
|
render: function() {
|
||||||
"keypress .uqf" : "updateOnEnter",
|
this.el.html(_.template(this.template, this.model.toJSON()));
|
||||||
"click .uq" : "update",
|
return this;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var CartWidget = Backbone.View.extend({
|
||||||
|
el: $('.cart-info'),
|
||||||
|
template: $('#store_cart_template').html(),
|
||||||
|
|
||||||
|
initialize: function() {
|
||||||
|
this.collection.bind('change', _.bind(this.render, this));
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
var self = this;
|
console.log(arguments);
|
||||||
this.el.fadeOut('fast', function() {
|
this.el.html(
|
||||||
self.el.html($.tmpl(self.itemTemplate, self.model.toJSON()));
|
_.template(this.template, {
|
||||||
self.el.fadeIn('fast');
|
'count': this.collection.getTotalCount(),
|
||||||
});
|
'cost': this.collection.getTotalCost()
|
||||||
return this;
|
})).animate({paddingTop: '30px'})
|
||||||
|
.animate({paddingTop: '10px'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var BackboneStore = Backbone.Router.extend({
|
||||||
|
views: {},
|
||||||
|
products: null,
|
||||||
|
cart: null,
|
||||||
|
|
||||||
var BackboneStore = Backbone.Controller.extend({
|
|
||||||
_index: null,
|
|
||||||
_products: null,
|
|
||||||
_cart :null,
|
|
||||||
routes: {
|
routes: {
|
||||||
"": "index",
|
"": "index",
|
||||||
"item/:id": "item",
|
"item/:id": "product",
|
||||||
},
|
},
|
||||||
|
|
||||||
initialize: function(data) {
|
initialize: function(data) {
|
||||||
this._cart = new Cart();
|
this.cart = new ItemCollection();
|
||||||
new CartView({collection: this._cart});
|
new CartWidget({collection: this.cart});
|
||||||
this._products = new ProductCollection(data);
|
|
||||||
this._index = new ProductListView({model: this._products});
|
this.products = new ProductCollection([], {
|
||||||
|
url: 'data/items.json'});
|
||||||
|
this.views = {
|
||||||
|
'_index': new ProductListView({
|
||||||
|
collection: this.products
|
||||||
|
})
|
||||||
|
};
|
||||||
|
$.when(this.products.fetch({reset: true}))
|
||||||
|
.then(function() { window.location.hash = ''; });
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
index: function() {
|
hideAllViews: function () {
|
||||||
this._index.render();
|
return _.select(
|
||||||
|
_.map(this.views, function(v) { return v.hide(); }),
|
||||||
|
function (t) { return t != null });
|
||||||
},
|
},
|
||||||
|
|
||||||
item: function(id) {
|
index: function() {
|
||||||
var product = this._products.getByCid(id);
|
var view = this.views['_index'];
|
||||||
if (_.isUndefined(product._view)) {
|
$.when(this.hideAllViews()).then(
|
||||||
product._view = new ProductView({model: product,
|
function() { return view.show(); });
|
||||||
cart: this._cart});
|
},
|
||||||
}
|
|
||||||
product._view.render();
|
product: function(id) {
|
||||||
|
var product, v, view;
|
||||||
|
product = this.products.detect(function(p) { return p.get('id') == (id); })
|
||||||
|
view = ((v = this.views)['item.' + id]) || (v['item.' + id] = (
|
||||||
|
new ProductView({model: product,
|
||||||
|
itemcollection: this.cart}).render()));
|
||||||
|
$.when(this.hideAllViews()).then(
|
||||||
|
function() { view.show(); });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
var fetch_items = function() {
|
new BackboneStore();
|
||||||
return $.ajax({
|
|
||||||
url: 'data/items.json',
|
|
||||||
data: {},
|
|
||||||
contentType: "application/json; charset=utf-8",
|
|
||||||
dataType: "json"
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
$.when(fetch_items()).then(function(data) {
|
|
||||||
new BackboneStore(data);
|
|
||||||
Backbone.history.start();
|
Backbone.history.start();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
}).call(this);
|
}).call(this);
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// Underscore.js 1.1.2
|
|
||||||
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
|
||||||
// Underscore is freely distributable under the MIT license.
|
|
||||||
// Portions of Underscore are inspired or borrowed from Prototype,
|
|
||||||
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
|
||||||
// For all details and documentation:
|
|
||||||
// http://documentcloud.github.com/underscore
|
|
||||||
(function(){var o=this,A=o._,r=typeof StopIteration!=="undefined"?StopIteration:"__break__",k=Array.prototype,m=Object.prototype,i=k.slice,B=k.unshift,C=m.toString,p=m.hasOwnProperty,s=k.forEach,t=k.map,u=k.reduce,v=k.reduceRight,w=k.filter,x=k.every,y=k.some,n=k.indexOf,z=k.lastIndexOf;m=Array.isArray;var D=Object.keys,c=function(a){return new l(a)};if(typeof exports!=="undefined")exports._=c;o._=c;c.VERSION="1.1.2";var j=c.each=c.forEach=function(a,b,d){try{if(s&&a.forEach===s)a.forEach(b,d);else if(c.isNumber(a.length))for(var e=
|
|
||||||
0,f=a.length;e<f;e++)b.call(d,a[e],e,a);else for(e in a)p.call(a,e)&&b.call(d,a[e],e,a)}catch(g){if(g!=r)throw g;}return a};c.map=function(a,b,d){if(t&&a.map===t)return a.map(b,d);var e=[];j(a,function(f,g,h){e[e.length]=b.call(d,f,g,h)});return e};c.reduce=c.foldl=c.inject=function(a,b,d,e){var f=d!==void 0;if(u&&a.reduce===u){if(e)b=c.bind(b,e);return f?a.reduce(b,d):a.reduce(b)}j(a,function(g,h,E){d=!f&&h===0?g:b.call(e,d,g,h,E)});return d};c.reduceRight=c.foldr=function(a,b,d,e){if(v&&a.reduceRight===
|
|
||||||
v){if(e)b=c.bind(b,e);return d!==void 0?a.reduceRight(b,d):a.reduceRight(b)}a=(c.isArray(a)?a.slice():c.toArray(a)).reverse();return c.reduce(a,b,d,e)};c.find=c.detect=function(a,b,d){var e;j(a,function(f,g,h){if(b.call(d,f,g,h)){e=f;c.breakLoop()}});return e};c.filter=c.select=function(a,b,d){if(w&&a.filter===w)return a.filter(b,d);var e=[];j(a,function(f,g,h){if(b.call(d,f,g,h))e[e.length]=f});return e};c.reject=function(a,b,d){var e=[];j(a,function(f,g,h){b.call(d,f,g,h)||(e[e.length]=f)});return e};
|
|
||||||
c.every=c.all=function(a,b,d){b=b||c.identity;if(x&&a.every===x)return a.every(b,d);var e=true;j(a,function(f,g,h){(e=e&&b.call(d,f,g,h))||c.breakLoop()});return e};c.some=c.any=function(a,b,d){b=b||c.identity;if(y&&a.some===y)return a.some(b,d);var e=false;j(a,function(f,g,h){if(e=b.call(d,f,g,h))c.breakLoop()});return e};c.include=c.contains=function(a,b){if(n&&a.indexOf===n)return a.indexOf(b)!=-1;var d=false;j(a,function(e){if(d=e===b)c.breakLoop()});return d};c.invoke=function(a,b){var d=i.call(arguments,
|
|
||||||
2);return c.map(a,function(e){return(b?e[b]:e).apply(e,d)})};c.pluck=function(a,b){return c.map(a,function(d){return d[b]})};c.max=function(a,b,d){if(!b&&c.isArray(a))return Math.max.apply(Math,a);var e={computed:-Infinity};j(a,function(f,g,h){g=b?b.call(d,f,g,h):f;g>=e.computed&&(e={value:f,computed:g})});return e.value};c.min=function(a,b,d){if(!b&&c.isArray(a))return Math.min.apply(Math,a);var e={computed:Infinity};j(a,function(f,g,h){g=b?b.call(d,f,g,h):f;g<e.computed&&(e={value:f,computed:g})});
|
|
||||||
return e.value};c.sortBy=function(a,b,d){return c.pluck(c.map(a,function(e,f,g){return{value:e,criteria:b.call(d,e,f,g)}}).sort(function(e,f){var g=e.criteria,h=f.criteria;return g<h?-1:g>h?1:0}),"value")};c.sortedIndex=function(a,b,d){d=d||c.identity;for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(b)?e=g+1:f=g}return e};c.toArray=function(a){if(!a)return[];if(a.toArray)return a.toArray();if(c.isArray(a))return a;if(c.isArguments(a))return i.call(a);return c.values(a)};c.size=function(a){return c.toArray(a).length};
|
|
||||||
c.first=c.head=function(a,b,d){return b&&!d?i.call(a,0,b):a[0]};c.rest=c.tail=function(a,b,d){return i.call(a,c.isUndefined(b)||d?1:b)};c.last=function(a){return a[a.length-1]};c.compact=function(a){return c.filter(a,function(b){return!!b})};c.flatten=function(a){return c.reduce(a,function(b,d){if(c.isArray(d))return b.concat(c.flatten(d));b[b.length]=d;return b},[])};c.without=function(a){var b=i.call(arguments,1);return c.filter(a,function(d){return!c.include(b,d)})};c.uniq=c.unique=function(a,
|
|
||||||
b){return c.reduce(a,function(d,e,f){if(0==f||(b===true?c.last(d)!=e:!c.include(d,e)))d[d.length]=e;return d},[])};c.intersect=function(a){var b=i.call(arguments,1);return c.filter(c.uniq(a),function(d){return c.every(b,function(e){return c.indexOf(e,d)>=0})})};c.zip=function(){for(var a=i.call(arguments),b=c.max(c.pluck(a,"length")),d=Array(b),e=0;e<b;e++)d[e]=c.pluck(a,""+e);return d};c.indexOf=function(a,b){if(n&&a.indexOf===n)return a.indexOf(b);for(var d=0,e=a.length;d<e;d++)if(a[d]===b)return d;
|
|
||||||
return-1};c.lastIndexOf=function(a,b){if(z&&a.lastIndexOf===z)return a.lastIndexOf(b);for(var d=a.length;d--;)if(a[d]===b)return d;return-1};c.range=function(a,b,d){var e=i.call(arguments),f=e.length<=1;a=f?0:e[0];b=f?e[0]:e[1];d=e[2]||1;e=Math.max(Math.ceil((b-a)/d),0);f=0;for(var g=Array(e);f<e;){g[f++]=a;a+=d}return g};c.bind=function(a,b){var d=i.call(arguments,2);return function(){return a.apply(b||{},d.concat(i.call(arguments)))}};c.bindAll=function(a){var b=i.call(arguments,1);if(b.length==
|
|
||||||
0)b=c.functions(a);j(b,function(d){a[d]=c.bind(a[d],a)});return a};c.memoize=function(a,b){var d={};b=b||c.identity;return function(){var e=b.apply(this,arguments);return e in d?d[e]:d[e]=a.apply(this,arguments)}};c.delay=function(a,b){var d=i.call(arguments,2);return setTimeout(function(){return a.apply(a,d)},b)};c.defer=function(a){return c.delay.apply(c,[a,1].concat(i.call(arguments,1)))};c.wrap=function(a,b){return function(){var d=[a].concat(i.call(arguments));return b.apply(b,d)}};c.compose=
|
|
||||||
function(){var a=i.call(arguments);return function(){for(var b=i.call(arguments),d=a.length-1;d>=0;d--)b=[a[d].apply(this,b)];return b[0]}};c.keys=D||function(a){if(c.isArray(a))return c.range(0,a.length);var b=[],d;for(d in a)if(p.call(a,d))b[b.length]=d;return b};c.values=function(a){return c.map(a,c.identity)};c.functions=c.methods=function(a){return c.filter(c.keys(a),function(b){return c.isFunction(a[b])}).sort()};c.extend=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});
|
|
||||||
return a};c.clone=function(a){return c.isArray(a)?a.slice():c.extend({},a)};c.tap=function(a,b){b(a);return a};c.isEqual=function(a,b){if(a===b)return true;var d=typeof a;if(d!=typeof b)return false;if(a==b)return true;if(!a&&b||a&&!b)return false;if(a.isEqual)return a.isEqual(b);if(c.isDate(a)&&c.isDate(b))return a.getTime()===b.getTime();if(c.isNaN(a)&&c.isNaN(b))return false;if(c.isRegExp(a)&&c.isRegExp(b))return a.source===b.source&&a.global===b.global&&a.ignoreCase===b.ignoreCase&&a.multiline===
|
|
||||||
b.multiline;if(d!=="object")return false;if(a.length&&a.length!==b.length)return false;d=c.keys(a);var e=c.keys(b);if(d.length!=e.length)return false;for(var f in a)if(!(f in b)||!c.isEqual(a[f],b[f]))return false;return true};c.isEmpty=function(a){if(c.isArray(a)||c.isString(a))return a.length===0;for(var b in a)if(p.call(a,b))return false;return true};c.isElement=function(a){return!!(a&&a.nodeType==1)};c.isArray=m||function(a){return!!(a&&a.concat&&a.unshift&&!a.callee)};c.isArguments=function(a){return!!(a&&
|
|
||||||
a.callee)};c.isFunction=function(a){return!!(a&&a.constructor&&a.call&&a.apply)};c.isString=function(a){return!!(a===""||a&&a.charCodeAt&&a.substr)};c.isNumber=function(a){return a===+a||C.call(a)==="[object Number]"};c.isBoolean=function(a){return a===true||a===false};c.isDate=function(a){return!!(a&&a.getTimezoneOffset&&a.setUTCFullYear)};c.isRegExp=function(a){return!!(a&&a.test&&a.exec&&(a.ignoreCase||a.ignoreCase===false))};c.isNaN=function(a){return c.isNumber(a)&&isNaN(a)};c.isNull=function(a){return a===
|
|
||||||
null};c.isUndefined=function(a){return typeof a=="undefined"};c.noConflict=function(){o._=A;return this};c.identity=function(a){return a};c.times=function(a,b,d){for(var e=0;e<a;e++)b.call(d,e)};c.breakLoop=function(){throw r;};c.mixin=function(a){j(c.functions(a),function(b){F(b,c[b]=a[b])})};var G=0;c.uniqueId=function(a){var b=G++;return a?a+b:b};c.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g};c.template=function(a,b){var d=c.templateSettings;d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+
|
|
||||||
a.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(d.interpolate,function(e,f){return"',"+f.replace(/\\'/g,"'")+",'"}).replace(d.evaluate||null,function(e,f){return"');"+f.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');";d=new Function("obj",d);return b?d(b):d};var l=function(a){this._wrapped=a};c.prototype=l.prototype;var q=function(a,b){return b?c(a).chain():a},F=function(a,b){l.prototype[a]=function(){var d=
|
|
||||||
i.call(arguments);B.call(d,this._wrapped);return q(b.apply(c,d),this._chain)}};c.mixin(c);j(["pop","push","reverse","shift","sort","splice","unshift"],function(a){var b=k[a];l.prototype[a]=function(){b.apply(this._wrapped,arguments);return q(this._wrapped,this._chain)}});j(["concat","join","slice"],function(a){var b=k[a];l.prototype[a]=function(){return q(b.apply(this._wrapped,arguments),this._chain)}});l.prototype.chain=function(){this._chain=true;return this};l.prototype.value=function(){return this._wrapped}})();
|
|
275
underscore.js
275
underscore.js
|
@ -1,5 +1,5 @@
|
||||||
// Underscore.js 1.1.2
|
// Underscore.js 1.1.7
|
||||||
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
// (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.
|
||||||
// Underscore is freely distributable under the MIT license.
|
// Underscore is freely distributable under the MIT license.
|
||||||
// Portions of Underscore are inspired or borrowed from Prototype,
|
// Portions of Underscore are inspired or borrowed from Prototype,
|
||||||
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
||||||
|
@ -17,11 +17,11 @@
|
||||||
// Save the previous value of the `_` variable.
|
// Save the previous value of the `_` variable.
|
||||||
var previousUnderscore = root._;
|
var previousUnderscore = root._;
|
||||||
|
|
||||||
// Establish the object that gets thrown to break out of a loop iteration.
|
// Establish the object that gets returned to break out of a loop iteration.
|
||||||
var breaker = typeof StopIteration !== 'undefined' ? StopIteration : '__break__';
|
var breaker = {};
|
||||||
|
|
||||||
// Save bytes in the minified (but not gzipped) version:
|
// Save bytes in the minified (but not gzipped) version:
|
||||||
var ArrayProto = Array.prototype, ObjProto = Object.prototype;
|
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
|
||||||
|
|
||||||
// Create quick reference variables for speed access to core prototypes.
|
// Create quick reference variables for speed access to core prototypes.
|
||||||
var slice = ArrayProto.slice,
|
var slice = ArrayProto.slice,
|
||||||
|
@ -42,48 +42,55 @@
|
||||||
nativeIndexOf = ArrayProto.indexOf,
|
nativeIndexOf = ArrayProto.indexOf,
|
||||||
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
||||||
nativeIsArray = Array.isArray,
|
nativeIsArray = Array.isArray,
|
||||||
nativeKeys = Object.keys;
|
nativeKeys = Object.keys,
|
||||||
|
nativeBind = FuncProto.bind;
|
||||||
|
|
||||||
// Create a safe reference to the Underscore object for use below.
|
// Create a safe reference to the Underscore object for use below.
|
||||||
var _ = function(obj) { return new wrapper(obj); };
|
var _ = function(obj) { return new wrapper(obj); };
|
||||||
|
|
||||||
// Export the Underscore object for **CommonJS**.
|
// Export the Underscore object for **CommonJS**, with backwards-compatibility
|
||||||
if (typeof exports !== 'undefined') exports._ = _;
|
// for the old `require()` API. If we're not in CommonJS, add `_` to the
|
||||||
|
// global object.
|
||||||
// Export Underscore to the global scope.
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
root._ = _;
|
module.exports = _;
|
||||||
|
_._ = _;
|
||||||
|
} else {
|
||||||
|
// Exported as a string, for Closure Compiler "advanced" mode.
|
||||||
|
root['_'] = _;
|
||||||
|
}
|
||||||
|
|
||||||
// Current version.
|
// Current version.
|
||||||
_.VERSION = '1.1.2';
|
_.VERSION = '1.1.7';
|
||||||
|
|
||||||
// Collection Functions
|
// Collection Functions
|
||||||
// --------------------
|
// --------------------
|
||||||
|
|
||||||
// The cornerstone, an `each` implementation, aka `forEach`.
|
// The cornerstone, an `each` implementation, aka `forEach`.
|
||||||
// Handles objects implementing `forEach`, arrays, and raw objects.
|
// Handles objects with the built-in `forEach`, arrays, and raw objects.
|
||||||
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
||||||
var each = _.each = _.forEach = function(obj, iterator, context) {
|
var each = _.each = _.forEach = function(obj, iterator, context) {
|
||||||
try {
|
if (obj == null) return;
|
||||||
if (nativeForEach && obj.forEach === nativeForEach) {
|
if (nativeForEach && obj.forEach === nativeForEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (_.isNumber(obj.length)) {
|
} else if (obj.length === +obj.length) {
|
||||||
for (var i = 0, l = obj.length; i < l; i++) iterator.call(context, obj[i], i, obj);
|
for (var i = 0, l = obj.length; i < l; i++) {
|
||||||
|
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for (var key in obj) {
|
for (var key in obj) {
|
||||||
if (hasOwnProperty.call(obj, key)) iterator.call(context, obj[key], key, obj);
|
if (hasOwnProperty.call(obj, key)) {
|
||||||
|
if (iterator.call(context, obj[key], key, obj) === breaker) return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(e) {
|
|
||||||
if (e != breaker) throw e;
|
|
||||||
}
|
}
|
||||||
return obj;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return the results of applying the iterator to each element.
|
// Return the results of applying the iterator to each element.
|
||||||
// Delegates to **ECMAScript 5**'s native `map` if available.
|
// Delegates to **ECMAScript 5**'s native `map` if available.
|
||||||
_.map = function(obj, iterator, context) {
|
_.map = function(obj, iterator, context) {
|
||||||
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
|
||||||
var results = [];
|
var results = [];
|
||||||
|
if (obj == null) return results;
|
||||||
|
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
results[results.length] = iterator.call(context, value, index, list);
|
results[results.length] = iterator.call(context, value, index, list);
|
||||||
});
|
});
|
||||||
|
@ -94,23 +101,27 @@
|
||||||
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
||||||
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
||||||
var initial = memo !== void 0;
|
var initial = memo !== void 0;
|
||||||
|
if (obj == null) obj = [];
|
||||||
if (nativeReduce && obj.reduce === nativeReduce) {
|
if (nativeReduce && obj.reduce === nativeReduce) {
|
||||||
if (context) iterator = _.bind(iterator, context);
|
if (context) iterator = _.bind(iterator, context);
|
||||||
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
||||||
}
|
}
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
if (!initial && index === 0) {
|
if (!initial) {
|
||||||
memo = value;
|
memo = value;
|
||||||
|
initial = true;
|
||||||
} else {
|
} else {
|
||||||
memo = iterator.call(context, memo, value, index, list);
|
memo = iterator.call(context, memo, value, index, list);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (!initial) throw new TypeError("Reduce of empty array with no initial value");
|
||||||
return memo;
|
return memo;
|
||||||
};
|
};
|
||||||
|
|
||||||
// The right-associative version of reduce, also known as `foldr`.
|
// The right-associative version of reduce, also known as `foldr`.
|
||||||
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
|
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
|
||||||
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
||||||
|
if (obj == null) obj = [];
|
||||||
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
||||||
if (context) iterator = _.bind(iterator, context);
|
if (context) iterator = _.bind(iterator, context);
|
||||||
return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
||||||
|
@ -122,10 +133,10 @@
|
||||||
// Return the first value which passes a truth test. Aliased as `detect`.
|
// Return the first value which passes a truth test. Aliased as `detect`.
|
||||||
_.find = _.detect = function(obj, iterator, context) {
|
_.find = _.detect = function(obj, iterator, context) {
|
||||||
var result;
|
var result;
|
||||||
each(obj, function(value, index, list) {
|
any(obj, function(value, index, list) {
|
||||||
if (iterator.call(context, value, index, list)) {
|
if (iterator.call(context, value, index, list)) {
|
||||||
result = value;
|
result = value;
|
||||||
_.breakLoop();
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
|
@ -135,8 +146,9 @@
|
||||||
// Delegates to **ECMAScript 5**'s native `filter` if available.
|
// Delegates to **ECMAScript 5**'s native `filter` if available.
|
||||||
// Aliased as `select`.
|
// Aliased as `select`.
|
||||||
_.filter = _.select = function(obj, iterator, context) {
|
_.filter = _.select = function(obj, iterator, context) {
|
||||||
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
|
||||||
var results = [];
|
var results = [];
|
||||||
|
if (obj == null) return results;
|
||||||
|
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
||||||
});
|
});
|
||||||
|
@ -146,6 +158,7 @@
|
||||||
// Return all the elements for which a truth test fails.
|
// Return all the elements for which a truth test fails.
|
||||||
_.reject = function(obj, iterator, context) {
|
_.reject = function(obj, iterator, context) {
|
||||||
var results = [];
|
var results = [];
|
||||||
|
if (obj == null) return results;
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
||||||
});
|
});
|
||||||
|
@ -156,11 +169,11 @@
|
||||||
// Delegates to **ECMAScript 5**'s native `every` if available.
|
// Delegates to **ECMAScript 5**'s native `every` if available.
|
||||||
// Aliased as `all`.
|
// Aliased as `all`.
|
||||||
_.every = _.all = function(obj, iterator, context) {
|
_.every = _.all = function(obj, iterator, context) {
|
||||||
iterator = iterator || _.identity;
|
|
||||||
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
|
||||||
var result = true;
|
var result = true;
|
||||||
|
if (obj == null) return result;
|
||||||
|
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop();
|
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
@ -168,23 +181,25 @@
|
||||||
// Determine if at least one element in the object matches a truth test.
|
// Determine if at least one element in the object matches a truth test.
|
||||||
// Delegates to **ECMAScript 5**'s native `some` if available.
|
// Delegates to **ECMAScript 5**'s native `some` if available.
|
||||||
// Aliased as `any`.
|
// Aliased as `any`.
|
||||||
_.some = _.any = function(obj, iterator, context) {
|
var any = _.some = _.any = function(obj, iterator, context) {
|
||||||
iterator = iterator || _.identity;
|
iterator = iterator || _.identity;
|
||||||
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
|
||||||
var result = false;
|
var result = false;
|
||||||
|
if (obj == null) return result;
|
||||||
|
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
if (result = iterator.call(context, value, index, list)) _.breakLoop();
|
if (result |= iterator.call(context, value, index, list)) return breaker;
|
||||||
});
|
});
|
||||||
return result;
|
return !!result;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Determine if a given value is included in the array or object using `===`.
|
// Determine if a given value is included in the array or object using `===`.
|
||||||
// Aliased as `contains`.
|
// Aliased as `contains`.
|
||||||
_.include = _.contains = function(obj, target) {
|
_.include = _.contains = function(obj, target) {
|
||||||
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
|
||||||
var found = false;
|
var found = false;
|
||||||
each(obj, function(value) {
|
if (obj == null) return found;
|
||||||
if (found = value === target) _.breakLoop();
|
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
||||||
|
any(obj, function(value) {
|
||||||
|
if (found = value === target) return true;
|
||||||
});
|
});
|
||||||
return found;
|
return found;
|
||||||
};
|
};
|
||||||
|
@ -193,7 +208,7 @@
|
||||||
_.invoke = function(obj, method) {
|
_.invoke = function(obj, method) {
|
||||||
var args = slice.call(arguments, 2);
|
var args = slice.call(arguments, 2);
|
||||||
return _.map(obj, function(value) {
|
return _.map(obj, function(value) {
|
||||||
return (method ? value[method] : value).apply(value, args);
|
return (method.call ? method || value : value[method]).apply(value, args);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -237,10 +252,20 @@
|
||||||
}), 'value');
|
}), 'value');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Groups the object's values by a criterion produced by an iterator
|
||||||
|
_.groupBy = function(obj, iterator) {
|
||||||
|
var result = {};
|
||||||
|
each(obj, function(value, index) {
|
||||||
|
var key = iterator(value, index);
|
||||||
|
(result[key] || (result[key] = [])).push(value);
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
// Use a comparator function to figure out at what index an object should
|
// Use a comparator function to figure out at what index an object should
|
||||||
// be inserted so as to maintain order. Uses binary search.
|
// be inserted so as to maintain order. Uses binary search.
|
||||||
_.sortedIndex = function(array, obj, iterator) {
|
_.sortedIndex = function(array, obj, iterator) {
|
||||||
iterator = iterator || _.identity;
|
iterator || (iterator = _.identity);
|
||||||
var low = 0, high = array.length;
|
var low = 0, high = array.length;
|
||||||
while (low < high) {
|
while (low < high) {
|
||||||
var mid = (low + high) >> 1;
|
var mid = (low + high) >> 1;
|
||||||
|
@ -253,7 +278,7 @@
|
||||||
_.toArray = function(iterable) {
|
_.toArray = function(iterable) {
|
||||||
if (!iterable) return [];
|
if (!iterable) return [];
|
||||||
if (iterable.toArray) return iterable.toArray();
|
if (iterable.toArray) return iterable.toArray();
|
||||||
if (_.isArray(iterable)) return iterable;
|
if (_.isArray(iterable)) return slice.call(iterable);
|
||||||
if (_.isArguments(iterable)) return slice.call(iterable);
|
if (_.isArguments(iterable)) return slice.call(iterable);
|
||||||
return _.values(iterable);
|
return _.values(iterable);
|
||||||
};
|
};
|
||||||
|
@ -270,7 +295,7 @@
|
||||||
// values in the array. Aliased as `head`. The **guard** check allows it to work
|
// values in the array. Aliased as `head`. The **guard** check allows it to work
|
||||||
// with `_.map`.
|
// with `_.map`.
|
||||||
_.first = _.head = function(array, n, guard) {
|
_.first = _.head = function(array, n, guard) {
|
||||||
return n && !guard ? slice.call(array, 0, n) : array[0];
|
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns everything but the first entry of the array. Aliased as `tail`.
|
// Returns everything but the first entry of the array. Aliased as `tail`.
|
||||||
|
@ -278,7 +303,7 @@
|
||||||
// the rest of the values in the array from that index onward. The **guard**
|
// the rest of the values in the array from that index onward. The **guard**
|
||||||
// check allows it to work with `_.map`.
|
// check allows it to work with `_.map`.
|
||||||
_.rest = _.tail = function(array, index, guard) {
|
_.rest = _.tail = function(array, index, guard) {
|
||||||
return slice.call(array, _.isUndefined(index) || guard ? 1 : index);
|
return slice.call(array, (index == null) || guard ? 1 : index);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the last element of an array.
|
// Get the last element of an array.
|
||||||
|
@ -302,8 +327,7 @@
|
||||||
|
|
||||||
// Return a version of the array that does not contain the specified value(s).
|
// Return a version of the array that does not contain the specified value(s).
|
||||||
_.without = function(array) {
|
_.without = function(array) {
|
||||||
var values = slice.call(arguments, 1);
|
return _.difference(array, slice.call(arguments, 1));
|
||||||
return _.filter(array, function(value){ return !_.include(values, value); });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Produce a duplicate-free version of the array. If the array has already
|
// Produce a duplicate-free version of the array. If the array has already
|
||||||
|
@ -316,9 +340,15 @@
|
||||||
}, []);
|
}, []);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Produce an array that contains the union: each distinct element from all of
|
||||||
|
// the passed-in arrays.
|
||||||
|
_.union = function() {
|
||||||
|
return _.uniq(_.flatten(arguments));
|
||||||
|
};
|
||||||
|
|
||||||
// Produce an array that contains every item shared between all the
|
// Produce an array that contains every item shared between all the
|
||||||
// passed-in arrays.
|
// passed-in arrays. (Aliased as "intersect" for back-compat.)
|
||||||
_.intersect = function(array) {
|
_.intersection = _.intersect = function(array) {
|
||||||
var rest = slice.call(arguments, 1);
|
var rest = slice.call(arguments, 1);
|
||||||
return _.filter(_.uniq(array), function(item) {
|
return _.filter(_.uniq(array), function(item) {
|
||||||
return _.every(rest, function(other) {
|
return _.every(rest, function(other) {
|
||||||
|
@ -327,6 +357,12 @@
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Take the difference between one array and another.
|
||||||
|
// Only the elements present in just the first array will remain.
|
||||||
|
_.difference = function(array, other) {
|
||||||
|
return _.filter(array, function(value){ return !_.include(other, value); });
|
||||||
|
};
|
||||||
|
|
||||||
// Zip together multiple lists into a single array -- elements that share
|
// Zip together multiple lists into a single array -- elements that share
|
||||||
// an index go together.
|
// an index go together.
|
||||||
_.zip = function() {
|
_.zip = function() {
|
||||||
|
@ -338,18 +374,27 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
|
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
|
||||||
// we need this function. Return the position of the first occurence of an
|
// we need this function. Return the position of the first occurrence of an
|
||||||
// item in an array, or -1 if the item is not included in the array.
|
// item in an array, or -1 if the item is not included in the array.
|
||||||
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
|
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
|
||||||
_.indexOf = function(array, item) {
|
// If the array is large and already in sort order, pass `true`
|
||||||
|
// for **isSorted** to use binary search.
|
||||||
|
_.indexOf = function(array, item, isSorted) {
|
||||||
|
if (array == null) return -1;
|
||||||
|
var i, l;
|
||||||
|
if (isSorted) {
|
||||||
|
i = _.sortedIndex(array, item);
|
||||||
|
return array[i] === item ? i : -1;
|
||||||
|
}
|
||||||
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
||||||
for (var i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
|
for (i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
||||||
_.lastIndexOf = function(array, item) {
|
_.lastIndexOf = function(array, item) {
|
||||||
|
if (array == null) return -1;
|
||||||
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
|
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
|
||||||
var i = array.length;
|
var i = array.length;
|
||||||
while (i--) if (array[i] === item) return i;
|
while (i--) if (array[i] === item) return i;
|
||||||
|
@ -360,18 +405,21 @@
|
||||||
// the native Python `range()` function. See
|
// the native Python `range()` function. See
|
||||||
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
||||||
_.range = function(start, stop, step) {
|
_.range = function(start, stop, step) {
|
||||||
var args = slice.call(arguments),
|
if (arguments.length <= 1) {
|
||||||
solo = args.length <= 1,
|
stop = start || 0;
|
||||||
start = solo ? 0 : args[0],
|
start = 0;
|
||||||
stop = solo ? args[0] : args[1],
|
}
|
||||||
step = args[2] || 1,
|
step = arguments[2] || 1;
|
||||||
len = Math.max(Math.ceil((stop - start) / step), 0),
|
|
||||||
idx = 0,
|
var len = Math.max(Math.ceil((stop - start) / step), 0);
|
||||||
range = new Array(len);
|
var idx = 0;
|
||||||
while (idx < len) {
|
var range = new Array(len);
|
||||||
|
|
||||||
|
while(idx < len) {
|
||||||
range[idx++] = start;
|
range[idx++] = start;
|
||||||
start += step;
|
start += step;
|
||||||
}
|
}
|
||||||
|
|
||||||
return range;
|
return range;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -380,10 +428,13 @@
|
||||||
|
|
||||||
// Create a function bound to a given object (assigning `this`, and arguments,
|
// Create a function bound to a given object (assigning `this`, and arguments,
|
||||||
// optionally). Binding with arguments is also known as `curry`.
|
// optionally). Binding with arguments is also known as `curry`.
|
||||||
|
// Delegates to **ECMAScript 5**'s native `Function.bind` if available.
|
||||||
|
// We check for `func.bind` first, to fail fast when `func` is undefined.
|
||||||
_.bind = function(func, obj) {
|
_.bind = function(func, obj) {
|
||||||
|
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
|
||||||
var args = slice.call(arguments, 2);
|
var args = slice.call(arguments, 2);
|
||||||
return function() {
|
return function() {
|
||||||
return func.apply(obj || {}, args.concat(slice.call(arguments)));
|
return func.apply(obj, args.concat(slice.call(arguments)));
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -399,10 +450,10 @@
|
||||||
// Memoize an expensive function by storing its results.
|
// Memoize an expensive function by storing its results.
|
||||||
_.memoize = function(func, hasher) {
|
_.memoize = function(func, hasher) {
|
||||||
var memo = {};
|
var memo = {};
|
||||||
hasher = hasher || _.identity;
|
hasher || (hasher = _.identity);
|
||||||
return function() {
|
return function() {
|
||||||
var key = hasher.apply(this, arguments);
|
var key = hasher.apply(this, arguments);
|
||||||
return key in memo ? memo[key] : (memo[key] = func.apply(this, arguments));
|
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -419,13 +470,51 @@
|
||||||
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Internal function used to implement `_.throttle` and `_.debounce`.
|
||||||
|
var limit = function(func, wait, debounce) {
|
||||||
|
var timeout;
|
||||||
|
return function() {
|
||||||
|
var context = this, args = arguments;
|
||||||
|
var throttler = function() {
|
||||||
|
timeout = null;
|
||||||
|
func.apply(context, args);
|
||||||
|
};
|
||||||
|
if (debounce) clearTimeout(timeout);
|
||||||
|
if (debounce || !timeout) timeout = setTimeout(throttler, wait);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns a function, that, when invoked, will only be triggered at most once
|
||||||
|
// during a given window of time.
|
||||||
|
_.throttle = function(func, wait) {
|
||||||
|
return limit(func, wait, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns a function, that, as long as it continues to be invoked, will not
|
||||||
|
// be triggered. The function will be called after it stops being called for
|
||||||
|
// N milliseconds.
|
||||||
|
_.debounce = function(func, wait) {
|
||||||
|
return limit(func, wait, true);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns a function that will be executed at most one time, no matter how
|
||||||
|
// often you call it. Useful for lazy initialization.
|
||||||
|
_.once = function(func) {
|
||||||
|
var ran = false, memo;
|
||||||
|
return function() {
|
||||||
|
if (ran) return memo;
|
||||||
|
ran = true;
|
||||||
|
return memo = func.apply(this, arguments);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// Returns the first function passed as an argument to the second,
|
// Returns the first function passed as an argument to the second,
|
||||||
// allowing you to adjust arguments, run code before and after, and
|
// allowing you to adjust arguments, run code before and after, and
|
||||||
// conditionally execute the original function.
|
// conditionally execute the original function.
|
||||||
_.wrap = function(func, wrapper) {
|
_.wrap = function(func, wrapper) {
|
||||||
return function() {
|
return function() {
|
||||||
var args = [func].concat(slice.call(arguments));
|
var args = [func].concat(slice.call(arguments));
|
||||||
return wrapper.apply(wrapper, args);
|
return wrapper.apply(this, args);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -435,20 +524,28 @@
|
||||||
var funcs = slice.call(arguments);
|
var funcs = slice.call(arguments);
|
||||||
return function() {
|
return function() {
|
||||||
var args = slice.call(arguments);
|
var args = slice.call(arguments);
|
||||||
for (var i=funcs.length-1; i >= 0; i--) {
|
for (var i = funcs.length - 1; i >= 0; i--) {
|
||||||
args = [funcs[i].apply(this, args)];
|
args = [funcs[i].apply(this, args)];
|
||||||
}
|
}
|
||||||
return args[0];
|
return args[0];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns a function that will only be executed after being called N times.
|
||||||
|
_.after = function(times, func) {
|
||||||
|
return function() {
|
||||||
|
if (--times < 1) { return func.apply(this, arguments); }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// Object Functions
|
// Object Functions
|
||||||
// ----------------
|
// ----------------
|
||||||
|
|
||||||
// Retrieve the names of an object's properties.
|
// Retrieve the names of an object's properties.
|
||||||
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
||||||
_.keys = nativeKeys || function(obj) {
|
_.keys = nativeKeys || function(obj) {
|
||||||
if (_.isArray(obj)) return _.range(0, obj.length);
|
if (obj !== Object(obj)) throw new TypeError('Invalid object');
|
||||||
var keys = [];
|
var keys = [];
|
||||||
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
|
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
|
||||||
return keys;
|
return keys;
|
||||||
|
@ -462,13 +559,29 @@
|
||||||
// Return a sorted list of the function names available on the object.
|
// Return a sorted list of the function names available on the object.
|
||||||
// Aliased as `methods`
|
// Aliased as `methods`
|
||||||
_.functions = _.methods = function(obj) {
|
_.functions = _.methods = function(obj) {
|
||||||
return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
var names = [];
|
||||||
|
for (var key in obj) {
|
||||||
|
if (_.isFunction(obj[key])) names.push(key);
|
||||||
|
}
|
||||||
|
return names.sort();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extend a given object with all the properties in passed-in object(s).
|
// Extend a given object with all the properties in passed-in object(s).
|
||||||
_.extend = function(obj) {
|
_.extend = function(obj) {
|
||||||
each(slice.call(arguments, 1), function(source) {
|
each(slice.call(arguments, 1), function(source) {
|
||||||
for (var prop in source) obj[prop] = source[prop];
|
for (var prop in source) {
|
||||||
|
if (source[prop] !== void 0) obj[prop] = source[prop];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fill in a given object with default properties.
|
||||||
|
_.defaults = function(obj) {
|
||||||
|
each(slice.call(arguments, 1), function(source) {
|
||||||
|
for (var prop in source) {
|
||||||
|
if (obj[prop] == null) obj[prop] = source[prop];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
@ -497,8 +610,12 @@
|
||||||
if (a == b) return true;
|
if (a == b) return true;
|
||||||
// One is falsy and the other truthy.
|
// One is falsy and the other truthy.
|
||||||
if ((!a && b) || (a && !b)) return false;
|
if ((!a && b) || (a && !b)) return false;
|
||||||
|
// Unwrap any wrapped objects.
|
||||||
|
if (a._chain) a = a._wrapped;
|
||||||
|
if (b._chain) b = b._wrapped;
|
||||||
// One of them implements an isEqual()?
|
// One of them implements an isEqual()?
|
||||||
if (a.isEqual) return a.isEqual(b);
|
if (a.isEqual) return a.isEqual(b);
|
||||||
|
if (b.isEqual) return b.isEqual(a);
|
||||||
// Check dates' integer values.
|
// Check dates' integer values.
|
||||||
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
||||||
// Both are NaN?
|
// Both are NaN?
|
||||||
|
@ -537,12 +654,17 @@
|
||||||
// Is a given value an array?
|
// Is a given value an array?
|
||||||
// Delegates to ECMA5's native Array.isArray
|
// Delegates to ECMA5's native Array.isArray
|
||||||
_.isArray = nativeIsArray || function(obj) {
|
_.isArray = nativeIsArray || function(obj) {
|
||||||
return !!(obj && obj.concat && obj.unshift && !obj.callee);
|
return toString.call(obj) === '[object Array]';
|
||||||
|
};
|
||||||
|
|
||||||
|
// Is a given variable an object?
|
||||||
|
_.isObject = function(obj) {
|
||||||
|
return obj === Object(obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Is a given variable an arguments object?
|
// Is a given variable an arguments object?
|
||||||
_.isArguments = function(obj) {
|
_.isArguments = function(obj) {
|
||||||
return !!(obj && obj.callee);
|
return !!(obj && hasOwnProperty.call(obj, 'callee'));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Is a given value a function?
|
// Is a given value a function?
|
||||||
|
@ -557,7 +679,13 @@
|
||||||
|
|
||||||
// Is a given value a number?
|
// Is a given value a number?
|
||||||
_.isNumber = function(obj) {
|
_.isNumber = function(obj) {
|
||||||
return (obj === +obj) || (toString.call(obj) === '[object Number]');
|
return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Is the given value `NaN`? `NaN` happens to be the only value in JavaScript
|
||||||
|
// that does not equal itself.
|
||||||
|
_.isNaN = function(obj) {
|
||||||
|
return obj !== obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Is a given value a boolean?
|
// Is a given value a boolean?
|
||||||
|
@ -575,12 +703,6 @@
|
||||||
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
|
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Is the given value NaN -- this one is interesting. NaN != NaN, and
|
|
||||||
// isNaN(undefined) == true, so we make sure it's a number first.
|
|
||||||
_.isNaN = function(obj) {
|
|
||||||
return _.isNumber(obj) && isNaN(obj);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Is a given value equal to null?
|
// Is a given value equal to null?
|
||||||
_.isNull = function(obj) {
|
_.isNull = function(obj) {
|
||||||
return obj === null;
|
return obj === null;
|
||||||
|
@ -588,7 +710,7 @@
|
||||||
|
|
||||||
// Is a given variable undefined?
|
// Is a given variable undefined?
|
||||||
_.isUndefined = function(obj) {
|
_.isUndefined = function(obj) {
|
||||||
return typeof obj == 'undefined';
|
return obj === void 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Utility Functions
|
// Utility Functions
|
||||||
|
@ -611,11 +733,6 @@
|
||||||
for (var i = 0; i < n; i++) iterator.call(context, i);
|
for (var i = 0; i < n; i++) iterator.call(context, i);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Break out of the middle of an iteration.
|
|
||||||
_.breakLoop = function() {
|
|
||||||
throw breaker;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add your own custom functions to the Underscore object, ensuring that
|
// Add your own custom functions to the Underscore object, ensuring that
|
||||||
// they're correctly added to the OOP wrapper as well.
|
// they're correctly added to the OOP wrapper as well.
|
||||||
_.mixin = function(obj) {
|
_.mixin = function(obj) {
|
||||||
|
|
Loading…
Reference in New Issue