'Item'이라는 자바 스크립트 클래스가 있습니다. '항목'은 다음과 같이 정의됩니다.자바 스크립트에서 이벤트 발생
function Item() { this.create(); }
Item.prototype = {
create: function() {
this.data = {
id: getNewID(),
}
},
save: function() {
$.ajax({
url: getBackendUrl(),
type: "POST",
data: JSON.stringify(this.data),
contentType: "application/json",
success: save_Succeeded,
error: save_Failed
});
},
function save_Succeeded(result) {
// Signal an event here that other JavaScript code can subscribe to.
}
function save_Failed(e1, e2, e3) {
// Signal an event here that other JavaScript code can subscript to.
}
}
참고 : 저는 C# 배경에서 온 것입니다. 그래서 내가 성취하고 싶은 것이 가능한 것인지 확신 할 수 없습니다. 하지만 본질적으로, 객체를 만들고, 이벤트 핸들러를 구독하고, 객체를 저장하려고합니다. 예를 들어, 내 코드 전체에서 다음과 같은 작업을 수행합니다.
var i = new Item();
i.item_save_succeeded = function() {
// Do stuff when the item has successfully saved
};
i.item_save_failed = function() {
// Do stuff when the item has failed to save
};
i.save(); // start the save process
자바 스크립트에서도이 이벤트 기반 방법이 가능합니까? 그렇다면 어떻게? 내가 뭘 놓치고 있니? 애매한 다양한 오류가 계속 발생합니다. 그렇기 때문에 나는 점점 더 멀어지고 있는지 잘 모르겠습니다.
도움 주셔서 감사합니다.
나는'$'을 본다. jQuery를 사용하고 있습니까? – epascarello
@epascarello 그는 $ .ajax()가 전형적인 jQuery 구문이므로 $ this라고 가정합니다. – Bram