Skip to content

Commit

Permalink
Merge branch 'friendly_errors'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedomas committed Jan 10, 2015
2 parents 0aa8628 + 2be2065 commit f995f1e
Show file tree
Hide file tree
Showing 22 changed files with 475 additions and 169 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Domas Bitvinskas <[email protected]>"
],
"description": "Exposes Ruby on Rails database to the Javascript side",
"description": "Provides Javascript a simple API to the Ruby on Rails CRUD",
"main": "dist/databound.js",
"dependencies": {
"jquery": "^2.1.1",
Expand Down
5 changes: 5 additions & 0 deletions bower_components/jquery/dist/jquery.min.js

Large diffs are not rendered by default.

67 changes: 59 additions & 8 deletions dist/databound-standalone.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Databound;
var Databound, DataboundError,
__slice = [].slice;

Databound = (function() {
function Databound(endpoint, scope, options) {
Expand Down Expand Up @@ -27,20 +28,31 @@ Databound = (function() {
Databound.prototype.where = function(params) {
var _this;
_this = this;
return this.request('where', params).then(function(records) {
records = records.concat(_this.seeds);
return this.wrappedRequest('where', params).then(function(resp) {
var records;
records = JSON.parse(resp.records).concat(_this.seeds);
_this.records = _.sortBy(records, 'id');
return _this.promise(_this.records);
});
};

Databound.prototype.all = function() {
return this.where();
};

Databound.prototype.find = function(id) {
var _this;
this.checkUndefinedId('find', id);
_this = this;
return this.where({
id: id
}).then(function() {
return _this.promise(_this.take(id));
var record;
record = _this.take(id);
if (!record) {
throw new DataboundError("Couldn't find record with id: " + id);
}
return _this.promise(record);
});
};

Expand All @@ -61,6 +73,7 @@ Databound = (function() {
};

Databound.prototype.destroy = function(id) {
this.checkUndefinedId('destroy', id);
return this.requestAndRefresh('destroy', {
id: id
});
Expand All @@ -83,11 +96,8 @@ Databound = (function() {
Databound.prototype.requestAndRefresh = function(action, params) {
var _this;
_this = this;
return this.request(action, params).then(function(resp) {
return this.wrappedRequest(action, params).then(function(resp) {
var records, records_with_seeds;
if (!(resp != null ? resp.success : void 0)) {
throw new Error('Error in the backend');
}
records = JSON.parse(resp.scoped_records);
records_with_seeds = records.concat(_this.seeds);
_this.records = _.sortBy(records_with_seeds, 'id');
Expand Down Expand Up @@ -115,6 +125,47 @@ Databound = (function() {
};
};

Databound.prototype.wrappedRequest = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.request.apply(this, args).then(_.bind(this.handleSuccess, this)).fail(this.handleFailure);
};

Databound.prototype.handleSuccess = function(resp) {
if (!(resp != null ? resp.success : void 0)) {
throw new Error('Error in the backend');
}
return this.promise(resp);
};

Databound.prototype.handleFailure = function(e) {
if (e.status === DataboundError.STATUS) {
throw new DataboundError(e.responseJSON.message);
} else {
throw new Error("Error in the backend with status " + e.status);
}
};

Databound.prototype.checkUndefinedId = function(action, id) {
if (!_.isUndefined(id)) {
return;
}
throw new DataboundError("Couldn't " + action + " a record without an id");
};

return Databound;

})();

DataboundError = (function() {
function DataboundError(text) {
this.message = "Databound: " + text;
}

DataboundError.STATUS = 405;

return DataboundError;

})();

DataboundError.prototype = new Error();
1 change: 1 addition & 0 deletions dist/databound-standalone.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 59 additions & 8 deletions dist/databound.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Databound, jQuery, _;
var Databound, DataboundError, jQuery, _,
__slice = [].slice;

_ = require('lodash');

Expand Down Expand Up @@ -31,20 +32,31 @@ Databound = (function() {
Databound.prototype.where = function(params) {
var _this;
_this = this;
return this.request('where', params).then(function(records) {
records = records.concat(_this.seeds);
return this.wrappedRequest('where', params).then(function(resp) {
var records;
records = JSON.parse(resp.records).concat(_this.seeds);
_this.records = _.sortBy(records, 'id');
return _this.promise(_this.records);
});
};

Databound.prototype.all = function() {
return this.where();
};

Databound.prototype.find = function(id) {
var _this;
this.checkUndefinedId('find', id);
_this = this;
return this.where({
id: id
}).then(function() {
return _this.promise(_this.take(id));
var record;
record = _this.take(id);
if (!record) {
throw new DataboundError("Couldn't find record with id: " + id);
}
return _this.promise(record);
});
};

Expand All @@ -65,6 +77,7 @@ Databound = (function() {
};

Databound.prototype.destroy = function(id) {
this.checkUndefinedId('destroy', id);
return this.requestAndRefresh('destroy', {
id: id
});
Expand All @@ -87,11 +100,8 @@ Databound = (function() {
Databound.prototype.requestAndRefresh = function(action, params) {
var _this;
_this = this;
return this.request(action, params).then(function(resp) {
return this.wrappedRequest(action, params).then(function(resp) {
var records, records_with_seeds;
if (!(resp != null ? resp.success : void 0)) {
throw new Error('Error in the backend');
}
records = JSON.parse(resp.scoped_records);
records_with_seeds = records.concat(_this.seeds);
_this.records = _.sortBy(records_with_seeds, 'id');
Expand Down Expand Up @@ -119,8 +129,49 @@ Databound = (function() {
};
};

Databound.prototype.wrappedRequest = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.request.apply(this, args).then(_.bind(this.handleSuccess, this)).fail(this.handleFailure);
};

Databound.prototype.handleSuccess = function(resp) {
if (!(resp != null ? resp.success : void 0)) {
throw new Error('Error in the backend');
}
return this.promise(resp);
};

Databound.prototype.handleFailure = function(e) {
if (e.status === DataboundError.STATUS) {
throw new DataboundError(e.responseJSON.message);
} else {
throw new Error("Error in the backend with status " + e.status);
}
};

Databound.prototype.checkUndefinedId = function(action, id) {
if (!_.isUndefined(id)) {
return;
}
throw new DataboundError("Couldn't " + action + " a record without an id");
};

return Databound;

})();

DataboundError = (function() {
function DataboundError(text) {
this.message = "Databound: " + text;
}

DataboundError.STATUS = 405;

return DataboundError;

})();

DataboundError.prototype = new Error();

module.exports = Databound;
2 changes: 1 addition & 1 deletion doc/assets/style.css

Large diffs are not rendered by default.

Loading

0 comments on commit f995f1e

Please sign in to comment.