녹아웃 JS - 관찰 가능한 배열은 세부 객체에서 채워져야합니다.
-
15-11-2019 - |
문제
여기에 바이올린이 있습니다 http://jsfiddle.net/srinivasanvee/yyewj/2/
카테고리, 제품, 수량이있는 목록이 있습니다. 드롭 다운 자체에서 새 범주를 추가 할 수있는 옵션이 있습니다 (선택 --add new-- 옵션), 카테고리 목록 ObservArray를 카테고리 가입 메소드에서 채우고 싶었습니다 (새로 추가 된 값은 그리드의 모든 행에 적용되어야 함). 그렇게하는 방법을 모르겠습니다. $ root.categorylist.push (이름), 행운은 없습니다
또는 우리는이 시나리오를 다루는 더 좋은 방법을 가지고 있습니까? 이것에 대한 도움이 정말로 감사 드리며 미리 감사드립니다.
해결책
Variables like $root
are only available within the bindings.
One way to make this work is to pass a reference to your root view model to your cartLine constructor.
Your cartLine would end up looking like:
var cartLine = function(data1, root) {
this.category = ko.observable(data1.category);
this.product = ko.observable(data1.product);
this.quantity = ko.observable(data1.quantity);
this.category.subscribe(function(newValue) {
if (newValue == "--Add New--") {
var name = prompt("Enter Table Name");
if (name == null) {
return false;
}
else {
root.categoryList.push(name);
}
}
});
};
Then, you just need to pass this
in as the second argument from your view model when creating a new cartLine. Sample here: http://jsfiddle.net/rniemeyer/kzZSH/
Otherwise, you could create your cartLine as you were and subscribe from your viewModel after getting a reference to the new line back.