1
在遍歷pager.Pages對象的foreach循環時,$ data是self(如預期的那樣)。但是,$ parent應該是尋呼機對象,但它正在返回WaterQualityResultViewModel。我究竟做錯了什麼?
ko.applyBindings(wqrvm, $('#dv_waterReports')[0]);
function WaterQualityResultViewModel() {
var self = this;
self.searchItem = new SearchItem();
self.items = ko.observableArray([]);
self.pager = new Pager();
self.search = function() {
$.ajax({
url: 'URL GOES HERE',
dataType: 'json',
data: {
'param': 'value'
},
async: false,
success: function(data) {
if ((data != null) && (data.searchResults !== undefined)) {
self.items(mappedItems);
self.pager.totalItemCount(data.resultsCount);
self.pager.lastPage(data.totalPages);
}
}
});
}
self.initFromUrl = function() {
var hash = getHash();
self.searchItem.geolocation(new geoLocation(0, 0, '', hash));
if (hash.currentpage !== 'undefined')
self.pager.currentPage(hash.currentpage);
if (hash.pagesize !== 'undefined')
self.pager.pageSize(hash.pagesize);
};
self.initialize = function() {
if (window.location.hash) {
self.initFromUrl();
self.search();
}
};
self.initialize();
}
function Pager() {
var self = this;
self.lastPage = ko.observable(1);
self.totalItemCount = ko.observable(0);
self.currentPage = ko.observable(1);
self.pageSize = ko.observable(10);
self.pageSlide = 2;
self.HasNextPage = ko.computed(function() {
return self.currentPage() < self.lastPage();
});
self.HasPrevPage = ko.computed(function() {
return self.currentPage() > 1;
});
self.FirstItemIndex = ko.computed(function() {
return self.pageSize() * (self.currentPage() - 1) + 1;
});
self.LastItemIndex = ko.computed(function() {
return Math.min(self.FirstItemIndex() + self.pageSize() - 1, self.totalItemCount());
});
self.Pages = ko.computed(function() {
//debugger;
var pageCount = self.lastPage();
var pageFrom = Math.max(1, self.currentPage() - self.pageSlide);
var pageTo = Math.min(pageCount, self.currentPage() + self.pageSlide);
pageFrom = Math.max(1, Math.min(pageTo - 2 * self.pageSlide, pageFrom));
pageTo = Math.min(pageCount, Math.max(pageFrom + 2 * self.pageSlide, pageTo));
var result = [];
for (var i = pageFrom; i <= pageTo; i++) {
result.push(i);
}
return result;
});
self.buildUrl = function() {
var locHash = encodeQueryData({
'currentpage': self.currentPage(),
'pagesize': self.pageSize()
});
return locHash;
};
}
下面的模板回報:
<!-- ko foreach:pager.Pages -->
<pre data-bind="text: 'pager is ' + JSON.stringify(ko.toJS($data))"></pre>
<pre data-bind="text: 'parent is: ' + JSON.stringify(ko.toJS($parent))"></pre>
<!-- /ko -->
你說得對。我可以使用$ parent.pager.currentPage()從pager對象獲取currentPage屬性。 –