简体   繁体   English

在Backbone中将NPM模块与RequireJS一起使用

[英]Using NPM module in Backbone with RequireJS

Hejsa, I'm writing a webapp, which consists of a Node backend (Express server), which serves a Backbone app to the clients. Hejsa,我正在编写一个Web应用程序,其中包含一个Node后端(Express服务器),该后端为客户端提供Backbone应用程序。 The Backbone app uses RequireJS to load the modules used. Backbone应用程序使用RequireJS加载使用的模块。 I would like to use Ag-grid clientside, which can be included as an NPM module. 我想使用Ag-grid客户端,它可以作为NPM模块包含在内。 https://www.ag-grid.com/javascript-grid-getting-started/index.php https://www.ag-grid.com/javascript-grid-getting-started/index.php

How can I reference this NPM module from Backbone? 如何从Backbone引用此NPM模块?

Project structure 项目结构

./node_modules ./src/package.json ./src/app (Node backend + Express server) ./src/public ./src/public/main.coffee (contains requireJs config) ./src/public/scripts (Backbone views, models, etc)

main.coffee 主咖啡

require.config
    baseUrl: '../scripts/'
    paths:
        jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min'
        jqueryui: '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min'
        underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min'
        ...

I would like to include the ag-grid NPM module here, but without having to reference the very top ./node_modules folder as ../../../node_modules/ag-grid/dist/ag-grid ( didn't count the levels.. ). 我想在此处包含ag-grid NPM模块,但不必将../../../node_modules/ag-grid/dist/ag-grid引用到最顶层的./node_modules文件夹( 没有计算水平.. )。 Also, I'd like if possible to avoid a second package.js, and a secondary npm install 另外,如果可能的话,我想避免第二个package.js和第二个npm install

Any help related specifically to this project structure? 与该项目结构相关的任何帮助吗? Secondarily, is there any better way to structure such a project? 其次,有没有更好的方法来组织这样的项目? (Node backend serving a Backbone webapp) (服务于Backbone Webapp的节点后端)

Thanks 谢谢

You guessed it and using a relative path all the way to the node_modules directory is the way to go. 您猜对了,一直使用相对路径到node_modules目录。

requirejs.config({
    paths: {
        "ag-grid": "../../../node_modules/ag-grid/dist/ag-grid",
        "backbone": "../../../node_modules/backbone/backbone"
    }
});

define(["backbone", "ag-grid"], function(Backbone, agGrid) {
    // whatever
});

You could also use npm for all the dependencies and bundle an optimized version of your app using the RequireJS optimizer ( r.js ). 您还可以将npm用于所有依赖项,并使用RequireJS优化器r.js )捆绑应用程序的优化版本。

Personally, I use npm for the development of the project and for server-side (node) dependencies. 就个人而言,我将npm用于项目开发和服务器端(节点)依赖性。 For my Backbone app, I use Bower as it's specialized in front-end dependencies management. 对于我的Backbone应用程序,我使用Bower,因为它专门用于前端依赖项管理。

I have a .bowerrc file that tells bower where to install the dependencies: 我有一个.bowerrc文件,它告诉bower在哪里安装依赖项:

{
    "directory": "src/lib",
}

And a Gulp task which calls bower install : 还有一个名为bower install的Gulp任务:

var bower = require("bower"),
    $ = require('gulp-load-plugins')({ lazy: true }),
    gulp = require("gulp");

gulp.task('bower', function() {
    return bower.commands.install()
        .on('log', function(data) {
            $.util.log('bower', $.util.colors.cyan(data.id), data.message);
        });
});

this task is called automatically after npm install with a npm hook: 使用npm挂钩在npm install后自动调用此任务:

"scripts": {
  // ...
  "postinstall": "gulp install"
}

Take a look at simplified-js-project , a sample project which shows my development tools around a Backbone and RequireJs project. 看一看简化的js项目 ,这是一个示例项目,展示了我的Backbone和RequireJs项目开发工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM