Knockout ko.dataFor(this)undefined

时间:2013-10-29 20:58:40

标签: jquery knockout.js breeze

使用Hot Towel模板,在主要细节类型的场景中,我试图使用ko.dataFor(this)将会话对象发送到路由到详细信息页面的函数,但是{{1}什么都没有给我,或者可能(这个)Breeze中没有正确的数据?我不确定我的问题是视图,模型,还是在Breeze的某个地方更深?

以下是观点:

ko.dataFor(this)

以下是视图模型中调用的函数: (编辑:这是整个视图模型):

<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>
                &nbsp;-&nbsp;
                <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>

所有数据都来自Breeze,但是当我点击div时,

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;
});

始终返回undefined。我只记录一个var session = ko.dataFor(this); 它记录了$(this).text的内容,所以有些东西存在。

1 个答案:

答案 0 :(得分:3)

我注意到你在模块定义中加载了敲除。这可能导致敲除加载两次出现问题 - 一次使用index.cshtml(如在模板中),然后在模块中。尝试删除模块定义中的挖空参考。