backbone-http
Version:
An HTTP interface for BackboneORM
56 lines (44 loc) • 1.98 kB
JavaScript
// Generated by CoffeeScript 1.6.3
/*
backbone-http.js 0.5.0
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-http
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Dependencies: Backbone.js, Underscore.js, Moment.js, Inflection.js, BackboneORM, and Superagent.
*/
(function() {
var Cursor, HTTPCursor, JSONUtils, _, _ref,
__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; };
_ = require('underscore');
Cursor = require('backbone-orm').Cursor;
JSONUtils = require('backbone-orm').JSONUtils;
module.exports = HTTPCursor = (function(_super) {
__extends(HTTPCursor, _super);
function HTTPCursor() {
_ref = HTTPCursor.__super__.constructor.apply(this, arguments);
return _ref;
}
HTTPCursor.prototype.toJSON = function(callback) {
var query,
_this = this;
if (this.hasCursorQuery('$zero')) {
return callback(null, this.hasCursorQuery('$one') ? null : []);
}
return this.request.get(this.url).query(query = JSONUtils.toQuery(_.extend(_.clone(this._find), this._cursor))).type('json').end(function(err, res) {
var result;
if (err) {
return callback(err);
}
if (query.$one && (res.status === 404)) {
return callback(null, null);
}
if (!res.ok) {
return callback(new Error("Ajax failed with status " + res.status + " with: " + (JSON.stringify(res.body))));
}
result = JSONUtils.parse(res.body);
return callback(null, _this.hasCursorQuery('$count') || _this.hasCursorQuery('$exists') ? result.result : result);
});
};
return HTTPCursor;
})(Cursor);
}).call(this);