2

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

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

但是如果我重新加载页面,则视图会正确显示。这是视图模型的示例:

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 语句修改为:

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

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

4

1 回答 1

2

这正是正确的方法!DUrandal 为您调用 Ko.applybindings ;) 意思是 Durandal 进行绑定!

热毛巾 SPA Durandal Knockout 和 Dynatree

于 2013-04-03T14:03:55.680 回答