使用AMD时未定义Knockout组件视图模型

时间:2015-03-24 16:57:06

标签: javascript mvvm knockout.js requirejs components

我一直在尝试使用带有require.js的Knockout组件来处理我正在进行的项目。当我使用该组件时,它将显示HTML模板,但模板中的绑定会引发一个引用错误,表示该字段未定义。 我相信这是因为1. viewModel未定义,或2. viewModel未绑定到模板。但是我无法弄清楚如何解决它。

我在phpStorm中使用内置服务器和Firefox开发人员版

的index.php

<?php
    namespace project;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Project | Admin</title>

    <link rel="stylesheet" href="css/grid.css"/>

    <style>
    </style>

</head>
<body>

    <admin-header params=""></admin-header>

    <script src="js/vendor/require.js" data-main="js/app"></script>
</body>
</html>

app.js

    requirejs.config({
    paths: {

        jquery: 'vendor/jquery',

        postbox: 'vendor/knockout-postbox',
        domReady: 'vendor/domReady',
        text: 'vendor/text',
        knockout: 'vendor/knockout',

        hashchange: 'vendor/hashchange',
        underscore: 'vendor/underscore',

        ComponentManager: 'components/ComponentManager'
    },

    shim: {
        knockout: {
            deps: ['jquery']
        },
        hashchange: {
            deps: ['jquery']
        },
        underscore:{
            exports: '-'
        }
    }
});



require(['knockout', 'AdminViewModel', 'domReady!'], function(ko, AdminViewModel){
    ko.applyBindings(new AdminViewModel());
});

AdminViewModel.js

 define(['knockout', 'postbox', 'ComponentManager'], function( ko, postbox,  ComponentManager) {
    return function AdminViewModel() {
        var self = this;

        self.pageTitle = ko.observable('Home').publishOn('pageTitle');

        var componentManager = new ComponentManager();
        componentManager.registerComponents(['admin-header']);

        /*
        // doesnt work when registered in the parent view either

        ko.components.register(
            'admin-header', {
                require: 'components/admin-header/admin-header'
            }
        );
        */

    }
});

的ComponentManager,JS

define(['knockout', 'underscore', 'require'], function(ko, _, require){
    return function ComponentManager() {
        this.registerComponents = function(components) {

            _.each(components, function (component) {
                var componentPath = 'components/' + component + '/' + component;

                console.log("registering: " + componentPath );
                ko.components.register(component, {
                    require:  componentPath
                });
            });

        }
    }
});

管理员-header.js 定义([&#39;淘汰&#39;,&#39;邮箱&#39;,&#39;文字!./ admin-header_template.html&#39;],

function(ko, postbox, template){

   function AdminHeaderModel(params){
        var self = this;

        self.pageTitle = ko.observable('Title');//.subscripeTo('pageTitle', true, function(title){return title.toUpperCase()});
   }


    return {
        viewmodel: AdminHeaderModel,
        template: template
    };

});

管理员集管template.html

<div>
<h1>Project</h1>
<h1 data-bind="text: pageTitle"></h1>
</div>

1 个答案:

答案 0 :(得分:0)

在您的admin-header组件代码中,您需要返回viewModel属性而不是viewmodel