app/assets/javascripts/spine/ajax.js in spine-rails-0.0.3 vs app/assets/javascripts/spine/ajax.js in spine-rails-0.0.4
- old
+ new
@@ -72,20 +72,41 @@
function Collection(model) {
this.model = model;
this.errorResponse = __bind(this.errorResponse, this);
this.recordsResponse = __bind(this.recordsResponse, this);
}
- Collection.prototype.findAll = function(params) {
+ Collection.prototype.find = function(id, params) {
+ var record;
+ record = new this.model({
+ id: id
+ });
return this.ajax(params, {
type: 'GET',
+ url: Ajax.getURL(record)
+ }).success(this.recordsResponse).error(this.errorResponse);
+ };
+ Collection.prototype.all = function(params) {
+ return this.ajax(params, {
+ type: 'GET',
url: Ajax.getURL(this.model)
}).success(this.recordsResponse).error(this.errorResponse);
};
Collection.prototype.fetch = function(params) {
- return this.findAll(params).success(__bind(function(records) {
- return this.model.refresh(records);
- }, this));
+ var id;
+ if (params == null) {
+ params = {};
+ }
+ if (id = params.id) {
+ delete params.id;
+ return this.find(id, params).success(__bind(function(record) {
+ return this.model.refresh(record);
+ }, this));
+ } else {
+ return this.all(params).success(__bind(function(records) {
+ return this.model.refresh(records);
+ }, this));
+ }
};
Collection.prototype.recordsResponse = function(data, status, xhr) {
return this.model.trigger('ajaxSuccess', null, status, xhr);
};
Collection.prototype.errorResponse = function(xhr, statusText, error) {
@@ -100,15 +121,17 @@
this.errorResponse = __bind(this.errorResponse, this);
this.blankResponse = __bind(this.blankResponse, this);
this.recordResponse = __bind(this.recordResponse, this);
this.model = this.record.constructor;
}
- Singleton.prototype.find = function(params) {
- return this.ajax(params, {
- type: 'GET',
- url: this.url
- });
+ Singleton.prototype.reload = function(params) {
+ return this.queue(__bind(function() {
+ return this.ajax(params, {
+ type: 'GET',
+ url: Ajax.getURL(this.record)
+ }).success(this.recordResponse).error(this.errorResponse);
+ }, this));
};
Singleton.prototype.create = function(params) {
return this.queue(__bind(function() {
return this.ajax(params, {
type: 'POST',
@@ -133,11 +156,11 @@
url: Ajax.getURL(this.record)
}).success(this.recordResponse).error(this.errorResponse);
}, this));
};
Singleton.prototype.recordResponse = function(data, status, xhr) {
- this.record.trigger('ajaxSuccess', this.record, status, xhr);
+ this.record.trigger('ajaxSuccess', status, xhr);
if (Spine.isBlank(data)) {
return;
}
data = this.model.fromJSON(data);
return Ajax.disable(__bind(function() {
@@ -146,13 +169,13 @@
}
return this.record.updateAttributes(data.attributes());
}, this));
};
Singleton.prototype.blankResponse = function(data, status, xhr) {
- return this.record.trigger('ajaxSuccess', this.record, status, xhr);
+ return this.record.trigger('ajaxSuccess', status, xhr);
};
Singleton.prototype.errorResponse = function(xhr, statusText, error) {
- return this.record.trigger('ajaxError', this.record, xhr, statusText, error);
+ return this.record.trigger('ajaxError', xhr, statusText, error);
};
return Singleton;
})();
Model.host = '';
Include = {