如何使用 RequireJS 在我的模块化主干.js 应用程序中包含 jQueryUI? [英] How can I include jQueryUI in my modular backbone.js app using RequireJS?

查看:22
本文介绍了如何使用 RequireJS 在我的模块化主干.js 应用程序中包含 jQueryUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 RequireJS 在我的 Backbone.js 应用程序中包含 jQueryUI.我的index.html中包含的main.js文件如下:

I want to include jQueryUI in my backbone.js app using RequireJS. The main.js file included in my index.html is as follows:

require.config({
    paths: {
            jquery: 'libs/jquery/jquery-1.7.2.min',
            jqueryui: 'libs/jquery/jquery-ui-1.8.18.custom.min',
            underscore: 'libs/underscore/underscore-min',
            backbone: 'libs/backbone/backbone-optamd3-min',
            text: 'libs/require/text',
            templates: 'templates'
       }

});

require(['app'], function(App){
    App.start();
});

对于每个模型/视图/路由器文件,我只在define"块的开头包含jquery"命名空间,如下所示:

And for each model/view/router file, I just include the 'jquery' namespace at the start of the "define" block as follows:

define([
    'jquery',
    'underscore',
    'backbone',
    'views/categoryview',
    'text!templates/category.html'
    ], function($, _, Backbone, CategoryView, categoryTemplate){
        // Here comes my code
});

但是在这些文件中无法使用 jQueryUI.我的代码有问题吗?或者我还应该在每个定义"块中包含jqueryui"吗?但是如果我在define"块中包含jqueryui",我应该如何在函数中命名它以避免与jquery的名称冲突?

But the jQueryUI could not be used in these files. Is there something wrong with my code? Or should I also include the "jqueryui" in each "define" block? But if I include "jqueryui" in the "define" block, How should I name it in the function to avoid name conflict with jquery?

推荐答案

您可以在主文件中包含所有仅修补其他对象(如 jQuery UI)的文件,如下所示(您需要确保 jQuery 在 jQuery 之前加载用户界面):

You can include all files that only patch some other object (like jQuery UI) in your main file as follows (you need to make sure jQuery is loaded before jQuery UI):

require(['jquery', 'app', 'jqueryui'], function ($, App) { App.start(); });

另一种方法是在您已经提到的每个模块中包含 jQuery UI.

Another approach is to include jQuery UI in every module as you already mentioned.

我个人更喜欢第一种方法,即使它隐藏了依赖关系.

I personally prefer first approach even though it's hiding dependencies.

这篇关于如何使用 RequireJS 在我的模块化主干.js 应用程序中包含 jQueryUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆