Created
October 12, 2012 07:02
-
-
Save sagar-ganatra/3877713 to your computer and use it in GitHub Desktop.
Collection calling Fetch by passing Success and Error handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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