How to get a global Object by Object name - node.js

In the meteor server.There is object like this:
A.js
testObject = function(){}
and I want to get testObject by testObject's name "testObject"
if "A.js" in Client . I know I can get the Object by
var a = window["testObject"]
because of window is a global Object and save all other global Object.
but I don't know how to get it in Server.
Any suggestions appreciated!

An easy way to retain a global scope reference is just to wrap your code in an IIFE closure like this:
(function( namespace ) {
console.log( namespace["testObject"] );
}( this ));
This will work on both server and client.

just like node code. use like this
global["testObject"]
I get the answer on FreeNode .Thank you #bline

Related

Electron app: Reference mainWindow object in another module?

I am building an electron app, where the mainWindow object is created following the quick start: http://electron.atom.io/docs/tutorial/quick-start/.
As per this quick start, it is created asynchronously. The problem that I run into, is that for instance when I want to send messages from main to renderer process, I need to reference the mainWindow object. If this happens to be in a module that I require, then I need a means to make this module know of the mainWindow object.
I could of course prepend it with global., but I know that this is very much advised against. So I wish to do it more elegantly.
I came across this post: Asynchronous nodejs module exports; which appears to offer a solution. Taking the main.js file from the quick start (see above link, it's explicitly shown there), it appears I would add to the createWindow function
if( typeof callback === 'function' ){
callback(mainWindow);
}
and export the main.js module as
module.exports = function(cb){
if(typeof mainWindow !== 'undefined'){
cb(mainWindow);
} else {
callback = cb;
}
}
Then, in a higher-level script, I would require as follows:
let main = require('./main.js');
let lib = require('./lib.js'); // Library where I need a mainWindow reference
main(function(window) {
lib.doSomething(window);
});
where lib.js looks like
module.exports.doSomething = function(window) {
// Do something with window object, like sending ipc messages to it
window.webContents.send('hello-from-main', "hi!");
}
Although the simple case in the original post 'Asynchronous nodejs module exports' works fine, I cannot get it to work like described above; running the app it complains Uncaught Exception: TypeError: Cannot read property 'webContents' of null. This is also the case if I directly require lib.js within main()'s callback (which I know is also advised against).
I confess that I do not fully understand the simple case of the post, as I am rather new to node. This prevents me from fixing my own implementation of it, which I agree is blunt copy/pasting which reasonably should be expected to fail. Could somebody help me with how to correct above method, or advise me of a different approach to make it work? Thank you!
I have created the npm package electron-main-window for the same.
Install:
$ npm install electron-main-window
or
$ yarn add electron-main-window
Usage:
// Import ES6 way
import { getMainWindow } from 'electron-main-window';
const mainWindow = getMainWindow();
// Import ES5 way
const mainWindow = require('electron-main-window').getMainWindow();
// e.g:
if(mainWindow !== null ){
mainWindow.webContents.send('mainWindowCommunication', "This is a test message");
}
Whooops! The devil is in the details... I had defined on top of main.js
let mainWindow = null, callback;
which caused the error! Should be
let mainWindow, callback;
then it works perfectly!
P.s. Instead of deleting my post, I opted for keeping it and answering myself for future reference of other people who need asynchronous exporting.

Undefined Variable in JSON Object in Node Module

I can't figure out why I get an undefined here for 'app':
module.exports = {
application: require('../../app').service,
request: require('supertest')(this.application),
startSetup: setup(this.application)
};
it throws up at the (this.application) for the request: line.
Yo can try this:
var app = require('../../app').service;
module.exports = {
application: app,
request: require('supertest')(app),
startSetup: setup(app)
};
The problems is that this.application doesn't exists yet.
You can't use the inside parts of an object that it is not defined (it is defined only after the final }).
Here is an example that you can try on your chrome console.
You can see that you can't use type because it is not defined.
Javascript doesn't know what this.application is. The object hasn't been defined yet so you can't use an attribute inside at object definition that's defined in the same object.

Webstorm don't recognize node.js third party modules

I tried in both Webstorm 6 and 7 EAP,
Auto-completion works fine but something strange happened,
var SyParams = require('../params');
....
SyParams.kioskParams ( IDE gives warning, 'unresolved variable kioskParams' )
If I write 'require' like this;
var SyParams = new require('../params');
Everything looks good, is there a solution for that ?
It seems that the '..\params' module is exporting a constructor function that constructs an object which has kioskParams as an attribute. And the constructor itself doesn't have an attribute called kioskParams.
It can be easier understood if you write it like this:
var SyParams = require('../params'); // The module exports a constructor
...
var syParams = new SyParams(); // You construct the actual object
syParams.kioskParams; //Then you access its members

Configuring $.ajax with backbone on node for testing with vows

(Edited to greatly simplify)
On node I have the following server.js file.
var Backbone = require('backbone');
var Tweet = Backbone.Model.extend({});
var Tweets = Backbone.Collection.extend({
model : Tweet,
url: function () {
return 'http://search.twitter.com/search.json?q=backbone'
}
});
var myTweets = new Tweets();
myTweets.fetch();
When I run this, I get an error that says. "Cannot call method 'ajax' of undefined" (1359:14)
basically that is the result of $ being undefined. Why is it undefined? Well there are a number of intermediate steps but when the file is loaded, it is expecting "this" to be "window" in browser or "global" on server. executed on node "this" = {}.
So the question, "How do I set 'this' to global" inside the backbone.js file?
On Backbone >= 1.x, you can simply assign Backbone.$ rather than using Backbone.setDomLibrary.
Solution for Backbone < 0.9.9
The first issue you need to address is how you are running this on Node anyway. Nodejs is a server-side JS environment, but it does not include any logic for controlling a DOM. For that you need to load something like JSDom.
When you have some DOM environment set up, you can load jQuery and your code into it and it should work just like a browser.
To answer your question specifically though, loading jQuery into the global is a bit of an ugly way to do it. You should use Backbone's setDomLibrary function to set $ to what you want.
Try something like this:
if (typeof exports !== 'undefined') {
MyModels = exports;
Backbone.setDomLibrary(require('jquery'));
server = true;
} else {
MyModels = this.MyModels = {};
}
This will fail if you try to do any DOM functions though.

how to retrieve caller function js file global variable value in nodejs

Sample Code from nodejs:
Code from JS1.js :
var js2=require("../../util");
var dataName="Billy";
function hello1(){
js2.hello2("message");
}
Code from JS3.js :
var js2=require("../../util");
var dataName="Tom";
function hello3(){
js2.hello2("message");
}
Code from JS2.js :
exports.hello2=hello2;
function hello2(arg1){
console.log(arg1);
//Here I need the data in global variable "dataName" of file JS1.js or JS3.js
}
I need to access the global variable of caller js file.
All modules share a global object in node.js. So in JS1.js ...
global.dataName = "Billy";
... then in JS2.js:
console.log(global.dataName);
However, it shouldn't be surprising that using global in this way is generally considered poor form. Unless you have a specific reason for not wanting JS2 to depend on JS1, it's probably better to just have JS2 export dataName as part of it's module.exports.

Resources