简体   繁体   English

在木偶项目中包含javascript文件

[英]include javascript files in marionette project

I have a marionette project, and I have my main js file which looks like: 我有一个木偶项目,并且我的主要js文件如下所示:

define(["marionette", "handlebars"], function(Marionette, Handlebars){
    ...
});

Now I want to make a structure of my big app. 现在,我要构建我的大应用程序的结构。 So I would like to separate the content of this function to different files. 因此,我想将此功能的内容分为不同的文件。 So how can I do something like: 所以我该怎么做:

define(["marionette", "handlebars"], function(Marionette, Handlebars){
    include routes.js
    include menu/model.js
    include menu/views.js
    ...
});

I think require.js can help me, but I don't know how. 我认为require.js可以帮助我,但我不知道如何。 Any ideas? 有任何想法吗?

To require a file in the AMD structure (like the one you describe), you can give the path to the file in the array and get the exported value as argument of the callback like this. 要在AMD结构中需要一个文件(如您所描述的那样),您可以在数组中提供文件的路径,并以如下方式获取导出的值作为回调的参数。

define(
  [
    "marionette",
    "handlebars",
    "routes",
    "menu/model",
    "menu/views"
  ],
  function(Marionette, Handlebars, Routes, Model, MenuView){
    // ...
});

Since this is a very basic thing in AMD, you should really read the documentation of Require.JS ( here ) and explanation about AMD modules (like this one ). 由于这是AMD中非常基本的内容,因此您应该真正阅读Require.JS文档( 此处 )和有关AMD模块的说明(如文档)。 You will find lot of information about the AMD structure, etc 您会发现许多有关AMD结构的信息,等等

If you prefer, browserify lets you to load front-end javascript modules using the require syntax. 如果愿意,可以使用browserify来使用require语法加载前端javascript模块。 It looks like this. 看起来像这样。

// library includes
var Marionette = require('backbone.marionette');
var handlebars = require('handlebars');

// custom includes
var routes = require('./routes.js');
var models = require('./menu/models.js');
var views = require('./menu/views.js');


var App = new Marionette.Application();

It includes modules recursively and there is less code. 它包括递归模块,并且代码更少。 It implements the Common.js format so it looks and feels the same as node.js. 它实现Common.js格式,因此其外观和感觉与node.js相同。

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

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