From 88571e7d5c7e4471b03bb39ebd2c49cd316e9955 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 2 Jul 2012 08:51:12 +0200 Subject: [PATCH] Updated to spine 1.0.8. --- app/assets/javascripts/app/lib/spine/ajax.js | 60 +++- app/assets/javascripts/app/lib/spine/list.js | 22 +- app/assets/javascripts/app/lib/spine/local.js | 4 +- .../javascripts/app/lib/spine/manager.js | 40 +-- .../javascripts/app/lib/spine/relation.js | 93 ++++-- app/assets/javascripts/app/lib/spine/route.js | 63 ++-- app/assets/javascripts/app/lib/spine/spine.js | 287 ++++++++++++------ 7 files changed, 384 insertions(+), 185 deletions(-) diff --git a/app/assets/javascripts/app/lib/spine/ajax.js b/app/assets/javascripts/app/lib/spine/ajax.js index 4f31dfa55..ab511711f 100755 --- a/app/assets/javascripts/app/lib/spine/ajax.js +++ b/app/assets/javascripts/app/lib/spine/ajax.js @@ -1,11 +1,12 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var $, Ajax, Base, Collection, Extend, Include, Model, Singleton, + var $, Ajax, Base, Collection, Extend, Include, Model, Singleton, Spine, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, - __slice = Array.prototype.slice; + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __slice = [].slice; - if (typeof Spine === "undefined" || Spine === null) Spine = require('spine'); + Spine = this.Spine || require('spine'); $ = Spine.$; @@ -21,8 +22,13 @@ disable: function(callback) { if (this.enabled) { this.enabled = false; - callback(); - return this.enabled = true; + try { + return callback(); + } catch (e) { + throw e; + } finally { + this.enabled = true; + } } else { return callback(); } @@ -43,7 +49,9 @@ }); }, queue: function(callback) { - if (!this.enabled) return; + if (!this.enabled) { + return; + } if (this.pending) { this.requests.push(callback); } else { @@ -86,7 +94,9 @@ function Collection(model) { this.model = model; this.errorResponse = __bind(this.errorResponse, this); + this.recordsResponse = __bind(this.recordsResponse, this); + } Collection.prototype.find = function(id, params) { @@ -110,8 +120,12 @@ Collection.prototype.fetch = function(params, options) { var id, _this = this; - if (params == null) params = {}; - if (options == null) options = {}; + if (params == null) { + params = {}; + } + if (options == null) { + options = {}; + } if (id = params.id) { delete params.id; return this.find(id, params).success(function(record) { @@ -143,7 +157,9 @@ function Singleton(record) { this.record = record; this.errorResponse = __bind(this.errorResponse, this); + this.recordResponse = __bind(this.recordResponse, this); + this.model = this.record.constructor; } @@ -191,7 +207,9 @@ Singleton.prototype.recordResponse = function(options) { var _this = this; - if (options == null) options = {}; + if (options == null) { + options = {}; + } return function(data, status, xhr) { var _ref; if (Spine.isBlank(data)) { @@ -214,7 +232,9 @@ Singleton.prototype.errorResponse = function(options) { var _this = this; - if (options == null) options = {}; + if (options == null) { + options = {}; + } return function(xhr, statusText, error) { var _ref; _this.record.trigger('ajaxError', xhr, statusText, error); @@ -236,7 +256,9 @@ var args, url; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; url = Ajax.getURL(this.constructor); - if (url.charAt(url.length - 1) !== '/') url += '/'; + if (url.charAt(url.length - 1) !== '/') { + url += '/'; + } url += encodeURIComponent(this.id); args.unshift(url); return args.join('/'); @@ -268,8 +290,12 @@ return (_ref = this.ajax()).fetch.apply(_ref, arguments); }, ajaxChange: function(record, type, options) { - if (options == null) options = {}; - if (options.ajax === false) return; + if (options == null) { + options = {}; + } + if (options.ajax === false) { + return; + } return record.ajax()[type](options.ajax, options); } }; @@ -285,6 +311,8 @@ Spine.Ajax = Ajax; - if (typeof module !== "undefined" && module !== null) module.exports = Ajax; + if (typeof module !== "undefined" && module !== null) { + module.exports = Ajax; + } }).call(this); diff --git a/app/assets/javascripts/app/lib/spine/list.js b/app/assets/javascripts/app/lib/spine/list.js index 5a4dc1b3c..1ca549503 100755 --- a/app/assets/javascripts/app/lib/spine/list.js +++ b/app/assets/javascripts/app/lib/spine/list.js @@ -1,10 +1,11 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var $, + var $, Spine, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; - if (typeof Spine === "undefined" || Spine === null) Spine = require('spine'); + Spine = this.Spine || require('spine'); $ = Spine.$; @@ -19,12 +20,13 @@ List.prototype.selectFirst = false; function List() { - this.change = __bind(this.change, this); List.__super__.constructor.apply(this, arguments); + this.change = __bind(this.change, this); + List.__super__.constructor.apply(this, arguments); this.bind('change', this.change); } List.prototype.template = function() { - return arguments[0]; + throw 'Override template'; }; List.prototype.change = function(item) { @@ -34,11 +36,13 @@ return; } this.children().removeClass('active'); - return this.children().forItem(this.current).addClass('active'); + return $(this.children().get(this.items.indexOf(this.current))).addClass('active'); }; List.prototype.render = function(items) { - if (items) this.items = items; + if (items) { + this.items = items; + } this.html(this.template(this.items)); this.change(this.current); if (this.selectFirst) { @@ -54,7 +58,7 @@ List.prototype.click = function(e) { var item; - item = $(e.currentTarget).item(); + item = this.items[$(e.currentTarget).index()]; this.trigger('change', item); return true; }; diff --git a/app/assets/javascripts/app/lib/spine/local.js b/app/assets/javascripts/app/lib/spine/local.js index 3cb511d66..bfe96e5e1 100755 --- a/app/assets/javascripts/app/lib/spine/local.js +++ b/app/assets/javascripts/app/lib/spine/local.js @@ -1,6 +1,8 @@ +// Generated by CoffeeScript 1.3.3 (function() { + var Spine; - if (typeof Spine === "undefined" || Spine === null) Spine = require('spine'); + Spine = this.Spine || require('spine'); Spine.Model.Local = { extended: function() { diff --git a/app/assets/javascripts/app/lib/spine/manager.js b/app/assets/javascripts/app/lib/spine/manager.js index da41a21da..6928c1323 100755 --- a/app/assets/javascripts/app/lib/spine/manager.js +++ b/app/assets/javascripts/app/lib/spine/manager.js @@ -1,10 +1,11 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var $, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, - __slice = Array.prototype.slice; + var $, Spine, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __slice = [].slice; - if (typeof Spine === "undefined" || Spine === null) Spine = require('spine'); + Spine = this.Spine || require('spine'); $ = Spine.$; @@ -104,15 +105,10 @@ Stack.prototype.className = 'spine stack'; function Stack() { - var key, value, _fn, _ref, _ref2, + var key, value, _fn, _ref, _ref1, _this = this; Stack.__super__.constructor.apply(this, arguments); this.manager = new Spine.Manager; - this.manager.bind('change', function() { - var args, controller; - controller = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; - if (controller) return _this.active.apply(_this, args); - }); _ref = this.controllers; for (key in _ref) { value = _ref[key]; @@ -121,21 +117,25 @@ }); this.add(this[key]); } - _ref2 = this.routes; + _ref1 = this.routes; _fn = function(key, value) { var callback; - if (typeof value === 'function') callback = value; + if (typeof value === 'function') { + callback = value; + } callback || (callback = function() { - var _ref3; - return (_ref3 = _this[value]).active.apply(_ref3, arguments); + var _ref2; + return (_ref2 = _this[value]).active.apply(_ref2, arguments); }); return _this.route(key, callback); }; - for (key in _ref2) { - value = _ref2[key]; + for (key in _ref1) { + value = _ref1[key]; _fn(key, value); } - if (this["default"]) this[this["default"]].active(); + if (this["default"]) { + this[this["default"]].active(); + } } Stack.prototype.add = function(controller) { @@ -151,4 +151,8 @@ module.exports = Spine.Manager; } + if (typeof module !== "undefined" && module !== null) { + module.exports.Stack = Spine.Stack; + } + }).call(this); diff --git a/app/assets/javascripts/app/lib/spine/relation.js b/app/assets/javascripts/app/lib/spine/relation.js index 737cf4c66..ca4445af0 100755 --- a/app/assets/javascripts/app/lib/spine/relation.js +++ b/app/assets/javascripts/app/lib/spine/relation.js @@ -1,17 +1,16 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var Collection, Instance, Singleton, isArray, singularize, underscore, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + var Collection, Instance, Singleton, Spine, isArray, require, singularize, underscore, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; - if (typeof Spine === "undefined" || Spine === null) Spine = require('spine'); + Spine = this.Spine || require('spine'); isArray = Spine.isArray; - if (typeof require === "undefined" || require === null) { - require = (function(value) { - return eval(value); - }); - } + require = this.require || (function(value) { + return eval(value); + }); Collection = (function(_super) { @@ -19,7 +18,9 @@ function Collection(options) { var key, value; - if (options == null) options = {}; + if (options == null) { + options = {}; + } for (key in options) { value = options[key]; this[key] = value; @@ -49,14 +50,16 @@ records = this.select(function(rec) { return rec.id + '' === id + ''; }); - if (!records[0]) throw 'Unknown record'; + if (!records[0]) { + throw 'Unknown record'; + } return records[0]; }; Collection.prototype.findAllByAttribute = function(name, value) { var _this = this; return this.model.select(function(rec) { - return rec[name] === value; + return _this.associated(rec) && rec[name] === value; }); }; @@ -72,21 +75,23 @@ }; Collection.prototype.refresh = function(values) { - var record, records, _i, _j, _len, _len2, _ref; + var record, records, _i, _j, _len, _len1, _ref; _ref = this.all(); for (_i = 0, _len = _ref.length; _i < _len; _i++) { record = _ref[_i]; delete this.model.records[record.id]; } records = this.model.fromJSON(values); - if (!isArray(records)) records = [records]; - for (_j = 0, _len2 = records.length; _j < _len2; _j++) { + if (!isArray(records)) { + records = [records]; + } + for (_j = 0, _len1 = records.length; _j < _len1; _j++) { record = records[_j]; record.newRecord = false; record[this.fkey] = this.record.id; this.model.records[record.id] = record; } - return this.model.trigger('refresh', records); + return this.model.trigger('refresh', this.model.cloneArray(records)); }; Collection.prototype.create = function(record) { @@ -108,7 +113,9 @@ function Instance(options) { var key, value; - if (options == null) options = {}; + if (options == null) { + options = {}; + } for (key in options) { value = options[key]; this[key] = value; @@ -120,8 +127,12 @@ }; Instance.prototype.update = function(value) { - if (!(value instanceof this.model)) value = new this.model(value); - if (value.isNew()) value.save(); + if (!(value instanceof this.model)) { + value = new this.model(value); + } + if (value.isNew()) { + value.save(); + } return this.record[this.fkey] = value && value.id; }; @@ -135,7 +146,9 @@ function Singleton(options) { var key, value; - if (options == null) options = {}; + if (options == null) { + options = {}; + } for (key in options) { value = options[key]; this[key] = value; @@ -147,7 +160,9 @@ }; Singleton.prototype.update = function(value) { - if (!(value instanceof this.model)) value = this.model.fromJSON(value); + if (!(value instanceof this.model)) { + value = this.model.fromJSON(value); + } value[this.fkey] = this.record.id; return value.save(); }; @@ -167,9 +182,13 @@ Spine.Model.extend({ hasMany: function(name, model, fkey) { var association; - if (fkey == null) fkey = "" + (underscore(this.className)) + "_id"; + if (fkey == null) { + fkey = "" + (underscore(this.className)) + "_id"; + } association = function(record) { - if (typeof model === 'string') model = require(model); + if (typeof model === 'string') { + model = require(model); + } return new Collection({ name: name, model: model, @@ -178,15 +197,21 @@ }); }; return this.prototype[name] = function(value) { - if (value != null) association(this).refresh(value); + if (value != null) { + association(this).refresh(value); + } return association(this); }; }, belongsTo: function(name, model, fkey) { var association; - if (fkey == null) fkey = "" + (singularize(name)) + "_id"; + if (fkey == null) { + fkey = "" + (singularize(name)) + "_id"; + } association = function(record) { - if (typeof model === 'string') model = require(model); + if (typeof model === 'string') { + model = require(model); + } return new Instance({ name: name, model: model, @@ -195,16 +220,22 @@ }); }; this.prototype[name] = function(value) { - if (value != null) association(this).update(value); + if (value != null) { + association(this).update(value); + } return association(this).exists(); }; return this.attributes.push(fkey); }, hasOne: function(name, model, fkey) { var association; - if (fkey == null) fkey = "" + (underscore(this.className)) + "_id"; + if (fkey == null) { + fkey = "" + (underscore(this.className)) + "_id"; + } association = function(record) { - if (typeof model === 'string') model = require(model); + if (typeof model === 'string') { + model = require(model); + } return new Singleton({ name: name, model: model, @@ -213,7 +244,9 @@ }); }; return this.prototype[name] = function(value) { - if (value != null) association(this).update(value); + if (value != null) { + association(this).update(value); + } return association(this).find(); }; } diff --git a/app/assets/javascripts/app/lib/spine/route.js b/app/assets/javascripts/app/lib/spine/route.js index 6e22eed90..5c5a00acb 100755 --- a/app/assets/javascripts/app/lib/spine/route.js +++ b/app/assets/javascripts/app/lib/spine/route.js @@ -1,10 +1,11 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var $, escapeRegExp, hashStrip, namedParam, splatParam, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, - __slice = Array.prototype.slice; + var $, Spine, escapeRegExp, hashStrip, namedParam, splatParam, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __slice = [].slice; - if (typeof Spine === "undefined" || Spine === null) Spine = require('spine'); + Spine = this.Spine || require('spine'); $ = Spine.$; @@ -48,12 +49,16 @@ }; Route.setup = function(options) { - if (options == null) options = {}; + if (options == null) { + options = {}; + } this.options = $.extend({}, this.options, options); if (this.options.history) { this.history = this.historySupport && this.options.history; } - if (this.options.shim) return; + if (this.options.shim) { + return; + } if (this.history) { $(window).bind('popstate', this.change); } else { @@ -82,11 +87,17 @@ } options = $.extend({}, this.options, options); path = args.join('/'); - if (this.path === path) return; + if (this.path === path) { + return; + } this.path = path; this.trigger('navigate', this.path); - if (options.trigger) this.matchRoute(this.path, options); - if (options.shim) return; + if (options.trigger) { + this.matchRoute(this.path, options); + } + if (options.shim) { + return; + } if (this.history) { return history.pushState({}, document.title, this.path); } else { @@ -97,7 +108,9 @@ Route.getPath = function() { var path; path = window.location.pathname; - if (path.substr(0, 1) !== '/') path = '/' + path; + if (path.substr(0, 1) !== '/') { + path = '/' + path; + } return path; }; @@ -116,16 +129,18 @@ Route.change = function() { var path; path = this.getFragment() !== '' ? this.getFragment() : this.getPath(); - if (path === this.path) return; + if (path === this.path) { + return; + } this.path = path; return this.matchRoute(this.path); }; Route.matchRoute = function(path, options) { - var route, _i, _len, _ref2; - _ref2 = this.routes; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - route = _ref2[_i]; + var route, _i, _len, _ref1; + _ref1 = this.routes; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + route = _ref1[_i]; if (route.match(path, options)) { this.trigger('change', route, path); return route; @@ -143,6 +158,10 @@ while ((match = namedParam.exec(path)) !== null) { this.names.push(match[1]); } + splatParam.lastIndex = 0; + while ((match = splatParam.exec(path)) !== null) { + this.names.push(match[1]); + } path = path.replace(escapeRegExp, '\\$&').replace(namedParam, '([^\/]*)').replace(splatParam, '(.*?)'); this.route = new RegExp('^' + path + '$'); } else { @@ -151,14 +170,18 @@ } Route.prototype.match = function(path, options) { - var i, match, param, params, _len; - if (options == null) options = {}; + var i, match, param, params, _i, _len; + if (options == null) { + options = {}; + } match = this.route.exec(path); - if (!match) return false; + if (!match) { + return false; + } options.match = match; params = match.slice(1); if (this.names.length) { - for (i = 0, _len = params.length; i < _len; i++) { + for (i = _i = 0, _len = params.length; _i < _len; i = ++_i) { param = params[i]; options[this.names[i]] = param; } diff --git a/app/assets/javascripts/app/lib/spine/spine.js b/app/assets/javascripts/app/lib/spine/spine.js index 699512599..867356c81 100755 --- a/app/assets/javascripts/app/lib/spine/spine.js +++ b/app/assets/javascripts/app/lib/spine/spine.js @@ -1,9 +1,10 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var $, Controller, Events, Log, Model, Module, Spine, isArray, isBlank, makeArray, moduleKeywords, - __slice = Array.prototype.slice, - __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, + var $, Controller, Events, Log, Model, Module, Spine, createObject, isArray, isBlank, makeArray, moduleKeywords, + __slice = [].slice, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; Events = { @@ -29,28 +30,36 @@ args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; ev = args.shift(); list = this.hasOwnProperty('_callbacks') && ((_ref = this._callbacks) != null ? _ref[ev] : void 0); - if (!list) return; + if (!list) { + return; + } for (_i = 0, _len = list.length; _i < _len; _i++) { callback = list[_i]; - if (callback.apply(this, args) === false) break; + if (callback.apply(this, args) === false) { + break; + } } return true; }, unbind: function(ev, callback) { - var cb, i, list, _len, _ref; + var cb, i, list, _i, _len, _ref; if (!ev) { this._callbacks = {}; return this; } list = (_ref = this._callbacks) != null ? _ref[ev] : void 0; - if (!list) return this; + if (!list) { + return this; + } if (!callback) { delete this._callbacks[ev]; return this; } - for (i = 0, _len = list.length; i < _len; i++) { + for (i = _i = 0, _len = list.length; _i < _len; i = ++_i) { cb = list[i]; - if (!(cb === callback)) continue; + if (!(cb === callback)) { + continue; + } list = list.slice(); list.splice(i, 1); this._callbacks[ev] = list; @@ -66,10 +75,16 @@ log: function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - if (!this.trace) return; - if (this.logPrefix) args.unshift(this.logPrefix); + if (!this.trace) { + return; + } + if (this.logPrefix) { + args.unshift(this.logPrefix); + } if (typeof console !== "undefined" && console !== null) { - if (typeof console.log === "function") console.log.apply(console, args); + if (typeof console.log === "function") { + console.log.apply(console, args); + } } return this; } @@ -81,23 +96,35 @@ Module.include = function(obj) { var key, value, _ref; - if (!obj) throw 'include(obj) requires obj'; + if (!obj) { + throw new Error('include(obj) requires obj'); + } for (key in obj) { value = obj[key]; - if (__indexOf.call(moduleKeywords, key) < 0) this.prototype[key] = value; + if (__indexOf.call(moduleKeywords, key) < 0) { + this.prototype[key] = value; + } + } + if ((_ref = obj.included) != null) { + _ref.apply(this); } - if ((_ref = obj.included) != null) _ref.apply(this); return this; }; Module.extend = function(obj) { var key, value, _ref; - if (!obj) throw 'extend(obj) requires obj'; + if (!obj) { + throw new Error('extend(obj) requires obj'); + } for (key in obj) { value = obj[key]; - if (__indexOf.call(moduleKeywords, key) < 0) this[key] = value; + if (__indexOf.call(moduleKeywords, key) < 0) { + this[key] = value; + } + } + if ((_ref = obj.extended) != null) { + _ref.apply(this); } - if ((_ref = obj.extended) != null) _ref.apply(this); return this; }; @@ -116,7 +143,9 @@ }; function Module() { - if (typeof this.init === "function") this.init.apply(this, arguments); + if (typeof this.init === "function") { + this.init.apply(this, arguments); + } } return Module; @@ -141,7 +170,9 @@ this.className = name; this.records = {}; this.crecords = {}; - if (attributes.length) this.attributes = attributes; + if (attributes.length) { + this.attributes = attributes; + } this.attributes && (this.attributes = makeArray(this.attributes)); this.attributes || (this.attributes = []); this.unbind(); @@ -155,15 +186,21 @@ Model.find = function(id) { var record; record = this.records[id]; - if (!record && ("" + id).match(/c-\d+/)) return this.findCID(id); - if (!record) throw 'Unknown record'; + if (!record && ("" + id).match(/c-\d+/)) { + return this.findCID(id); + } + if (!record) { + throw new Error('Unknown record'); + } return record.clone(); }; Model.findCID = function(cid) { var record; record = this.crecords[cid]; - if (!record) throw 'Unknown record'; + if (!record) { + throw new Error('Unknown record'); + } return record.clone(); }; @@ -177,20 +214,24 @@ Model.refresh = function(values, options) { var record, records, _i, _len; - if (options == null) options = {}; + if (options == null) { + options = {}; + } if (options.clear) { this.records = {}; this.crecords = {}; } records = this.fromJSON(values); - if (!isArray(records)) records = [records]; + if (!isArray(records)) { + records = [records]; + } for (_i = 0, _len = records.length; _i < _len; _i++) { record = records[_i]; record.id || (record.id = record.cid); this.records[record.id] = record; this.crecords[record.cid] = record; } - this.trigger('refresh', !options.clear && this.cloneArray(records)); + this.trigger('refresh', this.cloneArray(records)); return this; }; @@ -202,7 +243,9 @@ _results = []; for (id in _ref) { record = _ref[id]; - if (callback(record)) _results.push(record); + if (callback(record)) { + _results.push(record); + } } return _results; }).call(this); @@ -214,7 +257,9 @@ _ref = this.records; for (id in _ref) { record = _ref[id]; - if (record[name] === value) return record.clone(); + if (record[name] === value) { + return record.clone(); + } } return null; }; @@ -315,8 +360,12 @@ Model.fromJSON = function(objects) { var value, _i, _len, _results; - if (!objects) return; - if (typeof objects === 'string') objects = JSON.parse(objects); + if (!objects) { + return; + } + if (typeof objects === 'string') { + objects = JSON.parse(objects); + } if (isArray(objects)) { _results = []; for (_i = 0, _len = objects.length; _i < _len; _i++) { @@ -357,14 +406,24 @@ Model.idCounter = 0; - Model.uid = function() { - return this.idCounter++; + Model.uid = function(prefix) { + var uid; + if (prefix == null) { + prefix = ''; + } + uid = prefix + this.idCounter++; + if (this.exists(uid)) { + uid = this.uid(prefix); + } + return uid; }; function Model(atts) { Model.__super__.constructor.apply(this, arguments); - if (atts) this.load(atts); - this.cid || (this.cid = 'c-' + this.constructor.uid()); + if (atts) { + this.load(atts); + } + this.cid = this.constructor.uid('c-'); } Model.prototype.isNew = function() { @@ -404,17 +463,21 @@ } } } - if (this.id) result.id = this.id; + if (this.id) { + result.id = this.id; + } return result; }; Model.prototype.eql = function(rec) { - return !!(rec && rec.constructor === this.constructor && (rec.id === this.id || rec.cid === this.cid)); + return !!(rec && rec.constructor === this.constructor && (rec.cid === this.cid) || (rec.id && rec.id === this.id)); }; Model.prototype.save = function(options) { var error, record; - if (options == null) options = {}; + if (options == null) { + options = {}; + } if (options.validate !== false) { error = this.validate(); if (error) { @@ -428,9 +491,9 @@ return record; }; - Model.prototype.updateAttribute = function(name, value) { + Model.prototype.updateAttribute = function(name, value, options) { this[name] = value; - return this.save(); + return this.save(options); }; Model.prototype.updateAttributes = function(atts, options) { @@ -448,7 +511,9 @@ }; Model.prototype.destroy = function(options) { - if (options == null) options = {}; + if (options == null) { + options = {}; + } this.trigger('beforeDestroy', options); delete this.constructor.records[this.id]; delete this.constructor.crecords[this.cid]; @@ -471,12 +536,14 @@ }; Model.prototype.clone = function() { - return Object.create(this); + return createObject(this); }; Model.prototype.reload = function() { var original; - if (this.isNew()) return this; + if (this.isNew()) { + return this; + } original = this.constructor.find(this.id); this.load(original.attributes()); return original; @@ -519,7 +586,9 @@ Model.prototype.create = function(options) { var clone, record; this.trigger('beforeCreate', options); - if (!this.id) this.id = this.cid; + if (!this.id) { + this.id = this.cid; + } record = this.dup(false); this.constructor.records[this.id] = record; this.constructor.crecords[this.cid] = record; @@ -533,7 +602,9 @@ var binder, unbinder, _this = this; this.constructor.bind(events, binder = function(record) { - if (record && _this.eql(record)) return callback.apply(_this, arguments); + if (record && _this.eql(record)) { + return callback.apply(_this, arguments); + } }); this.constructor.bind('unbind', unbinder = function(record) { if (record && _this.eql(record)) { @@ -549,7 +620,7 @@ _this = this; return binder = this.bind(events, function() { _this.constructor.unbind(events, binder); - return callback.apply(_this); + return callback.apply(_this, arguments); }); }; @@ -582,6 +653,7 @@ function Controller(options) { this.release = __bind(this.release, this); + var key, value, _ref; this.options = options; _ref = this.options; @@ -589,39 +661,66 @@ value = _ref[key]; this[key] = value; } - if (!this.el) this.el = document.createElement(this.tag); + if (!this.el) { + this.el = document.createElement(this.tag); + } this.el = $(this.el); - if (this.className) this.el.addClass(this.className); - if (this.attributes) this.el.attr(this.attributes); - this.release(function() { - return this.el.remove(); - }); - if (!this.events) this.events = this.constructor.events; - if (!this.elements) this.elements = this.constructor.elements; - if (this.events) this.delegateEvents(); - if (this.elements) this.refreshElements(); + this.$el = this.el; + if (this.className) { + this.el.addClass(this.className); + } + if (this.attributes) { + this.el.attr(this.attributes); + } + if (!this.events) { + this.events = this.constructor.events; + } + if (!this.elements) { + this.elements = this.constructor.elements; + } + if (this.events) { + this.delegateEvents(this.events); + } + if (this.elements) { + this.refreshElements(); + } Controller.__super__.constructor.apply(this, arguments); } - Controller.prototype.release = function(callback) { - if (typeof callback === 'function') { - return this.bind('release', callback); - } else { - return this.trigger('release'); - } + Controller.prototype.release = function() { + this.trigger('release'); + this.el.remove(); + return this.unbind(); }; Controller.prototype.$ = function(selector) { return $(selector, this.el); }; - Controller.prototype.delegateEvents = function() { - var eventName, key, match, method, selector, _ref, _results; - _ref = this.events; + Controller.prototype.delegateEvents = function(events) { + var eventName, key, match, method, selector, _results, + _this = this; _results = []; - for (key in _ref) { - method = _ref[key]; - if (typeof method !== 'function') method = this.proxy(this[method]); + for (key in events) { + method = events[key]; + if (typeof method === 'function') { + method = (function(method) { + return function() { + method.apply(_this, arguments); + return true; + }; + })(method); + } else { + if (!this[method]) { + throw new Error("" + method + " doesn't exist"); + } + method = (function(method) { + return function() { + _this[method].apply(_this, arguments); + return true; + }; + })(method); + } match = key.match(this.eventSplitter); eventName = match[1]; selector = match[2]; @@ -699,7 +798,7 @@ var previous, _ref; _ref = [this.el, $(element.el || element)], previous = _ref[0], this.el = _ref[1]; previous.replaceWith(this.el); - this.delegateEvents(); + this.delegateEvents(this.events); this.refreshElements(); return this.el; }; @@ -712,14 +811,12 @@ return element; }; - if (typeof Object.create !== 'function') { - Object.create = function(o) { - var Func; - Func = function() {}; - Func.prototype = o; - return new Func(); - }; - } + createObject = Object.create || function(o) { + var Func; + Func = function() {}; + Func.prototype = o; + return new Func(); + }; isArray = function(value) { return Object.prototype.toString.call(value) === '[object Array]'; @@ -727,7 +824,9 @@ isBlank = function(value) { var key; - if (!value) return true; + if (!value) { + return true; + } for (key in value) { return false; } @@ -740,9 +839,11 @@ Spine = this.Spine = {}; - if (typeof module !== "undefined" && module !== null) module.exports = Spine; + if (typeof module !== "undefined" && module !== null) { + module.exports = Spine; + } - Spine.version = '1.0.6'; + Spine.version = '1.0.8'; Spine.isArray = isArray; @@ -769,27 +870,35 @@ __extends(result, _super); function result() { - result.__super__.constructor.apply(this, arguments); + return result.__super__.constructor.apply(this, arguments); } return result; })(this); - if (instances) result.include(instances); - if (statics) result.extend(statics); - if (typeof result.unbind === "function") result.unbind(); + if (instances) { + result.include(instances); + } + if (statics) { + result.extend(statics); + } + if (typeof result.unbind === "function") { + result.unbind(); + } return result; }; Model.setup = function(name, attributes) { var Instance; - if (attributes == null) attributes = []; + if (attributes == null) { + attributes = []; + } Instance = (function(_super) { __extends(Instance, _super); function Instance() { - Instance.__super__.constructor.apply(this, arguments); + return Instance.__super__.constructor.apply(this, arguments); } return Instance; @@ -799,10 +908,6 @@ return Instance; }; - Module.init = Controller.init = Model.init = function(a1, a2, a3, a4, a5) { - return new this(a1, a2, a3, a4, a5); - }; - Spine.Class = Module; }).call(this);