我是淘汰 js 的新手,我有一个类似于这个 jsfiddle http://jsfiddle.net/daedalus28/vpjhZ/的要求,从 viewmodel 渲染 json 非常棒,但是我无法做到反之亦然,因为 json 来自服务器需要渲染相同的视图。如何在映射创建函数中映射 observablearray?
var FilterGroup = function(parent) {
this.join = ko.observable('All');
this.filters = ko.observableArray([new Filter(this)]);
this.addFilter = function() {
var filter = new Filter(this);
this.filters.push(filter);
this.filters.valueHasMutated();
};
this.addGroup = function() {
var group = new FilterGroup(this);
this.filters.push(group);
this.filters.valueHasMutated();
};
this.remove = (function(parent) {
return function() {
parent.filters.remove(this);
}
})(parent);
this.isRoot = (function(parent) {
return function() {
return parent ? false : true;
}
})(parent);
var mappingOptions = {
filters: {
create: function(args) {
return new function () {
filters = ko.observableArray([new Filter(args.data)]);
};
}
}
};
ko.mapping.fromJS(parent, mappingOptions, this);
};
var data = {filters:{join:'All',filters:[{field:{name:'IsDeleted',label:'Deleted'},modifier:'contains',criteria:'50'},{join:'All',filters:[{field:{name:'IsDeleted',label:'Deleted'},modifier:'equals',criteria:'20'}]},{field:{name:'Email',label:'Email'},modifier:'equals',criteria:'30'}]}};
var viewModel = {
filters: new FilterGroup(data.filters),
options: {
joins: ['All', 'Any'],
modifiers: [
'equals',
'not equal to',
'less than',
'greater than',
'contains',
'does not contain',
'starts with'
],
fields: [{"name":"Id","label":"ContactID"},{"name":"IsDeleted","label":"Deleted"}],
},
toJSON: function() {
return {
filters: this.filters
}
}
};
$(function() {
ko.applyBindings(viewModel);
})
;