breeze.config.MetadataHelper 不是 Durandal 和 Breeze 的构造函数错误

breeze.config.MetadataHelper is not a constructor error with Durandal and Breeze

我正在使用 Durandal v2.1 和 Breeze v1.5 开发 SPA。这是我第一次使用这些库。我正在手动为 Breeze 创建元数据,因此我正在使用 Metadata-Helper 插件,如 Breeze 文档中所述。为此,我依赖于找到的示例 here。当我 运行 应用程序时,出现以下错误:

错误:无法加载根模块 (viewmodels/shell)。详细信息:breeze.config.MetadataHelper 不是构造函数。

错误似乎出现在我用来通过这行代码创建元数据的 metadataFactory.js 文件中(请参阅 post 中的 metadataFactory.js 文件):

helper = new breeze.config.MetadataHelper(namespace, keyGen);

我看到的所有例子都清楚地表明上面是一个有效的指令,所以我无法理解问题所在。

这是我的main.js:

requirejs.config({
    'paths': {
        'durandal': '../durandal',
        'plugins': '../durandal/plugins',
        'transitions': '../durandal/transitions',
        'text': '../durandal/amd/text',

        'jquery': '../../Scripts/jquery-2.1.3.min',
        'jqueryUI': '../../Scripts/jquery-ui-1.11.2.min',
        'knockout': '../../Scripts/knockout-3.2.0',
        'bootstrap': '../../Scripts/bootstrap.min',
        'breeze': '../../Scripts/breeze.min',
        'breezeMetadataHelper': '../../Scripts/breeze.metadata-helper',
        'Q': '../../Scripts/q.min',

        'toastr': '../../Scripts/toastr.min',
        'moment': '../../Scripts/moment',

        'logger': '../services/logger',
        'hubService': '../services/hubService',
        'datacontext': 'dataservices/datacontext',
        'metadataFactory': 'dataservices/metadataFactory',
        'constants': 'models/constants',
        'menu': '../shared/viewmodels/menu',
        'menuItem': '../shared/viewmodels/menuItem'
    },
    'shim': {
        'bootstrap': {
            'deps': ['jquery']
        },
        'select2': {
            deps: ['jquery', 'style!Content/select2-bootstrap'],
            exports: 'select2'
        },
        'breeze': {
            deps: ['knockout', 'jquery', 'Q']
        },
        'breezeMetadataHelper': {
            deps: ['breeze']
        }
    },
    urlArgs: 'bust=' + (new Date()).getTime()
});


define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'logger'], function (system, app, viewLocator, logger) {

    //>>excludeStart('build', true);
    system.debug(true);
    //>>excludeEnd('build');

    logger.log('Loading main module');

    app.title = 'ERWS';

    app.configurePlugins({
        router: true,
        dialog: true,
        widget: {
            kinds: ['grid'] // durandal-grid widget (https://github.com/tyrsius/durandal-grid)
        }
    });

    app.start().then(function () {
        viewLocator.useConvention();
        app.setRoot('viewmodels/shell', 'entrance');
    });
});

这是我的shell.js文件:

define(function (require) {
    'use strict';

    var logger = require('logger');
    logger.log('Loading shell module');

    var ko = require('knockout');
    var router = require('plugins/router');
    var app = require('durandal/app');
    var constants = require('constants');
    var model = require('models/userInfo');

    var entity = {
        contentBody: ko.observable(''),
        appName: constants.AppName,
        copyrightText: constants.CopyrightText,
        userName: ko.observable(''),
        userImage: ko.observable('')
    };

    var loadData = function () {
        var data = model.entity;

        if (data) {
            entity.userName(data.UserName);
            entity.userImage(data.UserImage);
        } else {
            logger.log('Error loading user info');
        }
    };

    return {
        dashboard: entity,
        activate: function () {
            loadData();
        }
    };
});

这是我用来创建元数据的 metadataFactory.js,基于我找到的示例 here:

/* 
 * Creates BreezeJS Metadata
 *
 * Usage:
 *    // assume you include this service as 'metadataFactory'
 *
 *    // create a new EntityManager
 *    var manager = new breeze.EntityManager("your-service-endpoint");
 *
 *    // get the MetadataStore from the manager and fill it
 *    var store = manager.metadataStore;
 *    metadataFactory.fillMetadataStore(store);
 *   
 */
define(function (require) {
    'use strict';

    var logger = require('logger');
    logger.log('Loading metadataFactory module');

    var breeze = require('breeze');

    var addType, date, dt, bool, helper, id, identity;

    function addCommunityInfo() {
        addType({
            name: 'CommunityInfo',
            defaultResourceName: 'CommunityInfos',
            autoGeneratedKeyType: identity,
            dataProperties: {
                id: { type: id },
                groupsTotal: { max: 3, nullOk: false },
                regionsTotal: { max: 3, nullOk: false },
                landExtensionTotal: { max: 7, nullOk: false },
                parcelsTotal: { max: 4, nullOk: false }
            }
        });
    }

    function addDevicesInfo() {
        addType({
            name: 'DevicesInfo',
            defaultResourceName: 'DevicesInfos',
            autoGeneratedKeyType: identity,
            dataProperties: {
                id: { type: id },
                deviceServers: { max: 3, nullOk: false },
                deviceAdBoards: { max: 3, nullOk: false },
                deviceControllers: { max: 3, nullOk: false },
                deviceTotalUnits: { max: 3, nullOk: false }
            }
        });
    }

    function addGroupInfo() {
        addType({
            name: 'GroupInfo',
            defaultResourceName: 'GroupInfos',
            autoGeneratedKeyType: identity,
            dataProperties: {
                id: { type: id },
                groupName: { max: 50, nullOk: false },
                acronym: { max: 5, nullOk: false },
                dateOfAffiliation: { type: date, nullOk: false },
                inworldDescription: { max: 50, nullOk: false },
                webDescription: { max: 50, nullOk: false },
                yourRoleValue: { max: 50, nullOk: false },
                landOwnedSize: { type: id },
                landOwnedValue: { max: 50, nullOk: false },
                numberOfRentals: { max: 50, nullOk: false },
                presentInRegions: { max: 50, nullOk: false },
                numberOfParcels: { max: 50, nullOk: false },
                numberOfDevices: { max: 50, nullOk: false },
                userRole: { max: 50, nullOk: false },
                inWorldKey: { max: 50, nullOk: false },
                created: { type: date, nullOk: false },
                updated: { type: date, nullOk: true }
            },

            navigationProperties: {
                regions: { type: 'Region', hasMany: true },
                devices: { type: 'Device', hasMany: true },
                rentals: { type: 'Rental', hasMany: true }
            }
        });
    }

    // shorten for brevity....

    // Initialize the metdataFactory with convenience fns and variables 
    function init(metadataStore, serviceName) {

        var store = metadataStore; // the metadataStore that we'll be filling
        store.namingConvention = breeze.NamingConvention.camelCase;

        // namespace of the corresponding classes on the server
        var namespace = 'FieryRed.ErcApplication.Core.ViewModel.Admin';

        // 'Identity' is the default key generation strategy for this app
        var keyGen = breeze.AutoGeneratedKeyType.Identity;

        // Breeze Labs: breeze.metadata-helper.js
        // https://github.com/Breeze/breeze.js.labs/blob/master/breeze.metadata-helper.js
        // The helper reduces data entry by applying common conventions
        // and converting common abbreviations (e.g., 'type' -> 'dataType')
        helper = new breeze.config.MetadataHelper(namespace, keyGen);      <===================== My problem seems to be caused by this line of code
        helper.addDataService(store, serviceName);

        // addType - make it easy to add the type to the store using the helper
        addType = function (type) {
            return helper.addTypeToStore(store, type);
        };

        // DataTypes we'll be using
        dt = breeze.DataType;
        date = dt.DateTime;
        id = dt.Int32;
        bool = dt.Boolean;

        identity = breeze.AutoGeneratedKeyType.Identity;
    }

    /*** Implementation ***/
    function fillMetadataStore(metadataStore, serviceName) {
        init(metadataStore, serviceName);

        addCommunityInfo();
        addDevicesInfo();
        addGroupInfo();
        // etc.
    }

    // The metadata definition service
    return {
        fillMetadataStore: fillMetadataStore
    };
});

我猜你没有为 "breeze.metadataHelper" 调用 require。您可以通过在 metadataFactory.js 中设置断点并查看它是否存在于 breeze.config 中来确认它是否已加载(或未加载)。如果不是,那么你没有加载它。

您可以 require 它明确地位于 metadataFactory.js 的顶部,如果那是您唯一使用它的地方。或者让 metadataFactory.js 依赖于它。尽管我还没有检查过自己,但两者都应该有效。

p.s.: 您可能会发现自己对 require 配置考虑过多。默认值可以 trim 这大大。另外 "breeze.metadataHelper" 已经告诉 require 它取决于 breeze.