tiger
Version:
A full port of Spine.js MVC framework to Titanium Mobile, with enhancements
351 lines (309 loc) • 9.63 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Ajax, Base, Collection, Extend, Include, Model, Pipeliner, Singleton, Tiger,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__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;
Tiger = this.Tiger || require('./tiger');
Model = Tiger.Model;
Pipeliner = require('/lib/icedlib').Pipeliner;
Ajax = {
getURL: function(object) {
return object && (typeof object.url === "function" ? object.url() : void 0) || object.url;
},
enabled: true,
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();
}
},
max: 100,
throttle: 0,
queue: function(request) {
this.pipeliner || (this.pipeliner = new Pipeliner(this.max, this.throttle));
if (!request) {
return this.pipeliner.queue;
}
await(this.pipeliner.waitInQueue(defer()));
return request(this.pipeliner.defer());
},
clearQueue: function() {
this.pipeliner.queue = [];
return this.pipeliner.n_out = 0;
}
};
Base = (function() {
function Base() {}
Base.prototype.defaults = {
contentType: 'application/json',
dataType: 'json',
processData: false,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
};
Base.prototype.queue = Ajax.queue;
Base.prototype.ajax = function(params, defaults) {
return new Tiger.Ajax(this.ajaxSettings(params, defaults));
};
Base.prototype.ajaxQueue = function(params, defaults) {
var defererror, defersuccess, request, rv, settings, xhr;
xhr = null;
rv = new iced.Rendezvous;
settings = this.ajaxSettings(params, defaults);
defersuccess = settings.success;
defererror = settings.error;
settings.success = rv.id('success').defer(data, statusText, xhr);
settings.error = rv.id('error').defer(xhr, statusText, error);
request = function(next) {
xhr = new Tiger.Ajax(settings);
await(rv.wait(defer(status)));
switch (status) {
case 'success':
defersuccess(data, statusText, xhr);
break;
case 'error':
defererror(xhr, statusText, error);
}
return next();
};
request.abort = function(statusText) {
var index;
if (xhr) {
return xhr.abort(statusText);
}
index = this.queue().indexOf(request);
if (index > -1) {
this.queue().splice(index, 1);
}
if (Ajax.pipeliner) {
Ajax.pipeliner.n_out--;
}
return request;
};
if (!Ajax.enabled) {
return request;
}
this.queue(request);
return request;
};
Base.prototype.ajaxSettings = function(params, defaults) {
return Tiger.extend({}, this.defaults, defaults, params);
};
return Base;
})();
Collection = (function(_super) {
__extends(Collection, _super);
function Collection(model) {
this.model = model;
this.failResponse = __bind(this.failResponse, this);
this.recordsResponse = __bind(this.recordsResponse, this);
}
Collection.prototype.find = function(id, params) {
var record;
record = new this.model({
id: id
});
return this.ajaxQueue(params, {
type: 'GET',
url: Ajax.getURL(record),
success: this.recordsResponse,
error: this.failResponse
});
};
Collection.prototype.all = function(params) {
return this.ajaxQueue(params, {
type: 'GET',
url: Ajax.getURL(this.model),
success: this.recordsResponse,
error: this.failResponse
});
};
Collection.prototype.fetch = function(params, options) {
var id,
_this = this;
if (params == null) {
params = {};
}
if (options == null) {
options = {};
}
if (id = params.id) {
delete params.id;
return this.find(id, params).done(function(record) {
return _this.model.refresh(record, options);
});
} else {
return this.all(params).done(function(records) {
return _this.model.refresh(records, options);
});
}
};
Collection.prototype.recordsResponse = function(data, status, xhr) {
return this.model.trigger('ajaxSuccess', null, status, xhr);
};
Collection.prototype.failResponse = function(xhr, statusText, error) {
return this.model.trigger('ajaxError', null, xhr, statusText, error);
};
return Collection;
})(Base);
Singleton = (function(_super) {
__extends(Singleton, _super);
function Singleton(record) {
this.record = record;
this.failResponse = __bind(this.failResponse, this);
this.recordResponse = __bind(this.recordResponse, this);
this.model = this.record.constructor;
}
Singleton.prototype.reload = function(params, options) {
return this.ajaxQueue(params, {
type: 'GET',
url: Ajax.getURL(this.record),
success: this.recordResponse(options),
error: this.failResponse(options)
});
};
Singleton.prototype.create = function(params, options) {
return this.ajaxQueue(params, {
type: 'POST',
data: JSON.stringify(this.record),
url: Ajax.getURL(this.model),
success: this.recordResponse(options),
error: this.failResponse(options)
});
};
Singleton.prototype.update = function(params, options) {
return this.ajaxQueue(params, {
type: 'PUT',
data: JSON.stringify(this.record),
url: Ajax.getURL(this.record),
success: this.recordResponse(options),
error: this.failResponse(options)
});
};
Singleton.prototype.destroy = function(params, options) {
return this.ajaxQueue(params, {
type: 'DELETE',
url: Ajax.getURL(this.record),
success: this.recordResponse(options),
error: this.failResponse(options)
});
};
Singleton.prototype.recordResponse = function(options) {
var _this = this;
if (options == null) {
options = {};
}
return function(data, status, xhr) {
var _ref, _ref1;
if (Spine.isBlank(data)) {
data = false;
} else {
data = _this.model.fromJSON(data);
}
Ajax.disable(function() {
if (data) {
if (data.id && _this.record.id !== data.id) {
_this.record.changeID(data.id);
}
return _this.record.updateAttributes(data.attributes());
}
});
_this.record.trigger('ajaxSuccess', data, status, xhr);
if ((_ref = options.success) != null) {
_ref.apply(_this.record);
}
return (_ref1 = options.done) != null ? _ref1.apply(_this.record) : void 0;
};
};
Singleton.prototype.failResponse = function(options) {
var _this = this;
if (options == null) {
options = {};
}
return function(xhr, statusText, error) {
var _ref, _ref1;
_this.record.trigger('ajaxError', xhr, statusText, error);
if ((_ref = options.error) != null) {
_ref.apply(_this.record);
}
return (_ref1 = options.fail) != null ? _ref1.apply(_this.record) : void 0;
};
};
return Singleton;
})(Base);
Model.host = '';
Include = {
ajax: function() {
return new Singleton(this);
},
url: function() {
var args, url;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
url = Ajax.getURL(this.constructor);
if (url.charAt(url.length - 1) !== '/') {
url += '/';
}
url += Tiger.Ajax.encode(this.id);
args.unshift(url);
return args.join('/');
}
};
Extend = {
ajax: function() {
return new Collection(this);
},
url: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
args.unshift(this.className.toLowerCase() + 's');
args.unshift(Model.host);
return args.join('/');
}
};
Model.Ajax = {
extended: function() {
this.fetch(this.ajaxFetch);
this.change(this.ajaxChange);
this.extend(Extend);
return this.include(Include);
},
ajaxFetch: function() {
var _ref;
return (_ref = this.ajax()).fetch.apply(_ref, arguments);
},
ajaxChange: function(record, type, options) {
if (options == null) {
options = {};
}
if (options.ajax === false) {
return;
}
return record.ajax()[type](options.ajax, options);
}
};
Model.Ajax.Methods = {
extended: function() {
this.extend(Extend);
return this.include(Include);
}
};
Ajax.defaults = Base.prototype.defaults;
Tiger.Ajax.ModelAdapter = Ajax;
Tiger.Ajax.Q = Base;
if (typeof module !== "undefined" && module !== null) {
module.exports = Ajax;
}
}).call(this);