Durandal 2.0,微风,淘汰赛绑定问题

时间:2014-03-07 18:59:15

标签: knockout.js breeze durandal

确定。我一直在看这几天,我很难过。有人能告诉我这段代码有什么问题。任何帮助都会很棒。

我已将durandal Starter套件与来自微风的温和样品的修改版本合并。

我在加载Shell时尝试从数据库加载用户,这样我就可以根据用户动态生成菜单。我知道对数据库的调用会返回数据。我可以在FireFox的Data.Results中看到它。

但是当我加载页面时,我收到以下错误。

“无法解析绑定。 绑定值:value:firstName 消息:未定义firstName; 查看:views / shell; ModuleId:viewmodels / shell“

Shell.js

define(['durandal/system', 'plugins/router', 'services/logger', 'services/entitymanagerprovider', 'model/modelBuilder', 'services/errorhandler'],
function (system, router, logger, entitymanagerprovider, modelBuilder, errorhandler) {

    entitymanagerprovider.modelBuilder = modelBuilder.extendMetadata;

    var shell = {
        activate: activate,
        attched: attached,
        router: router,
        search: search,
        user: ko.observable()
    };

    errorhandler.includeIn(shell);

    return shell;

    function activate() {
        return entitymanagerprovider
            .prepare()
            .then(bootPrivate)
            .fail(function (e) {
                if (e.status === 401) {
                    return bootPublic();
                } else {
                    shell.handleError(e);
                    return false;
                }
            });
    }

    function bootPrivate() {

        var self = this;
        log('TempHire Loaded!', null, true);

        var query = new breeze.EntityQuery().from('resource/Users');
        return entitymanagerprovider.manager().executeQuery(query)
            .then(function (data) {
                shell.user(data.results);
                router.map([
                    { route: '', title: 'Welcome', moduleId: 'viewmodels/welcome', nav: true },
                    { route: 'flickr', moduleId: 'viewmodels/flickr', nav: true }
                ]).buildNavigationModel();
                return router.activate();
            })
            .fail(
                alert("failed")
            );
    }

    function bootPublic() {
        return router
            .makeRelative({ moduleId: 'viewmodels' })
            .map([
                { route: '', moduleId: 'login', nav: true}])
            .mapUnknownRoutes('login', 'not-found')
            .activate();
    }

    function attached() {

    }

    function search() {
        //It's really easy to show a message box.
        //You can add custom options too. Also, it returns a promise for the user's response.
        app.showMessage('Search not yet implemented...');
    }

    function log(msg, data, showToast) {
        logger.log(msg, data, system.getModuleId(shell), showToast);
    }
});

shell.html

define(['durandal/system', 'plugins/router', 'services/logger', 'services/entitymanagerprovider', 'model/modelBuilder', 'services/errorhandler'],
function (system, router, logger, entitymanagerprovider, modelBuilder, errorhandler) {

    entitymanagerprovider.modelBuilder = modelBuilder.extendMetadata;

    var shell = {
        activate: activate,
        attched: attached,
        router: router,
        search: search,
        user: ko.observable()
    };

    errorhandler.includeIn(shell);

    return shell;

    function activate() {
        return entitymanagerprovider
            .prepare()
            .then(bootPrivate)
            .fail(function (e) {
                if (e.status === 401) {
                    return bootPublic();
                } else {
                    shell.handleError(e);
                    return false;
                }
            });
    }

    function bootPrivate() {

        var self = this;
        log('TempHire Loaded!', null, true);

        var query = new breeze.EntityQuery().from('resource/Users');
        return entitymanagerprovider.manager().executeQuery(query)
            .then(function (data) {
                shell.user(data.results);
                router.map([
                    { route: '', title: 'Welcome', moduleId: 'viewmodels/welcome', nav: true },
                    { route: 'flickr', moduleId: 'viewmodels/flickr', nav: true }
                ]).buildNavigationModel();
                return router.activate();
            })
            .fail(
                alert("failed")
            );
    }

    function bootPublic() {
        return router
            .makeRelative({ moduleId: 'viewmodels' })
            .map([
                { route: '', moduleId: 'login', nav: true}])
            .mapUnknownRoutes('login', 'not-found')
            .activate();
    }

    function attached() {

    }

    function search() {
        //It's really easy to show a message box.
        //You can add custom options too. Also, it returns a promise for the user's response.
        app.showMessage('Search not yet implemented...');
    }

    function log(msg, data, showToast) {
        logger.log(msg, data, system.getModuleId(shell), showToast);
    }
});

entitymanagerprovider.js

define(['durandal/app'],
function(app) {
    breeze.NamingConvention.camelCase.setAsDefault();
    var serviceName = 'breeze';
    var masterManager = new breeze.EntityManager(serviceName);

    var EntityManagerProvider = (function() {

        var entityManagerProvider = function() {
            var manager;

            this.manager = function() {
                if (!manager) {
                    manager = masterManager.createEmptyCopy();

                    // Populate with lookup data
                    manager.importEntities(masterManager.exportEntities());

                    // Subscribe to events
                    manager.hasChangesChanged.subscribe(function(args) {
                        app.trigger('hasChanges');
                    });
                }

                return manager;
            };
        };

        return entityManagerProvider;
    })();

    var self = {
        prepare: prepare,
        create: create,
        manager: manager
    };

    return self;

    function create() {
        return new EntityManagerProvider();
    }

    function prepare() {
        return masterManager.fetchMetadata()
            .then(function() {
                if (self.modelBuilder) {
                    self.modelBuilder(masterManager.metadataStore);
                }

                var query = breeze.EntityQuery
                    .from('resource/lookups');

                return masterManager.executeQuery(query);
            });
    }

    function manager() {

        return masterManager;
    }

}

);

1 个答案:

答案 0 :(得分:0)

无法看到shell.html,很难回答这个问题。但是您没有将firstName暴露给视图。在shell.js中,您需要将其变为变量。

如果firstName是用户中的属性,则在您的html中,您将其引用为:

<span data-bind='text: user().firstName'></span>