我正在使用MV * JavaScript模式创建一个应用程序,其中提供了以下功能
我的注释Model
就像:
var Note = (function(){
var Note = function(noteHeader,note){
this.noteHeader = noteHeader;
this.note = note;
this.noteId = new Date().getTime();
}
Note.prototype.getNoteHeader = function(){
return this.noteHeader;
}
Note.prototype.setNoteHeader = function(noteHeader){
this.noteHeader = noteHeader;
}
Note.prototype.getNote = function(){
return this.note;
}
Note.prototype.setNote = function(note){
this.note = note;
}
Note.prototype.getNoteId = function(){
return this.noteId;
}
Note.prototype.setNoteId = function(nodeId){
this.noteId = nodeId;
}
return Note;
})();
View
就像:
var NoteView = (function(){
var NoteView = function(note){
this.note = note;
};
NoteView.prototype.template = $("#addNoteTemplate").html();
NoteView.prototype.editTemplate = $("#createNoteTemplate").html();
NoteView.prototype.create = function(){
this.template = this.template.replace("@@addNoteHeader@@",this.note.getNoteHeader())
.replace("@@addNote@@",this.note.getNote())
.replace("@@noteId@@",this.note.getNoteId());
$noteContainer.append(this.template);
}
NoteView.prototype.bindevents = function(){
var that = this;
$("footer","#" + that.note.getNoteId()).off("click").on("click",function(){
$("#" + that.note.getNoteId()).find(".addNoteTemplate").addClass("hide");
$("#" + that.note.getNoteId()).append(that.editTemplate);
$("#" + that.note.getNoteId()).find(".headerInput").prop("value",that.note.getNoteHeader());
$("#" + that.note.getNoteId()).find("textarea").val(that.note.getNote());
$("#" + that.note.getNoteId()).find(".createButton").off("click").on("click",function(){
$("#" + that.note.getNoteId()).find(".addNoteTemplate").removeClass("hide");
that.note.setNoteHeader($("#" + that.note.getNoteId()).find(".headerInput").val());
that.note.setNote($("#" + that.note.getNoteId()).find("textarea").val());
$("#" + that.note.getNoteId()).find(".createNoteTemplate").remove();
$("#" + that.note.getNoteId()).find(".noteHeader").html(that.note.getNoteHeader());
$("#" + that.note.getNoteId()).find(".noteText").html(that.note.getNote());
});
});
}
return NoteView;
})();
和Controller like thing
如下:
var $noteContainer = $("#noteContainer");
var noteCollection = [];
var init = function(){
window.noteCollection = noteCollection;
render();
}
var render = function(){
createNoteTemplate();
}
var createNoteTemplate = function(){
var createNoteTemplate = $("#createNoteTemplate").html();
$noteContainer.append(createNoteTemplate);
$(".createButton",$noteContainer).off("click").on("click",function(){
var note = new Note($("input").val(),$("textarea").val());
var noteView = new NoteView(note);
noteView.create();
noteView.bindevents();
noteCollection.push(note);
});
}
问题:
event-view-model
之间进行通信(就像我正在编辑注释而不是编辑事件我想要更改视图和模型两者)。Github上提供了完全正常工作的代码,请指导。