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());
...
}
enabled
json数组中每个模块对象中的键值不应该在我的控制台中显示为可观察函数,其值设置为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 成为一个可观察的数组,但这没有用。
任何帮助将非常感激!