저는 SPA 구축을 위해 Pluralsight에서 John Papa의 멋진 비디오를 보았습니다.페이지 번호가있는 CodeCamper 개선
이제는 세션 페이지에 대한 페이지 매김을 구현하려고하는데, 내가 한 일이 올바른 방법인지는 확실치 않습니다. 다른 사람이 비슷한 것을 시도해 본다면 의견을 보내 주시면 감사하겠습니다.
다음은 내가 수행 한 작업입니다.
sessions.html
<section>
<footer>
<ul class="pager">
<li>
<button class="btn btn-info btn-small" data-bind="click: previous, enable:canPrev"><i class="icon-angle-left"></i>Previous</button>
</li>
<li>
<button class="btn btn-info btn-small" data-bind="click: next, enable: canPrev">Next<i class="icon-angle-right"></i></button>
</li>
</ul>
</footer>
</section>
sessions.js
var currentPage = ko.observable(),
activate = function() {
currentPage(0);
return datacontext.getSessionPartials(sessions);
},
previous = function() {
currentPage(currentPage() - 1);
return datacontext.getSessionPartials(sessions, true, currentPage());
},
canPrev = ko.computed(function() {
return currentPage() > 0;
}),
canNext = ko.computed(function() {
//return currentPage > 0;
}),
next = function() {
currentPage(currentPage() +1);
return datacontext.getSessionPartials(sessions, true, currentPage());
},
var vm = {
//existing stuff plus the following:
previous: previous,
next: next,
currentPage: currentPage,
canPrev: canPrev,
canNext: canNext,
};
datacontext.js
var query = EntityQuery.from('Sessions')
.select('id, title, code, speakerId, trackId, timeSlotId, roomId, level, tags')
.skip(currentPage * 5).take(5)
.orderBy('timeSlotId, level, speaker.firstName')
.inlineCount(true);
inlineCount의 결과를 세션 viewModel에 추가하는 방법을 모르기 때문에 canNext를 제외하고는 작동합니다. 가장 좋은 방법은 무엇일까요?
를 추가 그것은 또한 당신이 돈 있음을 주목할 필요가 datacontext.js에서이 줄을 추가; t 동일한 기록을 두 번 이상 얻는 것에 대해 걱정해야합니다. Breeze는 이미 일부 레코드가 있고 병합 된 것을 알기에 충분히 똑똑합니다. –
John에게 답해 주셔서 감사합니다. 사실, 당신의 대답은 놀랍습니다. :) 좋아, Breeze는 캐싱을합니다. 그러나 데이터베이스에 백만 개의 레코드가 있다면, 나는 그들을 한꺼번에 가져오고 싶지 않습니다. 영원히 걸릴 것입니다. 따라서 '실제'페이징의 필요성. 위 코드에서와 같이 inlineCount() 함수를 사용하여 총 레코드 수를 얻을 수 있습니다. 그러나 당신에게 내 질문은 쿼리가 datacontext 서비스에서 실행될 때 세션 정보로 전달해야하는 것과 관련이 있습니다. 희망이 그게 ....? – Sam
100 만 개의 레코드가있는 경우 모두 가져 오지 마십시오. 페이징을 사용하십시오. 너는 페이지가 더 있는지 아는 법을 물었다. 그것이 대답의 두 번째 부분입니다 : 기록 카운트를 요구하여 무엇이 올지 알 수 있도록하십시오. datacontext 또는 일부 dataservice 모듈이이를 처리해야하고 viewmodel이이를 요구합니다. –