나는 내가 observableArray에 대해 뭔가 흥미로운 것을 발견 그러나 RP Niemeyer
의 우편함 라이브러리를 사용하고 동기화 모델을 발견하는 방법. 그래서 저는 새로운 대답을 만들었습니다. Niemeyer의 답을 완성하기 만하면됩니다.
postbox 및 observableArray를 사용하면 observableArray에서 요소를 추가하거나 제거 할 때 "subscribeTo"및 "publishOn"이벤트가 발생합니다. 배열 내 요소를 업데이트 할 때 아무 것도 실행되지 않습니다. 나는 이것이 우편함 라이브러리와 관련이 없지만 녹아웃 제한이라고 생각한다.
관찰 가능 어레이의 요소를 업데이트 할 때 이벤트를 얻으려는 경우에는 우편함 라이브러리에서 "게시"및 "가입"메소드를 사용하는 것이 좋습니다.
다음 FIDDLE
코드 참조 참조하십시오 : 당신이 모든 페이지에 검색 상자 모델을 가지고있는 상황에 대해
function FundEntity (fund)
{
var self = this;
self.id = fund.id;
self.fundName = fund.fundName;
self.description = fund.description;
self.isFavorite = ko.observable(fund.isFavorite);
}
function GridViewModel(model) {
var self = this;
self.fundList = ko.observableArray();
model.funds.forEach(function(fund) {
self.fundList.push(new FundEntity(fund));
});
self.favorite = function (id, index) {
var newValue = {
id: id,
index: index,
isFavorite: self.fundList()[index].isFavorite()
};
ko.postbox.publish("itemChanged", newValue);
return true;
};
self.isEditable = ko.observable().subscribeTo("myEditableTopic");
}
function FundDetailViewModel(model) {
var self = this;
self.fundList = ko.observableArray();
model.funds.forEach(function(fund) {
self.fundList.push(new FundEntity(fund));
});
ko.postbox.subscribe("itemChanged", function (newValue) {
self.fundList()[newValue.index].isFavorite(newValue.isFavorite);
});
self.editable = ko.observable(false).publishOn("myEditableTopic");
}
멋지게 보입니다. 감사합니다. – jaffa
@RP Niemeyer - ko scribable과 observable의 차이점은 무엇입니까? – Ryan
ko.subscribable은 관측 가능 및 계산 된 관측 가능 함수에서 기능을 가져 오는 기본 "클래스"입니다. 이것은 관측 가능 값과 계산 된 관측 값이 사용하는 기본 펍/서브 기능을 제공합니다. –