knockout을 사용하여 주석 필드를 만들고 foreach를 사용할 때 충돌이 발생하여 여러 뷰 모델을 하나의 부모 viewmodel로 변경했습니다. 내 새로운 문제는 부모 ViewModel에서 함수를 실행하는 것입니다. 내 코멘트() 함수는 작동하지만 javascript 내에서 commentSection()을 호출하는 데 문제가 있습니다.ViewModel 함수에 액세스
commentSection 함수에서 getNewEntries를 호출하려고합니다. 이 방법은 vm.cSection.getNewEntries를 사용하여 Viewmodel (VM)을 호출하는 것이지만 콘솔은 함수가 아니라고 말합니다. 그래서 내 질문에, 어떻게 VM을 통해 getNewEntries이 함수를 호출 할 수 있습니까? 여기
는 보이는 방법은 다음과 같습니다
function Comment() {
var self = this;
self.nickname = ko.observable();
self.newMsg = ko.observable("");
self.sendEntry = function() {
if (self.newMsg() !== "" && self.nickname() !== "") {
$.post(writeUrl, "entry=" + ko.toJSON(self));
self.newMsg("");
}
};
}
function commentSection() {
var self = this;
self.timestamp = 0;
self.comments = ko.observableArray();
self.editable = ko.observable(false);
self.deleteComment = function() {
vm.cSection.comments.remove(self);
};
self.editComment = function() {
self.editable(!self.editable());
};
self.getNewEntries = function() {
$.getJSON(readUrl, "timestamp=" + self.timestamp, function(comments) {
for (var i = 0; i < comments.length; i++) {
var entry = comments[i];
if (entry.timestamp > self.timestamp) {
self.timestamp = entry.timestamp;
}
self.comments.unshift(entry);
}
self.getNewEntries();
});
};
}
function ViewModel() {
var self = this;
self.cSection = ko.observable(new commentSection());
self.comments = ko.observableArray();
self.selectedComment = ko.observable(new Comment());
//self.cSection.getNewEntries();
}
var vm = new ViewModel();
ko.applyBindings(vm);
vm.cSection.getNewEntries();
});
내 질문이 확실하지 않은 경우 알려주십시오. 미리 감사드립니다!
아. 신기한 실수! 고마워. :) –