0

我正在使用 Almond 和 r.js 优化器将我的所有脚本组合到一个文件中,用于基于 Backbone.js 的单页应用程序。

我的构建文件:

({
    name: '../bower_components/almond/almond',
    include: ['main'],
    insertRequire: ['main'],
    baseUrl: 'src',
    optimize: 'uglify2',
    mainConfigFile: 'src/main.js',
    out: 'javascripts/scripts.js',
    preserveLicenseComments: false,
    paths: {
        jquery: '../bower_components/jquery/dist/jquery',
        underscore: '../bower_components/underscore/underscore',
        backbone: '../bower_components/backbone/backbone',
        kineticjs: '../bower_components/kineticjs/kineticjs',
        'jquery-ui': '../bower_components/jquery-ui/ui',
        'jquery.layout': '../bower_components/jquery.layout/dist/jquery.layout-latest'
    },
    shim: {
        'jquery.layout': {
            deps: [
                'jquery-ui/core',
                'jquery-ui/draggable',
                'jquery-ui/effect',
                'jquery-ui/effect-slide'
            ],
            exports: 'jQuery.fn.layout'
        }
    }
})

在我的 Backbone 视图中,我初始化了 jquery-layout 插件:

define(['backbone', 'underscore', 'jquery.layout'], function(Backbone, _) {
	'use strict'

    return Backbone.View.extend({
        el: $('#application'),

        initialize: function() {
            this.$el.layout({
              west: {
                size: '50%',
                closable: false,
                slidable: false,
                resizable: true
              }
            });
        }
    });
});

窗格正确显示,但是我无法调整其中任何一个的大小。我发现$.fn.draggable(JQuery UI Draggable 小部件)在声明 jquery-layout 插件时没有被初始化。

这表明可能没有加载小部件。所以我检查了合并的源文件,注意到 JQuery UI Draggable 小部件代码出现在 jquery-layout 插件代码之前,这意味着依赖项以正确的顺序插入。在 Firebug 控制台中打印$.fn.draggable还显示该小部件可用,至少在文档准备好之后是这样。

jQuery、下划线、主干、jQuery UI 都是 AMD 模块。jquery-layout 没有 AMD 兼容性,因此使用了 shim 配置。这取决于 JQuery UI 的 ui.core 和 ui.draggable每个他们的文档

为了让所有这些听起来不太抽象,这里有一个 jquery-layout 插件的演示。

我已经“浪费”了 8 多个小时试图解决这个问题,但任何关于如何解决这个问题的帮助都可以节省我的时间。这很可能只涉及构建文件中我的 shim 配置的一些修复。

4

2 回答 2

1

我通过使非 AMD 感知 jquery-layout 插件与 AMD 兼容来解决了这个问题。我onBuildWrite在构建文件中使用了 r.js 优化器的钩子。这样,ui.draggable在加载非 AMD 感知插件的代码之前初始化 jQuery UI 依赖项:

({
	name: '../bower_components/almond/almond',
        include: ['main'],
        insertRequire: ['main'],
	baseUrl: 'src',
	optimize: 'uglify2',
	mainConfigFile: 'src/main.js',
	out: 'javascripts/scripts.js',
	preserveLicenseComments: false,
	paths: {
		jquery: '../bower_components/jquery/dist/jquery',
		underscore: '../bower_components/underscore/underscore',
		backbone: '../bower_components/backbone/backbone',
		kineticjs: '../bower_components/kineticjs/kineticjs',
		'jquery-ui': '../bower_components/jquery-ui/ui',
		'jquery.layout': '../bower_components/jquery.layout/dist/jquery.layout-latest'
	},
	shim: {
		'jquery.layout': {
			deps: ['jquery', 'jquery-ui/draggable', 'jquery-ui/effect', 'jquery-ui/effect-slide'],
			exports: 'jQuery.fn.layout'
		}
	},
	onBuildWrite: function(moduleName, path, contents) {
		if (moduleName == 'jquery.layout') {
			contents = "define('jquery.layout', ['jquery', 'jquery-ui/draggable', 'jquery-ui/effect', 'jquery-ui/effect-slide'], function(jQuery) {" + contents + "});";
		}
		return contents;
	}
})

于 2015-03-19T11:57:31.810 回答
0

遵循@Genti 的回答并使用最新的 jquery-ui,以下 shim 为我工作。

  shim: {
    'jquery.layout': {
      deps: ['jquery', 'jquery-ui/core', 'jquery-ui/widgets/draggable', 'jquery-ui/effect', 'jquery-ui/effects/effect-slide', 'jquery-ui/effects/effect-drop', 'jquery-ui/effects/effect-scale'],
      exports: 'jQuery.fn.layout'
    }
  }
于 2016-09-29T07:03:26.360 回答