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

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

本文关键字:Breeze 构造函数 错误 Durandal config MetadataHelper 不是 breeze      更新时间:2023-09-26

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

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

错误似乎发生在我用来用这行代码创建元数据的元数据工厂.js文件中(请参阅帖子后面的元数据工厂.js文件):

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

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

这是我的主要.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');
    });
});

这是我的外壳.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();
        }
    };
});

这是元数据工厂.js我使用它来创建元数据,基于我在这里找到的示例:

/* 
 * 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。您可以通过在元数据工厂中设置断点并查看它是否存在于breeze.config 来确认它已加载(或未加载).js并查看它是否存在于 上。如果不是,则您没有加载它。

您可以在元数据工厂的顶部显式require它.js如果这是您使用它的唯一位置。或者使元数据工厂.js依赖于它。尽管我没有检查自己,但两者都应该有效。

PS:你可能会发现你过度考虑你的require配置。默认值可能会大幅减少这一点。此外,"breeze.metadataHelper"已经告诉require这取决于breeze