使用 Hot Towel 模板,在主详细信息类型的场景中,我试图获取会话对象,ko.dataFor(this)
用于将其发送到路由到详细信息页面的函数,但ko.dataFor(this)
没有给我任何东西,或者可能 (this) 没有没有来自 Breeze 的正确数据?我不确定我的问题是视图、模型还是 Breeze 中某个更深的地方?
这是视图:
<section id="confschedule-view" class="view">
<h2 class="page-title" data-bind="text: title"></h2>
<header>
<button class="btn btn-info btn-force-refresh pull-right" data-bind="click: refresh">
<i class="icon-refresh"></i>
</button>
<div class="article-counter">
<span data-bind="text: sessions().length"></span>sessions in this conference.
</div>
</header>
<section data-bind="foreach: sessions()" class="view-list" id="view-list">
<article class="list-item">
<div class="session-brief" title="Go to session details">
<h3 data-bind="text: title"></h3>
<div class="session-starttime">
<span data-bind="text: moment.utc(startTime()).format('dddd h:mm a')" class="session-starttime"></span>
-
<span data-bind="text: sessions_Rooms().sessions_Venues().venue"></span>
<span data-bind="text: sessions_Rooms().room"></span>
</div>
<div data-bind="foreach: sessions_CliniciansInSessions()">
<div class="clinician-name">
<span data-bind="text: sessions_Clinicians().firstName"></span>
<span data-bind="text: sessions_Clinicians().lastName"></span>
</div>
</div>
<span data-bind="text: sessionID"></span>
</div>
</article>
</section>
</section>
以下是视图模型中调用的函数:(编辑:这是整个视图模型):
define(['services/logger', 'services/datacontext', '../Scripts/knockout-2.2.1.js', 'durandal/plugins/router'],
function (logger, datacontext, ko, router) {
var sessions = ko.observableArray();
function activate() {
logger.log('Conference Schedule Activated', null, 'confschedule', true);
return datacontext.getSessionPartials(sessions, false);
}
function refresh() {
return datacontext.getSessionPartials(sessions, true);
}
var viewAttached = function (view) {
//This is called when the view loads and the DOM is ready for jQuery
logger.log('viewAttached called. View:' + view.id.toString(), null, 'confschedule', true);
bindEventToList(view, '.session-brief', gotoDetails, 'click');
}
var bindEventToList = function (rootSelector, selector, callback, eventName) {
logger.log('bindEventToList Called', null, 'confschedule', true);
var eName = eventName || 'click';
$(rootSelector).on(eName, selector, function () {
var session = ko.dataFor(this);
if (session)
logger.log('[ko.dataFor(this)] got the session data', session, 'confschedule', true);
else
logger.log('[ko.dataFor(this)] got nothing!!!', session, 'confschedule', true);
callback(session);
return false;
});
}
var gotoDetails = function (selectedSession) {
logger.log('gotoDetails called.', selectedSession, 'confschedule', true);
if (selectedSession && selectedSession.sessionID) {
var url = '#/sessiondetail/' + selectedSession.sessionID;
router.navigateTo(url);
}
}
var vm = {
activate: activate,
sessions: sessions,
title: 'Conference',
refresh: refresh,
viewAttached: viewAttached
};
return vm;
});
所有数据都从 Breeze 加载,但是当我点击 div 时,
var session = ko.dataFor(this);
总是返回未定义。我只是记录一个$(this).text
它记录的内容<div>
,所以有些东西在那里。