var modules;
$.when(
$.get('modules.json', function(data) {
var mapping = { 'observe' : ['enabled'] };
modules = ko.mapping.fromJS(data, mapping);
})
).then(function() {
ko.applyBindings(new viewModel(modules));
});
function viewModel(modules) {
var self = this;
self.modules = modules;
console.log(self.modules());
...
}
不應該從JSON陣列中的每個模塊對象enabled
鍵值顯示爲我設置什麼是在JSON中定義的值控制檯可觀察到的功能(在這種情況下true
)?映值不成爲觀察的,等
另外,有什麼更好的方式等待綁定接收的json數據比我現在嘗試的方式(如果有更好的方法)?
從我的JSON數組對象看起來是這樣的:
{
"id" : "spnsrlogo",
"name" : "Sponsor Logo",
"code" : "<h3>Sponsor Logo</h3><label for=\"spnsrLogoSrc\" data-bind=\"text: $root.spnsrLogoSrc\">Sponsor Logo Path</label><input type=\"text\" id=\"spnsrLogoSrc\" data-bind=\"value: $root.spnsrLogoSrc, valueUpdate: 'afterkeydown'\">",
"output" : "assets/img/sponsor-logo.png",
"enabled" : true
}
另外,我怎麼可以利用code
值,以便當它被附加了可觀變得活躍?舉個例子:
<div>
<div data-bind="foreach: modules">
<input type="checkbox" data-bind="checked: $parent.modulesEnabled(), value: $data.id, attr: { id: $data.id }">
<label data-bind="attr: { for: $data.id }, text: $data.name" class="inlineblock"></label>
</div>
</div>
<div id="modulecontainer" data-bind="foreach: modules">
<div class="moduleoptions" data-bind="visible: $parent.modulesEnabled(), html: code, attr: { id: 'module-' + $data.id}"></div>
</div>
我試着讓modulesEnabled一個可觀察的數組,但是沒有奏效。
任何幫助將不勝感激!