Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout ko.dataFor(this) undefined
    text
    copied!<p>Using the Hot Towel template, in a master-details kind of scenario I'm trying to get the session object using <code>ko.dataFor(this)</code> to send it to the function that routes to the details page, but <code>ko.dataFor(this)</code> 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?</p> <p>Here's the view:</p> <pre><code>&lt;section id="confschedule-view" class="view"&gt; &lt;h2 class="page-title" data-bind="text: title"&gt;&lt;/h2&gt; &lt;header&gt; &lt;button class="btn btn-info btn-force-refresh pull-right" data-bind="click: refresh"&gt; &lt;i class="icon-refresh"&gt;&lt;/i&gt; &lt;/button&gt; &lt;div class="article-counter"&gt; &lt;span data-bind="text: sessions().length"&gt;&lt;/span&gt;sessions in this conference. &lt;/div&gt; &lt;/header&gt; &lt;section data-bind="foreach: sessions()" class="view-list" id="view-list"&gt; &lt;article class="list-item"&gt; &lt;div class="session-brief" title="Go to session details"&gt; &lt;h3 data-bind="text: title"&gt;&lt;/h3&gt; &lt;div class="session-starttime"&gt; &lt;span data-bind="text: moment.utc(startTime()).format('dddd h:mm a')" class="session-starttime"&gt;&lt;/span&gt; &amp;nbsp;-&amp;nbsp; &lt;span data-bind="text: sessions_Rooms().sessions_Venues().venue"&gt;&lt;/span&gt; &lt;span data-bind="text: sessions_Rooms().room"&gt;&lt;/span&gt; &lt;/div&gt; &lt;div data-bind="foreach: sessions_CliniciansInSessions()"&gt; &lt;div class="clinician-name"&gt; &lt;span data-bind="text: sessions_Clinicians().firstName"&gt;&lt;/span&gt; &lt;span data-bind="text: sessions_Clinicians().lastName"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;span data-bind="text: sessionID"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/article&gt; &lt;/section&gt; &lt;/section&gt; </code></pre> <p>and here are the functions called in the view model: (Edit: here is the entire view model):</p> <pre><code>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 &amp;&amp; selectedSession.sessionID) { var url = '#/sessiondetail/' + selectedSession.sessionID; router.navigateTo(url); } } var vm = { activate: activate, sessions: sessions, title: 'Conference', refresh: refresh, viewAttached: viewAttached }; return vm; }); </code></pre> <p>All the data loads from Breeze, but when I click on the div, </p> <pre><code>var session = ko.dataFor(this); </code></pre> <p>always returns undefined. I just log a <code>$(this).text</code> it logs the contents of the <code>&lt;div&gt;</code>, so something is there. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload