[英]How can I import a node module into my backbone.js app?
I'm trying to wrap my head around node.js modules, require, and how to use modules in a backbone.js app. 我正在尝试围绕node.js模块,需求以及如何在骨干.js应用程序中使用模块展开讨论。
Right now I am using an express server which starts off with the code: 现在,我正在使用一个以代码开头的快递服务器:
var express = require('express'),
http = require('http'),
config = require("./config"),
oauth = require("oauth"),
bcrypt = require("bcrypt"),
sqlite = require("sqlite3"),
_ = require("underscore"),
exec = require('child_process').exec,
app = express(),
server = http.createServer(app).listen( process.env.PORT || config.port);
and then I have a router.js file that defines ALL views and models. 然后我有一个定义所有视图和模型的router.js文件。
config.js file that looks like this: 如下所示的config.js文件:
require.config({
baseUrl: '/',
paths: {
'util' : 'assets/lib/util',
'jquery' : 'assets/lib/jquery',
'underscore' : 'assets/lib/underscore',
'backbone' : 'assets/lib/backbone'
},
shim: {
'underscore' : { exports : '_' },
'backbone' : { deps : ['underscore', 'jquery'], exports : 'Backbone' },
'bootstrap' : { deps : ['jquery'], exports : 'Bootstrap' },
'parsley' : { deps: ['jquery'] },
'socket' : { deps : ['underscore', 'jquery'], exports : 'socket' }
}
});
require(['main']);
My router.js file starts off like this (I took out a bunch of code) 我的router.js文件像这样开始(我拿出了一堆代码)
define(['app','jquery','underscore','backbone',], function(app, $, _, Backbone){
var WebRouter = Backbone.Router.extend({
routes: {
"": "index"
},
initialize: function(options) {
}
return WebRouter;
});
and I'll just throw in my main.js file for good measure 我会把我的main.js文件丢进去
require([
"app",
"router",
"models/SettingsModel"
],
function(app, WebRouter, SettingsModel) {
// Just use GET and POST to support all browsers
Backbone.emulateHTTP = true;
app.router = new WebRouter();
Backbone.history.start({ pushState: true, root: app.root });
}); });
Alright now here comes the tricky part. 好了,现在到了棘手的部分。 I want to use a node module, specifically the twitter-js-client module found at https://github.com/BoyCook/TwitterJSClient
我想使用一个节点模块,特别是在https://github.com/BoyCook/TwitterJSClient中找到的twitter-js-client模块
so in my TwitterView.js, I can run the following code: 因此,在我的TwitterView.js中,我可以运行以下代码:
var twitter = new Twitter(config);
steps I've tried 我尝试过的步骤
I am somewhat new to Node/backbone, and I'm looking for clarification of how Node,Require and Backbone all work with eachother. 我对Node / backbone有点陌生,我正在寻找有关Node,Require和Backbone如何相互配合的说明。 If anyone has any tips or constructive criticism on how to structure this app, I would greatly appreciate it.
如果有人对如何构建此应用程序有任何建议或建设性的批评,我将不胜感激。 And if there are any solutions to loading the NPM module within a backbone view, I would be very grateful.
如果有在骨干网视图中加载NPM模块的解决方案,我将不胜感激。 Do I need a package manager like bower?
我需要像bower这样的软件包管理器吗? I have no problems with Jquery, and underscore, but I can't seem to load this npm module.
我对Jquery和下划线没有任何问题,但似乎无法加载此npm模块。
Thanks! 谢谢!
You can use browserify . 您可以使用browserify 。
Write you client side scripts with require statements. 用require语句编写客户端脚本。
var Twitter = require('twitter').Twitter;
run browserify against your scripts to generate a single bundle.js file 针对脚本运行browserify以生成单个bundle.js文件
include the resulting file on your page. 在页面上添加结果文件。
<script src="bundle.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.