我有单个网格页面并尝试使用Knockout绑定数据,但我想有一个逻辑问题(因为我)。数据绑定在从json获取数据之前开始。
这里是javascript;
$(function () {
my.PriceListViewModel = (function () {
var
data = ko.observableArray([]),
totalCount = ko.observable(),
currentPage = ko.observable(1),
page = ko.observable(my.PageModel),
goTo = function (d) {
$.getJSON("/prices/GetByFilterViaJSON?limit=3&page=" + d, function (data) {
Data(data.Data);
CurrentPage(d);
});
},
loadData = function () {
$.getJSON("/prices/GetByFilterViaJSON?limit=3", function (list) {
$.each(list, function (key, val) {
data.push(val);
});
my.PageModel.LoadData(data);
Page = my.PageModel;
});
};
return {
Data: data,
TotalCount: totalCount,
CurrentPage: currentPage,
Page: page,
GoTo: goTo,
LoadData: loadData
};
})();
my.PageModel = (function () {
loadData = function (data) {
ko.mapping.fromJS(data.Page, {}, this);
};
return {
LoadData: loadData
};
})();
my.PriceListViewModel.LoadData();
debugger;
ko.applyBindings(my.PriceListViewModel);
});
和html;
<div class="section table_section">
<div class="section_inner" id="divTestKoRemoteGrid" >
<div class="title_wrapper">
<h2 data-bind="text: TotalCount">
Prices</h2>
</div>
<div class="section_content">
<div id="product_list">
<!--[if !IE]>start table_wrapper<![endif]-->
<div class="table_wrapper">
<div class="table_wrapper_inner">
<table cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th>
No.
</th>
<th>
Tier
</th>
<th>
</th>
</tr>
<!-- ko foreach: Data -->
<tr data-bind="css: { second: $index() % 2 }">
<td data-bind="text: Id" style="width: 26px;">
</td>
<td data-bind="text: Tier" style="width: 35px;">
</td>
<td style="width: 120px;">
<div class="actions_menu">
<ul>
<li><a class="edit" href="">edit</a></li>
<li><a class="delete" href="">deny</a></li>
</ul>
</div>
</td>
</tr>
<!-- /ko -->
</tbody>
</table>
</div>
</div>
<!--[if !IE]>end table_wrapper<![endif]-->
</div>
<!--[if !IE]>start pagination<![endif]-->
<div class="pagination">
<span class="page_no" data-bind="text: CurrentPage()"></span>
<ul class="pag_list" data-bind="foreach: ko.utils.range(1, Page.TotalPage)">
<li><a href="" data-bind="click: $root.GoTo, css:{current_page: $data==$root.CurrentPage()}"><span><span data-bind="text: $data"></span></span></a>
</li>
</ul>
</div>
<!--[if !IE]>end pagination<![endif]-->
</div>
</div>
如何在绑定开始之前获取数据?
答案 0 :(得分:1)
你可以调用ko.applyBindings(my.PriceListViewModel);只有在检索到初始数据后才会这样做。
答案 1 :(得分:0)
在文档就绪功能中调用应用绑定。
答案 2 :(得分:-1)
我找到了解决方案。
我使用了异步的$ .getJson方法,因此在加载viewmodel之后数据就会出现。
我用$ .ajax.async = false更改了$ .getJson方法,然后一切正常。
my.PriceListViewModel = (function () {
var
data = ko.observableArray([]),
totalCount = ko.observable(),
page = ko.observable(),
goTo = function (d) {
$.getJSON("/prices/GetByFilterViaJSON?limit=6&page=" + d, function (list) {
data(list.Data);
page(list.Page);
});
},
loadData = function () {
$.ajax({
type: 'GET',
url: '/prices/GetByFilterViaJSON',
dataType: 'json',
success: function (list) {
data(list.Data);
page(list.Page);
totalCount(list.TotalCount);
},
data: { limit: 6 },
async: false
});
};
return {
Data: data,
TotalCount: totalCount,
Page: page,
GoTo: goTo,
LoadData: loadData
};
})();
my.PriceListViewModel.LoadData();
ko.applyBindings(my.PriceListViewModel);