[英]Meteor - How to call an external class methods from other JS files?
I'm new to meteor. 我是新来的流星。 I've been trying to write my meteor code the "object-oriented way" so I created an object called Message for my Parser and called it's methods from my app.js.
我一直试图以“面向对象的方式”编写流星代码,所以我为Parser创建了一个名为Message的对象,并从我的app.js中调用了它的方法。
I would like to instantiate the object and call the methods defined in my Message.JS from my app.js? 我想实例化对象并从app.js调用Message.JS中定义的方法吗?
App 应用
|--app.html | --app.html
|--app.js | --app.js
|--app.css | --app.css
|--message.js | --message.js
|--.meteor | - 流星
message.js message.js
var Message = {
init: function( message ){
/* Initialization function with some properties */
}
getHash: function() {
return this.hash_table;
}
parseMessage: function(input) {
/* Some Parsing logic */
return(hash_table);
}
}
app.js app.js
if (Meteor.isServer) {
var msg = new Message.init(response);
var hash = msg.getHash();
console.log(hash);
}
Variables declared with var have a file scope in Meteor, and cannot be accessed from within other files. 用var声明的变量在Meteor中具有文件作用域,并且不能从其他文件中访问。 If you declare the variable without var, it will have a bigger scope and can be seen from within your app,
如果您声明不带var的变量,则它将具有更大的作用域,并且可以在您的应用中看到,
Message = {
...
}
If that doesn't work, you might have a problem with the load order. 如果这不起作用,则可能是装载顺序有问题。 The code in message.js should be loaded before the code in app.js gets called.
应该先调用message.js中的代码,然后再调用app.js中的代码。 To make sure it is, put message.js in the /lib folder.
为确保正确,将message.js放在/ lib文件夹中。 Files in this folder are always loaded before everything else.
此文件夹中的文件始终先加载。
Add message.js into a folder named lib so it gets loaded first when meteor starts see http://docs.meteor.com/#/full/examplefilestructure . 将message.js添加到名为lib的文件夹中,以便在流星开始时首先加载它,请参阅http://docs.meteor.com/#/full/examplefilestructure 。 Then just remove var from Message so it becomes global.
然后从Message中删除var,使其变为全局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.