Skip to content

Commit

Permalink
Move per-status response handlers to class methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Aug 23, 2010
1 parent 0a3d9de commit 776fcbe
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/model_rest_persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,9 @@ Model.RestPersistence = function(resource, methods) {
});
},

// Rails' preferred failed validation response code, assume the response
// contains errors and replace current model errors with them.
handle422: function(xhr, textStatus, model) {
var data = Model.RestPersistence.parseResponseData(xhr);

if (data) {
model.errors.clear()

for (var attribute in data) {
for (var i = 0; i < data[attribute].length; i++) {
model.errors.add(attribute, data[attribute][i])
}
}
}
},

xhrComplete: function(xhr, textStatus, model, callback) {
// Allow custom handlers to be defined per-HTTP status code.
var handler = this["handle" + xhr.status]
var handler = Model.RestPersistence["handle" + xhr.status]
if (handler) handler.call(this, xhr, textStatus, model)

var success = textStatus === "success"
Expand All @@ -131,6 +115,22 @@ Model.RestPersistence = function(resource, methods) {
}
};

// Rails' preferred failed validation response code, assume the response
// contains errors and replace current model errors with them.
Model.RestPersistence.handle422 = function(xhr, textStatus, model) {
var data = Model.RestPersistence.parseResponseData(xhr);

if (data) {
model.errors.clear()

for (var attribute in data) {
for (var i = 0; i < data[attribute].length; i++) {
model.errors.add(attribute, data[attribute][i])
}
}
}
}

Model.RestPersistence.parseResponseData = function(xhr) {
try {
return /\S/.test(xhr.responseText) ?
Expand Down

0 comments on commit 776fcbe

Please sign in to comment.