我不清楚如何在 MVC4 SPA HotTowel 模板中实现以过滤谓词作为参数的新视图的架构导航。假设我有一个带有日期(过滤器)的下拉菜单和一个按钮“gotoSessions”来导航到会话视图。GoToSessions 按钮将导航到仅显示所选日期的会话的会话视图。不选择日期将强制会话视图显示所有可用的会话。
更新 config.js 中的“会话”路由以接受过滤器参数
var routes = [{ url: 'sessions/:filter', moduleId: 'viewmodels/sessions', name: 'Sessions', visible: true, caption: '<i class="icon-book"></i> Sessions' }, {... the rest of the routes ..}];
实现“gotoSessions”按钮
var selectedDate = ko.observable(); var gotoSessions = function(viewData) { if (viewData && viewData.selectedDate) { // build filter to pass to Breeze QueryManager as where clause var filter = viewData.selectedDate.peek(); // build url with filter parameter var url = '#/sessions/' + predicate; router.navigateTo(url); } };
在 session.js 视图模型中更新 activate 方法以接受 routeData 作为参数并在 datacontext.js 服务中实现过滤
var activate = function (context) { var filterParam = context.filter; // update datacontext getSessionsPartials method to accept filter parameter return datacontext.getSessionsPartials(sessions, false, filterParam ); };
在 services/datacontext.js 中更新查询以使用过滤器
var getSpeakersPartials = function (speakersObservable, forceRemote, filter) { //... code ... var query = EntityQuery.from('Speakers') .select('id, firstName, lastName, imageSource') .where(... filter here ...) .orderBy(orderBy.speaker); //... rest of the code... }; function getLocal(resource, filtering, ordering) { var query; if (filtering) { query = EntityQuery.from(resource) .where('yourPropertyNameHere', 'equals', filtering) .orderBy(ordering); } else { query = EntityQuery.from(resource).orderBy(ordering); } }
这是 MVC4 SPA HotTowel 架构的正确实现吗?您能否提供过滤器(谓词)格式的代码示例?谢谢