javascript:durandaljs,viewmodel中的ApplyBinding

javascript:durandaljs,viewmodel中的ApplyBinding,javascript,knockout.js,durandal,Javascript,Knockout.js,Durandal,我正在尝试使用durandaljs制作一个具有动态内容的单页应用程序。例如,如果更改设置中的语言,则会更新UI。我使用signar从服务器加载对象,除了导航之外,其他一切都正常工作。第一次加载视图时,出现以下错误: Uncaught Error: Unable to parse bindings. Message: ReferenceError: router is not defined; Bindings value: compose: { model: router.ac

我正在尝试使用durandaljs制作一个具有动态内容的单页应用程序。例如,如果更改设置中的语言,则会更新UI。我使用signar从服务器加载对象,除了导航之外,其他一切都正常工作。第一次加载视图时,出现以下错误:

Uncaught Error: Unable to parse bindings.
Message: ReferenceError: router is not defined;
Bindings value: compose: { 
        model: router.activeItem, //wiring the router
        afterCompose: router.afterCompose, //wiring the router
        transition:'entrance', //use the 'entrance' transition when switching views
        cacheViews:true //telling composition to keep views in the dom, and reuse them (only a good idea with singleton view models)
        }
但如果重新加载页面,则视图将正确显示。 以下是viewmodel的一个示例:

define(function (require) {
    var p = require('hubs/myhub'),
        rep = require('repositories/myrepository');
    var myViewModel = function(data, proxy, cookie) {
        var self = this;
        self.proxy = proxy;
        self.cookie = cookie;
        self.Labels = ko.observableArray([]);
        try {
            self.proxy
                .invoke('Setup', self.cookie.Username, self.cookie.Language)
                .done(function (res) {
                    if (res.Result) {
                        console.log(JSON.stringify(res.Object, null, 4));
                        self.Labels(res.Object.Labels);
                    } else {
                        console.log(res.Error);
                    }
                });
        } catch (e) {
            console.log(e.message);
        }
    };
    return {
        activate: function () {
            var cookie = JSON.parse($.cookie(rep.cookieName));
            ko.applyBindings(myViewModel({}, p.proxy, cookie), document.getElementById('my_container'));
        }
    };
});
如果我取消激活功能的applyBinding,那么导航中就没有问题了。有没有合适的方法

我修改了以下内容的返回语句:

return {
        myModel: new myViewModel ({ }, p.proxy, JSON.parse($.cookie(rep.cookieName))),
        activate: function () {
            this.myModel.init();
        }
    };

并将信号器调用包装在init()函数中。现在一切都很好。

这正是正确的方法!DUrandal为您调用Ko.applybindings;)意思是Durandal负责装订