使用 ko.mapping 使 knockout observablearray 项目可观察
Make knockout observablearray items observable with ko.mapping
我有这个型号:
var child = function(c){
var self = this;
self.id = ko.observable(c.id);
self.first_name = ko.observable(c.first_name);
self.last_name = ko.observable(c.last_name);
self.gender_id = ko.observable(c.gender_id);
self.birthday= ko.observable(c.birthday);
self.family_id = ko.observable(c.family_id);
}
我有一个这样定义的视图模型:
var childrenViewModel = function(initialData){
var self = this;
//
self.children = ko.observableArray(initialData);
//
self.removeChild = function(){};
};
我是这样获取初始数据的:
$.ajax({
type: 'GET',
url: getChildrenListUrl,
contentType: 'application/json'
})
.done(function (data) {
vmChildren =new childrenViewModel(JSON.parse(data));
ko.applyBindings(vmChildren,document.getElementById('portlet-children'));
Metronic.unblockUI('#portlet-children');
});
我现在想通过使用映射插件制作子 observablearray 的内容,observables 本身,但我在这样做时遇到了困难...
为什么需要 ko.mapping
,难道你只是想将 initialData
映射为 child
的实例吗?
self.children = ko.observableArray(initialData.map(function(e){
return new child(e);
}));
我有这个型号:
var child = function(c){
var self = this;
self.id = ko.observable(c.id);
self.first_name = ko.observable(c.first_name);
self.last_name = ko.observable(c.last_name);
self.gender_id = ko.observable(c.gender_id);
self.birthday= ko.observable(c.birthday);
self.family_id = ko.observable(c.family_id);
}
我有一个这样定义的视图模型:
var childrenViewModel = function(initialData){
var self = this;
//
self.children = ko.observableArray(initialData);
//
self.removeChild = function(){};
};
我是这样获取初始数据的:
$.ajax({
type: 'GET',
url: getChildrenListUrl,
contentType: 'application/json'
})
.done(function (data) {
vmChildren =new childrenViewModel(JSON.parse(data));
ko.applyBindings(vmChildren,document.getElementById('portlet-children'));
Metronic.unblockUI('#portlet-children');
});
我现在想通过使用映射插件制作子 observablearray 的内容,observables 本身,但我在这样做时遇到了困难...
为什么需要 ko.mapping
,难道你只是想将 initialData
映射为 child
的实例吗?
self.children = ko.observableArray(initialData.map(function(e){
return new child(e);
}));