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