Reputation: 413
Using the Hot Towel template, in a master-details kind of scenario I'm trying to get the session object using ko.dataFor(this)
to send it to the function that routes to the details page, but ko.dataFor(this)
is giving me nothing, or maybe (this) doesn't have the correct data in it from Breeze? I'm not sure if my problem is the view, the model, or even deeper somewhere in Breeze?
Here's the view:
<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>
and here are the functions called in the view model: (Edit: here is the entire view model):
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;
});
All the data loads from Breeze, but when I click on the div,
var session = ko.dataFor(this);
always returns undefined. I just log a $(this).text
it logs the contents of the <div>
, so something is there.
Upvotes: 0
Views: 4414
Reputation: 166
I noticed that you are loading knockout in module definition. This may be causing problems with knockout loading twice - once with the index.cshtml (as in the template) and then in your module. Try removing the knockout reference in module definition.
Upvotes: 3