1
다음 knockout.js 코드가 있습니다.knockout.js 바인딩 인스턴스를 추가하고 바꾸는 방법은 무엇입니까?
다음과 같습니다.
var itemLine = function() {
this.itemId = ko.observable();
this.itemType = ko.observable();
///this.otherFields = = ko.observable();....
};
var itemSet = function() {
this.items = ko.observableArray(_.map(initialData, function (item) {
var newItem = new itemLine();
newItem.itemId(item.itemId);
newItem.itemType(item.itemType);
return newItem;
}));
this.current = new itemLine();
// works, but it req duplication of all my fields
this.add = function() {
var newItem = new itemLine();
newItem.itemId(this.current.itemId());
newItem.itemType(this.current.itemType());
this.items.push(newItem);
this.current.itemerId("");
this.current.itemType("");
};
// doesn't work
this.add = function() {
this.items.push(this.current);
this.current = new interviewLine(); // bindings are lost
};
두 번째 구현이 잘못되었습니다. 이 작업을 수행하는 올바른 방법은 무엇입니까?
감사합니다. 그게 내가하려는 일입니다. "현재"편집기를 템플릿으로 사용하지 않고도이 작업을 수행 할 수 있습니까? –
예, http://jsfiddle.net/rniemeyer/KSPGA/와 같습니다. 바인딩은 dependentObservables로 구현되므로 바인딩에서 current()에 액세스하면 현재 변경 사항을 언제든지 다시 실행하고 원하는 결과를 제공해야합니다. –