Skip to content

Instantly share code, notes, and snippets.

@sagar-ganatra
Created October 12, 2012 07:02
Show Gist options
  • Save sagar-ganatra/3877713 to your computer and use it in GitHub Desktop.
Save sagar-ganatra/3877713 to your computer and use it in GitHub Desktop.
Collection calling Fetch by passing Success and Error handlers
var CarCollection = Backbone.Collection.extend({
//Specify REST URL
url: 'http://localhost:8500/rest/Car/CarService',
initialize: function () {
this.bind("reset", function (model, options) {
console.log("Inside event");
console.log(model);
});
}
});
var carCollectionInstance = new CarCollection();
carCollectionInstance.fetch({
success: function(response,xhr) {
console.log("Inside success");
console.log(response);
},
error: function (errorResponse) {
console.log(errorResponse)
}
});
(function () {
//Create a Car Collection
var CarCollection = Backbone.Collection.extend({
//Specify REST URL
url: 'http://localhost:8500/rest/Car/CarService',
//Parse the response
parse: function (response) {
console.log("Inside Parse");
//keys
var keys = response.COLUMNS;
//values
var values = response.DATA;
//Parse the response and construct models
for (var i = 0, length = values.length; i < length; i++) {
var currentValues = values[i];
var carObject = {};
for (var j = 0, valuesLength = currentValues.length; j < valuesLength; j++) {
carObject[keys[j]] = currentValues[j];
}
//push the model object
this.push(carObject);
}
console.log(this.toJSON());
//return models
return this.models;
},
initialize: function () {
this.bind("reset", function (model, options) {
console.log("Inside event");
console.log(model);
});
}
});
var carCollectionInstance = new CarCollection();
carCollectionInstance.fetch({
success: function(response,xhr) {
console.log("Inside success");
console.log(response);
},
error: function (errorResponse) {
console.log(errorResponse)
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment