UNPKG

3vot-model

Version:

3VOT Model based on SpineJS

1,732 lines (1,511 loc) 79.9 kB
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ (function() { var Action, Ajax, AjaxUtils, Collection, Extend, Include, Query, SalesforceRest, Singleton, View, ajax_request, _3Model, __slice = [].slice; _3Model = require('3vot-model'); AjaxUtils = require("./ajax_utils"); Collection = require("./ajax_collection"); Singleton = require("./ajax_singleton"); Action = require("./ajax_action"); Query = require("./ajax_query"); SalesforceRest = require("./ajax_salesforcerest"); View = require("./ajax_view"); ajax_request = require("./ajax_request"); Include = { ajax: function() { return new Singleton(this); }, url: function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; args.unshift(encodeURIComponent(this.id)); return AjaxUtils.generateURL.apply(AjaxUtils, [this].concat(__slice.call(args))); } }; Extend = { ajax: function() { return new Collection(this); }, view: function() { return new View(this); }, salesforceRest: function() { return new SalesforceRest(this); }, action: function() { return new Action(this); }, queryManager: function() { return new Query(this); }, url: function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return AjaxUtils.generateURL.apply(AjaxUtils, [this].concat(__slice.call(args))); } }; Ajax = { extended: function() { this.fetch(this.ajaxFetch); this.change(this.ajaxChange); this.query = this.ajaxQuery; this.extend(Extend); return this.include(Include); }, ajaxFetch: function() { var _ref; return (_ref = this.ajax()).fetch.apply(_ref, arguments); }, callAction: function() { var _ref; return (_ref = this.action()).call.apply(_ref, arguments); }, salesforceRest: function() { var _ref; return (_ref = this.salesforceRest).call.apply(_ref, arguments); }, callView: function() { var _ref; return (_ref = this.view()).call.apply(_ref, arguments); }, ajaxQuery: function() { var _ref; return (_ref = this.queryManager()).manualQuery.apply(_ref, arguments); }, destroy: function() { var _ref; return (_ref = this.action()).destroy.apply(_ref, arguments); }, ajaxChange: function(record, type, options) { if (options == null) { options = {}; } if (options.ajax === false || record.constructor.autoAjax === false) { return; } return record.ajax()[type](options.ajax || {}, options); } }; Ajax.request = ajax_request; module.exports = Ajax; }).call(this); },{"./ajax_action":2,"./ajax_collection":3,"./ajax_query":4,"./ajax_request":5,"./ajax_salesforcerest":6,"./ajax_singleton":7,"./ajax_utils":8,"./ajax_view":9,"3vot-model":"bRhrlU"}],2:[function(require,module,exports){ (function() { var Action, ajax_request, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ajax_request = require("./ajax_request"); Action = (function() { function Action(model) { this.model = model; this.failResponse = __bind(this.failResponse, this); this.recordsResponse = __bind(this.recordsResponse, this); } Action.prototype.destroy = function(values, options) { var params; if (values == null) { values = {}; } if (options == null) { options = {}; } options.url = this.model.url(); params = { query: values }; ajax_request.queueRequest.del(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordsResponse(res.body, options); }; })(this)); return true; }; Action.prototype.call = function(name, values, options) { var params; if (values == null) { values = {}; } if (options == null) { options = {}; } options.url = this.model.url() + "/actions/" + name; params = { data: values }; ajax_request.queueRequest.post(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordsResponse(res.body, options); }; })(this)); return true; }; Action.prototype.recordsResponse = function(data, options) { var _ref; this.model.trigger('ajaxSuccess', data); this.model.trigger('actionSuccess', data); return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0; }; Action.prototype.failResponse = function(error, options) { var _ref; this.model.trigger('ajaxError', error); this.model.trigger('actionError', error); return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0; }; return Action; })(); module.exports = Action; }).call(this); },{"./ajax_request":5}],3:[function(require,module,exports){ (function() { var AjaxUtils, Collection, ajax_request, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ajax_request = require("./ajax_request"); AjaxUtils = require("./ajax_utils"); Collection = (function() { function Collection(model) { this.model = model; this.failResponse = __bind(this.failResponse, this); this.recordsResponse = __bind(this.recordsResponse, this); if (typeof Visualforce !== "undefined") { ajax_request = require("./vf_request"); } } Collection.prototype.find = function(id, params, options) { var record; if (options == null) { options = {}; } record = new this.model({ id: id }); options.url = options.url || AjaxUtils.getURL(record); options.model = this.model; params.query = params.query || this.model.attributes.join(","); return ajax_request.queueRequest.get(params, options); }; Collection.prototype.all = function(params, options) { if (options == null) { options = {}; } if (!options.url) { options.url = this.model.url(); } return ajax_request.queueRequest.get(params, options); }; Collection.prototype.fetch = function(params, options) { var id, _this; if (params == null) { params = {}; } if (options == null) { options = {}; } if (id = params.id) { delete params.id; _this = this; this.find(id, params, options).end(function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } _this.model.refresh(res.body, options); return _this.recordsResponse(res, options); }); return true; } else { this.all(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } _this.model.refresh(res.body, options); return _this.recordsResponse(res, options); }; })(this)); return true; } }; Collection.prototype.recordsResponse = function(data, options) { var _ref; this.model.trigger('ajaxSuccess', data); return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0; }; Collection.prototype.failResponse = function(error, options) { var _ref; this.model.trigger('ajaxError', error); return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0; }; return Collection; })(); module.exports = Collection; }).call(this); },{"./ajax_request":5,"./ajax_utils":8,"./vf_request":15}],4:[function(require,module,exports){ (function() { var AjaxUtils, Query, ajax_request, _3Model, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ajax_request = require("./ajax_request"); AjaxUtils = require("./ajax_utils"); _3Model = require("3vot-model"); Query = (function() { function Query(model) { this.model = model; this.failResponse = __bind(this.failResponse, this); this.recordsResponse = __bind(this.recordsResponse, this); if (typeof Visualforce !== "undefined") { ajax_request = require("./vf_request"); } } Query.prototype.manualQuery = function(query, options) { if (options == null) { options = {}; } options.url = _3Model.Model.host + "/query?query=" + query; ajax_request.queueRequest.get({}, options, this.model).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } _this.model.refresh(res.body, options); return _this.recordsResponse(res, options); }; })(this)); return true; }; Query.prototype.recordsResponse = function(data, options) { var _ref; this.model.trigger('ajaxSuccess', data); return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0; }; Query.prototype.failResponse = function(error, options) { var _ref; this.model.trigger('ajaxError', error); return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0; }; return Query; })(); module.exports = Query; ({ getQuery: (function(_this) { return function(options) { if (options == null) { options = { "": true }; } if (!RSpine.isArray(options)) { options = [options]; } return _this.queryString() + _this.getQueryCondition(options); }; })(this), getQueryCondition: function(conditions) { var filter, filterKey, key, orderFilterString, queryFilterString, querySinceLastUpdate, querySinceLastUpdated, stringFilters, thisFilter, _i, _j, _len, _len1, _ref; if (Object.keys(this.filters).length === 0) { return ""; } stringFilters = []; queryFilterString = ""; orderFilterString = ""; querySinceLastUpdated = this.querySinceLastUpdate; for (_i = 0, _len = conditions.length; _i < _len; _i++) { filter = conditions[_i]; if (!filter.junction) { filter.junction = "and"; } if (stringFilters.length === 0) { filter.junction = "where"; } filterKey = ""; _ref = Object.keys(filter); for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { key = _ref[_j]; if (key !== "junction") { filterKey = key; } } if (key === "sinceLastUpdate") { querySinceLastUpdate = true; } if (key === "avoidLastUpdate") { querySinceLastUpdate = false; } if (filterKey !== "orderBy" && filterKey !== "sinceLastUpdate" && key !== "avoidLastUpdate") { thisFilter = this.filters[filterKey]; thisFilter = thisFilter.replace("?", filter[filterKey]); stringFilters.push(thisFilter); queryFilterString += " " + filter.junction + " " + thisFilter; } else if (filterKey === "orderBy") { orderFilterString = " ORDER " + filter[filterKey]; } } if (querySinceLastUpdated) { queryFilterString = " and LastModifiedDate >= " + this.lastUpdate; } return queryFilterString + orderFilterString; }, queryString: (function(_this) { return function() { var attribute, query, _i, _len, _ref, _ref1; query = "select "; _ref = _this.attributes; for (_i = 0, _len = _ref.length; _i < _len; _i++) { attribute = _ref[_i]; if (((_ref1 = _this.avoidQueryList) != null ? _ref1.indexOf(attribute) : void 0) === -1) { query += attribute + ","; } } query += "Id "; query += "from " + _this.className; query += " "; return query; }; })(this) }); }).call(this); },{"./ajax_request":5,"./ajax_utils":8,"./vf_request":15,"3vot-model":"bRhrlU"}],5:[function(require,module,exports){ (function() { var AjaxRequest, AjaxUtils, superagent, _3Model; superagent = require("superagent"); AjaxUtils = require("./ajax_utils"); _3Model = require("3vot-model"); AjaxRequest = (function() { function AjaxRequest() {} AjaxRequest.promise = { end: function() {} }; AjaxRequest.enabled = true; AjaxRequest.disable = function(callback) { var e; if (this.enabled) { this.enabled = false; try { return callback(); } catch (_error) { e = _error; throw e; } finally { this.enabled = true; } } else { return callback(); } }; AjaxRequest.queueRequest = { get: function(params, options) { return AjaxRequest.executeRequest("get", params, options); }, post: function(params, options) { return AjaxRequest.executeRequest("post", params, options); }, put: function(params, options) { return AjaxRequest.executeRequest("put", params, options); }, del: function(params, options) { return AjaxRequest.executeRequest("del", params, options); } }; AjaxRequest.executeRequest = function(type, params, options) { var header, request, _i, _len, _ref; if (this.enabled === false) { return this.promise; } request = superagent[type](options.url).set('X-Requested-With', 'XMLHttpRequest'); _ref = _3Model.Model.headers; for (_i = 0, _len = _ref.length; _i < _len; _i++) { header = _ref[_i]; if (!header.name || !header.value) { throw "header should be an object with name and value"; } request = request.set(header.name, header.value); } if (typeof request.withCredentials === "function") { request.withCredentials(); } if (type === "put" || type === "post") { request = request.type('json'); } if (options.error) { request.on("error", options.error); } if (params.query) { request = request.query(params.query); } if (params.data) { if (typeof params.data !== 'string') { params.data = JSON.stringify(params.data); } request = request.send(params.data); } return request; }; return AjaxRequest; })(); module.exports = AjaxRequest; }).call(this); },{"./ajax_utils":8,"3vot-model":"bRhrlU","superagent":16}],6:[function(require,module,exports){ (function() { var SalesforceRest, ajax_request, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ajax_request = require("./ajax_request"); SalesforceRest = (function() { function SalesforceRest(model) { this.model = model; this.failResponse = __bind(this.failResponse, this); this.recordsResponse = __bind(this.recordsResponse, this); } SalesforceRest.prototype.call = function(url, params, options) { if (options == null) { options = {}; } options.url = url; ajax_request.queueRequest.get(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordsResponse(res.body, options); }; })(this)); return true; }; SalesforceRest.prototype.recordsResponse = function(data, options) { var _ref; this.model.trigger('ajaxSuccess', data); this.model.trigger('restSuccess', data); return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0; }; SalesforceRest.prototype.failResponse = function(error, options) { var _ref; this.model.trigger('ajaxError', error); this.model.trigger('restError', error); return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0; }; return SalesforceRest; })(); module.exports = SalesforceRest; }).call(this); },{"./ajax_request":5}],7:[function(require,module,exports){ (function() { var AjaxUtils, Singleton, ajax_request, _3Model, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; _3Model = require("3vot-model"); ajax_request = require("./ajax_request"); AjaxUtils = require("./ajax_utils"); Singleton = (function() { function Singleton(record) { this.record = record; this.failResponse = __bind(this.failResponse, this); this.recordResponse = __bind(this.recordResponse, this); this.model = this.record.constructor; if (typeof Visualforce !== "undefined") { ajax_request = require("./vf_request"); } } Singleton.prototype.reload = function(params, options) { if (options == null) { options = {}; } params.data = this.record.toJSON(); options.url = options.url || AjaxUtils.getURL(this.record); options.error = this.failResponse; options.record = this.record; return ajax_request.queueRequest.get(params, options).end((function(_this) { return function(res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordResponse(res.body, options); }; })(this)); }; Singleton.prototype.create = function(params, options) { if (options == null) { options = {}; } params.data = this.record.toJSON(); options.url = options.url || AjaxUtils.getCollectionURL(this.record); return ajax_request.queueRequest.post(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordResponse(res.body, options); }; })(this)); }; Singleton.prototype.update = function(params, options) { if (options == null) { options = {}; } params.data = this.record.toJSON(); options.url = options.url || AjaxUtils.getURL(this.record); return ajax_request.queueRequest.put(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordResponse(res.body, options); }; })(this)); }; Singleton.prototype.destroy = function(params, options) { if (options == null) { options = {}; } params.data = this.record.toJSON(); options.url = options.url || AjaxUtils.getURL(this.record); options.error = this.failResponse; return ajax_request.queueRequest.del(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordResponse(res.body, options); }; })(this)); }; Singleton.prototype.recordResponse = function(data, options) { var _ref; if (options == null) { options = {}; } ajax_request.disable((function(_this) { return function() { if (!(_3Model.isBlank(data) || _this.record.destroyed)) { if (data.id && _this.record.id !== data.id) { _this.record.changeID(data.id); } return _this.record.refresh(data); } }; })(this)); this.record.trigger('ajaxSuccess', data); return (_ref = options.done) != null ? _ref.apply(this.record) : void 0; }; Singleton.prototype.failResponse = function(error, options) { var _ref; if (options == null) { options = {}; } this.record.trigger('ajaxError', error); return (_ref = options.fail) != null ? _ref.apply(this.record, [error]) : void 0; }; return Singleton; })(); module.exports = Singleton; }).call(this); },{"./ajax_request":5,"./ajax_utils":8,"./vf_request":15,"3vot-model":"bRhrlU"}],8:[function(require,module,exports){ (function() { var AjaxUtils, _3Model, __slice = [].slice; _3Model = require("3vot-model"); AjaxUtils = (function() { function AjaxUtils() {} AjaxUtils.getURL = function(object) { return (typeof object.url === "function" ? object.url() : void 0) || object.url; }; AjaxUtils.getScope = function(object) { return (typeof object.scope === "function" ? object.scope() : void 0) || object.scope; }; AjaxUtils.generateURL = function() { var args, collection, object, path, scope; object = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if (window && typeof window.Visualforce !== "undefined") { if (object.className) { collection = object.className; scope = AjaxUtils.getScope(object); } else { collection = typeof object.constructor.url === 'string' ? object.constructor.url : object.constructor.className; scope = AjaxUtils.getScope(object) || AjaxUtils.getScope(object.constructor); } } else if (object.className) { collection = object.className.toLowerCase() + 's'; scope = AjaxUtils.getScope(object); } else { if (typeof object.constructor.url === 'string') { collection = object.constructor.url; } else { collection = object.constructor.className.toLowerCase() + 's'; } scope = AjaxUtils.getScope(object) || AjaxUtils.getScope(object.constructor); } args.unshift(collection); args.unshift(scope); path = args.join('/'); path = path.replace(/(\/\/)/g, "/"); path = path.replace(/^\/|\/$/g, ""); if (path.indexOf("../") !== 0) { return _3Model.Model.host + "/" + path; } else { return path; } }; AjaxUtils.getCollectionURL = function(object) { if (object) { if (typeof object.url === "function") { return AjaxUtils.generateURL(object); } else { return object.url; } } }; return AjaxUtils; })(); module.exports = AjaxUtils; }).call(this); },{"3vot-model":"bRhrlU"}],9:[function(require,module,exports){ (function() { var View, ajax_request, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ajax_request = require("./ajax_request"); View = (function() { function View(model) { this.model = model; this.failResponse = __bind(this.failResponse, this); this.recordsResponse = __bind(this.recordsResponse, this); } View.prototype.call = function(name, values, options) { var params; if (options == null) { options = {}; } options.url = this.model.url() + "/views/" + name; params = { query: values }; ajax_request.queueRequest.get(params, options).end((function(_this) { return function(err, res) { if (err) { return _this.failResponse(err, options); } else if (res.status >= 400) { return _this.failResponse(res.text, options); } return _this.recordsResponse(res.body, options); }; })(this)); return true; }; View.prototype.recordsResponse = function(data, options) { var _ref; this.model.trigger('ajaxSuccess', data); this.model.trigger('viewSuccess', data); return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0; }; View.prototype.failResponse = function(error, options) { var _ref; this.model.trigger('ajaxError', error); this.model.trigger('viewError', error); return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0; }; return View; })(); module.exports = View; }).call(this); },{"./ajax_request":5}],10:[function(require,module,exports){ (function() { var Events, trim, __slice = [].slice; trim = function(text) { var rtrim, _ref; rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; if ((_ref = text === null) != null) { _ref; } else { ({ "": (text + "").replace(rtrim, "") }); }; return text; }; Events = { bind: function(ev, callback) { var calls, evs, name, _i, _len; evs = ev.split(' '); if (this.hasOwnProperty('_callbacks') && this._callbacks) { calls = this._callbacks; } else { this._callbacks = {}; calls = this._callbacks; } for (_i = 0, _len = evs.length; _i < _len; _i++) { name = evs[_i]; calls[name] || (calls[name] = []); calls[name].push(callback); } return this; }, one: function(ev, callback) { var handler; return this.bind(ev, handler = function() { this.unbind(ev, handler); return callback.apply(this, arguments); }); }, trigger: function() { var args, callback, ev, list, _i, _len, _ref; 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; } for (_i = 0, _len = list.length; _i < _len; _i++) { callback = list[_i]; if (callback.apply(this, args) === false) { break; } } return true; }, listenTo: function(obj, ev, callback) { obj.bind(ev, callback); this.listeningTo || (this.listeningTo = []); this.listeningTo.push({ obj: obj, ev: ev, callback: callback }); return this; }, listenToOnce: function(obj, ev, callback) { var handler, listeningToOnce; listeningToOnce = this.listeningToOnce || (this.listeningToOnce = []); obj.bind(ev, handler = function() { var i, idx, lt, _i, _len; idx = -1; for (i = _i = 0, _len = listeningToOnce.length; _i < _len; i = ++_i) { lt = listeningToOnce[i]; if (lt.obj === obj) { if (lt.ev === ev && lt.callback === callback) { idx = i; } } } obj.unbind(ev, handler); if (idx !== -1) { listeningToOnce.splice(idx, 1); } return callback.apply(this, arguments); }); listeningToOnce.push({ obj: obj, ev: ev, callback: callback, handler: handler }); return this; }, stopListening: function(obj, events, callback) { var ev, evts, i, idx, listeningTo, lt, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _results; if (arguments.length === 0) { _ref = [this.listeningTo, this.listeningToOnce]; for (_i = 0, _len = _ref.length; _i < _len; _i++) { listeningTo = _ref[_i]; if (!listeningTo) { continue; } for (_j = 0, _len1 = listeningTo.length; _j < _len1; _j++) { lt = listeningTo[_j]; lt.obj.unbind(lt.ev, lt.handler || lt.callback); } } this.listeningTo = void 0; return this.listeningToOnce = void 0; } else if (obj) { _ref1 = [this.listeningTo, this.listeningToOnce]; _results = []; for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) { listeningTo = _ref1[_k]; if (!listeningTo) { continue; } events = events ? events.split(' ') : [void 0]; _results.push((function() { var _l, _len3, _results1; _results1 = []; for (_l = 0, _len3 = events.length; _l < _len3; _l++) { ev = events[_l]; _results1.push((function() { var _m, _ref2, _results2; _results2 = []; for (idx = _m = _ref2 = listeningTo.length - 1; _ref2 <= 0 ? _m <= 0 : _m >= 0; idx = _ref2 <= 0 ? ++_m : --_m) { lt = listeningTo[idx]; if ((!ev) || (ev === lt.ev)) { lt.obj.unbind(lt.ev, lt.handler || lt.callback); if (idx !== -1) { _results2.push(listeningTo.splice(idx, 1)); } else { _results2.push(void 0); } } else if (ev) { evts = lt.ev.split(' '); if (~(i = evts.indexOf(ev))) { evts.splice(i, 1); lt.ev = trim(evts.join(' ')); _results2.push(lt.obj.unbind(ev, lt.handler || lt.callback)); } else { _results2.push(void 0); } } else { _results2.push(void 0); } } return _results2; })()); } return _results1; })()); } return _results; } }, unbind: function(ev, callback) { var cb, evs, i, list, name, _i, _j, _len, _len1, _ref; if (arguments.length === 0) { this._callbacks = {}; return this; } if (!ev) { return this; } evs = ev.split(' '); for (_i = 0, _len = evs.length; _i < _len; _i++) { name = evs[_i]; list = (_ref = this._callbacks) != null ? _ref[name] : void 0; if (!list) { continue; } if (!callback) { delete this._callbacks[name]; continue; } for (i = _j = 0, _len1 = list.length; _j < _len1; i = ++_j) { cb = list[i]; if (!(cb === callback)) { continue; } list = list.slice(); list.splice(i, 1); this._callbacks[name] = list; break; } } return this; } }; Events.on = Events.bind; Events.off = Events.unbind; module.exports = Events; }).call(this); },{}],"3vot-model":[function(require,module,exports){ module.exports=require('bRhrlU'); },{}],"bRhrlU":[function(require,module,exports){ (function() { var Events, Model, Module, _3Model; Events = require("./events"); Model = require("./model"); Module = require("./module"); _3Model = this._3Model = {}; _3Model.Events = Events; _3Model.Module = Module; _3Model.Model = Model; _3Model.isBlank = Model.isBlank; Module.extend.call(_3Model, Events); _3Model.Class = Module; module.exports = _3Model; }).call(this); },{"./events":10,"./model":13,"./module":14}],13:[function(require,module,exports){ (function() { var Events, Model, Module, createObject, isArray, isBlank, makeArray, __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; Events = require("./events"); Module = require("./module"); Model = (function(_super) { __extends(Model, _super); Model.extend(Events); Model.records = []; Model.irecords = {}; Model.attributes = []; Model.host = ""; Model.headers = []; Model.configure = function() { var attributes, name; name = arguments[0], attributes = 2 <= arguments.length ? __slice.call(arguments, 1) : []; this.className = name; this.deleteAll(); if (attributes.length) { this.attributes = attributes; } this.attributes && (this.attributes = makeArray(this.attributes)); this.attributes || (this.attributes = []); this.unbind(); return this; }; Model.toString = function() { return "" + this.className + "(" + (this.attributes.join(", ")) + ")"; }; Model.find = function(id) { var record; record = this.exists(id); if (!record) { throw new Error("\"" + this.className + "\" model could not find a record for the ID \"" + id + "\""); } return record; }; Model.exists = function(id) { var _ref; return (_ref = this.irecords[id]) != null ? _ref.clone() : void 0; }; Model.addRecord = function(record) { if (record.id && this.irecords[record.id]) { this.irecords[record.id].remove(); } record.id || (record.id = record.cid); this.records.push(record); this.irecords[record.id] = record; return this.irecords[record.cid] = record; }; Model.refresh = function(values, options) { var record, records, result, _i, _len; if (options == null) { options = {}; } if (options.clear) { this.deleteAll(); } records = this.fromJSON(values); if (!isArray(records)) { records = [records]; } for (_i = 0, _len = records.length; _i < _len; _i++) { record = records[_i]; this.addRecord(record); } this.sort(); result = this.cloneArray(records); this.trigger('refresh', result, options); return result; }; Model.select = function(callback) { var record, _i, _len, _ref, _results; _ref = this.records; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { record = _ref[_i]; if (callback(record)) { _results.push(record.clone()); } } return _results; }; Model.findByAttribute = function(name, value) { var record, _i, _len, _ref; _ref = this.records; for (_i = 0, _len = _ref.length; _i < _len; _i++) { record = _ref[_i]; if (record[name] === value) { return record.clone(); } } return null; }; Model.findAllByAttribute = function(name, value) { return this.select(function(item) { return item[name] === value; }); }; Model.each = function(callback) { var record, _i, _len, _ref, _results; _ref = this.records; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { record = _ref[_i]; _results.push(callback(record.clone())); } return _results; }; Model.all = function() { return this.cloneArray(this.records); }; Model.first = function() { var _ref; return (_ref = this.records[0]) != null ? _ref.clone() : void 0; }; Model.last = function() { var _ref; return (_ref = this.records[this.records.length - 1]) != null ? _ref.clone() : void 0; }; Model.count = function() { return this.records.length; }; Model.deleteAll = function() { this.records = []; return this.irecords = {}; }; Model.destroyAll = function(options) { var record, _i, _len, _ref, _results; _ref = this.records; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { record = _ref[_i]; _results.push(record.destroy(options)); } return _results; }; Model.update = function(id, atts, options) { return this.find(id).updateAttributes(atts, options); }; Model.create = function(atts, options) { var record; record = new this(atts); return record.save(options); }; Model.destroy = function(id, options) { return this.find(id).destroy(options); }; Model.change = function(callbackOrParams) { if (typeof callbackOrParams === 'function') { return this.bind('change', callbackOrParams); } else { return this.trigger.apply(this, ['change'].concat(__slice.call(arguments))); } }; Model.fetch = function(callbackOrParams) { if (typeof callbackOrParams === 'function') { return this.bind('fetch', callbackOrParams); } else { return this.trigger.apply(this, ['fetch'].concat(__slice.call(arguments))); } }; Model.toJSON = function() { return this.records; }; Model.fromJSON = function(objects) { var value, _i, _len, _results; if (!objects) { return; } if (typeof objects === 'string') { objects.replace(/\Id/g, 'id'); objects = JSON.parse(objects); } if (isArray(objects)) { _results = []; for (_i = 0, _len = objects.length; _i < _len; _i++) { value = objects[_i]; if (value.Id) { value.id = value.Id; } _results.push(new this(value)); } return _results; } else { if (objects.Id) { objects.id = objects.Id; } return new this(objects); } }; Model.fromForm = function() { var _ref; return (_ref = new this).fromForm.apply(_ref, arguments); }; Model.escapeSingleQuotes = function(str) { return str = str.replace(/'/g, "\\'"); }; Model.sort = function() { if (this.comparator) { this.records.sort(this.comparator); } return this; }; Model.cloneArray = function(array) { var value, _i, _len, _results; _results = []; for (_i = 0, _len = array.length; _i < _len; _i++) { value = array[_i]; _results.push(value.clone()); } return _results; }; Model.idCounter = 0; 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 = (atts != null ? atts.cid : void 0) || this.constructor.uid('c-'); } Model.prototype.isNew = function() { return !this.exists(); }; Model.prototype.isValid = function() { return !this.validate(); }; Model.prototype.validate = function() {}; Model.prototype.load = function(atts) { var key, value; if (atts.id) { this.id = atts.id; } for (key in atts) { value = atts[key]; if (atts.hasOwnProperty(key) && typeof this[key] === 'function') { this[key](value); } else { this[key] = value; } } return this; }; Model.prototype.get = function(attr) { return this[attr]; }; Model.prototype.set = function(attr, value) { return this[attr] = value; }; Model.prototype.attributes = function() { var key, result, _i, _len, _ref; result = {}; _ref = this.constructor.attributes; for (_i = 0, _len = _ref.length; _i < _len; _i++) { key = _ref[_i]; if (key in this) { if (typeof this[key] === 'function') { result[key] = this[key](); } else { result[key] = this[key]; } } } if (this.id) { result.id = this.id; } return result; }; Model.prototype.eql = function(rec) { 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.validate !== false) { error = this.validate(); if (error) { this.trigger('error', error); return false; } } this.trigger('beforeSave', options); record = this.isNew() ? this.create(options) : this.update(options); this.stripCloneAttrs(); this.trigger('save', options); return record; }; Model.prototype.stripCloneAttrs = function() { var key, value; if (this.hasOwnProperty('cid')) { return; } for (key in this) { if (!__hasProp.call(this, key)) continue; value = this[key]; if (this.constructor.attributes.indexOf(key) > -1) { delete this[key]; } } return this; }; Model.prototype.updateAttribute = function(name, value, options) { var atts; atts = {}; atts[name] = value; return this.updateAttributes(atts, options); }; Model.prototype.updateAttributes = function(atts, options) { this.load(atts); return this.save(options); }; Model.prototype.changeID = function(id) { var records; if (id === this.id) { return; } records = this.constructor.irecords; records[id] = records[this.id]; delete records[this.id]; this.id = id; return this.save(); }; Model.prototype.remove = function() { var i, record, records, _i, _len; records = this.constructor.records.slice(0); for (i = _i = 0, _len = records.length; _i < _len; i = ++_i) { record = records[i]; if (!(this.eql(record))) { continue; } records.splice(i, 1); break; } this.constructor.records = records; delete this.constructor.irecords[this.id]; return delete this.constructor.irecords[this.cid]; }; Model.prototype.destroy = function(options) { if (options == null) { options = {}; } this.trigger('beforeDestroy', options); this.remove(); this.destroyed = true; this.trigger('destroy', options); this.trigger('change', 'destroy', options); if (this.listeningTo) { this.stopListening(); } this.unbind(); return this; }; Model.prototype.dup = function(newRecord) { var atts; if (newRecord == null) { newRecord = true; } atts = this.attributes(); if (newRecord) { delete atts.id; } else { atts.cid = this.cid; } return new this.constructor(atts); }; Model.prototype.clone = function() { return createObject(this); }; Model.prototype.reload = function() { var original; if (this.isNew()) { return this; } original = this.constructor.find(this.id); this.load(original.attributes()); return original; }; Model.prototype.refresh = function(data) { var root; root = this.constructor.irecords[this.id]; root.load(data); this.trigger('refresh'); return this; }; Model.prototype.toJSON = function() { return this.attributes(); }; Model.prototype.toString = function() { return "<" + this.constructor.className + " (" + (JSON.stringify(this)) + ")>"; }; Model.prototype.fromForm = function(form) { var result; result = {}; /* for checkbox in $(form).find('[type=checkbox]:not([value])') result[checkbox.name] = $(checkbox).prop('checked') for checkbox in $(form).find('[type=checkbox][name$="[]"]') name = checkbox.name.replace(/\[\]$/, '') result[name] or= [] result[name].push checkbox.value if $(checkbox).prop('checked') for key in $(form).serializeArray() result[key.name] or= key.value */ return this.load(result); }; Model.prototype.exists = function() { return this.constructor.exists(this.id); }; Model.prototype.update = function(options) { var clone, records; this.trigger('beforeUpdate', options); records = this.constructor.irecords; records[this.id].load(this.attributes()); this.constructor.sort(); clone = records[this.id].clone(); clone.trigger('update', options); clone.trigger('change', 'update', options); return clone; }; Model.prototype.create = function(options) { var clone, record; this.trigger('beforeCreate', options); this.id || (this.id = this.cid); record = this.dup(false); this.constructor.addRecord(record); this.constructor.sort(); clone = record.clone(); clone.trigger('create', options); clone.trigger('change', 'create', options); return clone; }; Model.prototype.bind = function(events, callback) { var binder, singleEvent, _fn, _i, _len, _ref; this.constructor.bind(events, binder = (function(_this) { return function(record) { if (record && _this.eql(record)) { return callback.apply(_this, arguments); } }; })(this)); _ref = events.split(' '); _fn = (function(_this) { return function(singleEvent) { var unbinder; return _this.constructor.bind("unbind", unbinder = function(record, event, cb) { if (record && _this.eql(record)) { if (event && event !== singleEvent) { return; } if (cb && cb !== callback) { return; } _this.constructor.unbind(singleEvent, binder); return _this.constructor.unbind("unbind", unbinder); } }); }; })(this); for (_i = 0, _len = _ref.length; _i < _len; _i++) { singleEvent = _ref[_i]; _fn(singleEvent); } return this; }; Model.prototype.one = function(events, callback) { var handler; return this.bind(events, handler = (function(_this) { return function() { _this.unbind(events, handler); return callback.apply(_this, arguments); }; })(this)); }; Model.prototype.trigger = function() { var args, _ref; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; args.splice(1, 0, this); return (_ref = this.constructor).trigger.apply(_ref, args); }; Model.prototype.listenTo = function() { return Events.listenTo.apply(this, arguments); }; Model.prototype.listenToOnce = function() { return Events.listenToOnce.apply(this, arguments); }; Model.prototype.stopListening = function() { return Events.stopListening.apply(this, arguments); }; Model.prototype.unbind = function(events, callback) { var event, _i, _len, _ref, _results; if (arguments.length === 0) { return this.trigger('unbind'); } else if (events) { _ref = events.split(' '); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { event = _ref[_i]; _results.push(this.trigger('unbind', event, callback)); } return _results; } }; return Model; })(Module); Model.prototype.on = Model.prototype.bind; Model.prototype.off = Model.prototype.unbind; 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]'; }; isBlank = function(value) { var key; if (!value) { return true; } for (key in value) { return false; } return true; }; makeArray = function(args) { return Array.prototype.slice.call(args, 0); }; Model.isBlank = isBlank; Model.sub = function(instances, statics) { var Result; Result = (function(_super) { __extends(Result, _super); function Result() { 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(); } return Result; }; Model.setup = function(name, attributes) { var Ajax, Instance; if (attributes == null) { attributes = []; } Instance