I am working at fully understanding class definitions in JavaScript. Currently, I have a class defined like the following:
function Item() { this.init(); }
Item.prototype = {
init: function () {
this.data = {
id: 0,
name: "",
description: ""
}
},
save: function() {
$.ajax({
url: getUrl(),
type: "POST",
data: JSON.stringify(this.data),
contentType: "application/json",
success: save_Succeeded,
error: save_Failed
});
}
}
My problem is, I’m not sure how, or where, to define my save_Succeeded and save_Failed event handlers. Can someone please help me out? Thank you!
Add a
context:to your$.ajaxcall pointing tothisso that the correct object is passed asthiswhen the handlers are called.Something like:
(assuming that you also put
save_Succeededandsave_Failedinto theprototype)}