我是使用 Knockoutjs 的新手
我有一个 viewModel 定义如下:
// this is a subclass
var Order = {
selfdata: {
id : ko.observable(),
goodsName : ko.observable(''),
color : ko.observable(''),
size : ko.observable(''),
count: ko.observable(''),
orderDate: ko.observable(''),
remarks: ko.observable('')
},
}
// this is a viewModel
function ShoppingCar() {
var self = this;
self.orders = ko.observableArray([]);
self.show = function (msg) {
return msg.id;
}
//init
$.get('/Shopping/GetOrders', null, function (data) {
var items = $.map(data, function (item) { return Order.init(item) });// I write a init function to init order
self.orders(items);
});
}
//then I use Knockoutjs foreach binding as below:
<ul data-role="listview" data-theme="e" data-divider-theme="d">
<!-- ko foreach: orders -->
<li data-role="list-divider" data-bind="text: $data.selfdata.id">//!I get null value here</li>
<li><a href="#">
<h3 data-bind="text: $data.isValid"></h3>
<p><strong data-bind="text: $data.selfdata.color">//! here has null value too!</strong></p>
<p class="ui-li-aside"><strong>12:14</strong>PM</p>
</a></li>
<!-- /ko -->
所以,我的问题是,如何order.selfdata
在 ko 的 foreach 中显示?