使用 requireJS 加载主干插件

loading backbone plugins with requirejs

本文关键字:插件 加载 requireJS 使用      更新时间:2023-09-26

我想知道如何加载带有require的骨干插件.js我目前的主要内容是这个.js

(function() {
    'use strict';
    require.config({
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        },
        deepModel: {
            deps: ['underscore', 'backbone']
        }
    },
    paths: {
        jquery: 'lib/jquery/jquery',
        underscore: 'lib/underscore/underscore',
        backbone: 'lib/backbone/backbone',
        text: 'lib/requirejs-text/text',
        deepModel: 'lib/deep-model/deep-model.min'
    },

在我的模型中,我有这样的东西

var myapp = myapp|| {};
(function() {
    'use strict';
    define([
    'jquery',
    'underscore',
    'backbone',
    'deepModel',
    ], function($, _, Backbone) {
        myapp.model= new Backbone.DeepModel.extend({
            defaults: {
            },
            urlRoot: '/users',

由于某种原因,上述内容似乎没有按预期工作。我想我错过了一些东西,但不确定那是什么。我正在使用骨干深度模型插件。

这是我在调试器中遇到的错误

未捕获的类型错误:对象 [对象对象] 没有方法"应用"

在函数签名中将 DeepModel 添加到您的作用域:

define([
'jquery',
'underscore',
'backbone',
'deepModel',
], function($, _, Backbone, **DeepModel**) 

如果您使用与AMD兼容的骨干和下划线版本,可能会使您的生活更轻松。默认情况下,它们不支持 AMD。

https://github.com/amdjs/backbone

https://github.com/amdjs/underscore