Three on node.js with ColladaLoarder - node.js

I know now how use JsonLoader in three.js for node.js.
But I have see, in the folder examples (three\examples\js\loaders) of node module an other loader whose is ColladaLoader.
I have try to execute this loader, but he isn't in the core folder of module.
I obtain an error: "ColladaLoader is not a function"
I have try to make a require to this file, but I obtain an error even I make a require to three module : "Three is not defined"
How can I use this ColladaLoader in node.js?
Thank You

var fs = require('fs');
var THREE = require("three");
eval(fs.readFileSync("bower_components/three.js/examples/js/loaders/ColladaLoader.js")+"");
First you load three.js, ensure you set it to a var with the uppercase name "THREE". Then you load the ColladaLoader - feels quite dirty.
A more beautiful solution would encapsule ColladaLoader.js to a node module.

If "ColladaLoader is not a function", then you did not reference to the ColladaLoader.js file.
Make sure to add <script src="js/loaders/ColladaLoader.js"></script> (or where ever your src is located) in your <head> or <body> somewhere before calling the ColladaLoader() function.

Related

How do I use Node.JS modules on my site?

Okay, let me preface this by saying I am a complete noob when it comes to Node.
I have a Node module, Typed.js, that I want to use on my site, but I'm not sure how. I don't think it'll work out the box since Node is a server-side tool, and you import stuff from the Typed.js package.
You need to wrapper your code up like this
(function(exports){
// your code goes here
})(typeof exports === 'undefined'? this['typed']={}: exports);
Then in your client side
<script src="typed.js"></script>
<script>
console.log(typed.your-function());
</script>

The react-select module cannot find react

React-select cannot find React:
TypeError: React is undefined1 react-select.js:826:4
React-select.js is getting react through
var React = (window.React);
It lookes like i have to include React in the html to make this work, but i would like to avoid this. Is there something i am missing?
(using node)
Let me guess, you require dist/react-select.js. This script is used for browser environment. Try to require node environment script, which described in package.json. Or if you use npm install react-select it enough to call require('react-select');
We need some more clarification here, "using node" - are you rendering on node server?
If you are rendering in the client/browser, var React = (window.React); can possibly be called before window.React is available...
I recommend using commonJs pattern ie: var React = require('React');
I hope my answer will solve some of your issues with React. To start off, React is not the most 'noob' frendily 'View'(MVC).
I personally believe that HTML should only handle any Javascript. Let's separate the concerns into two.
|-- index.html
|-- app.js
Now, we have two files with two separate concerns. I do agree that the proper way to 'requiring' <== current way || 'import' new ES6way (AS of Release of Node 6) but for the simplicity sake, let's use CDNs.
Add these script tags
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react-dom.min.js'> </script>
"Keep clam =*= solve bugs"

what does var io = require('../..')(server) do?

I've build the project https://github.com/Automattic/socket.io/tree/master/examples/chat locally and it is working great. However, it would be nice to understand a little more about how a socket application works.
In the main startup script one of the modules that is pulled in with require is
var io = require('../..')(server)
what does require('../..') do?
thanks!
When a path to a directory is given to require, it will implicitly look for an index.js in that directory.
In this case, it's the equivalent of
var socket = require("../../index.js");
var io = socket(server);
In the example provided, they're just using some shorthand and throw away the intermediate value returned by the call to require.
Check out the module.require docs for more info.
Here, in your code
require('../..');
Will add File form the path, which have used SOCKET.IO, as you can see that you have not added Socket.io module.
Also, if no specific path give for file or folder, Module require will try to load index.js or index.node. if no such file exist then it will give error.

How to reference local files in a npm module?

I wrote a simple npm module to precompile my handlebars templates when using django compressor to do post-processing for some client side components and found that I need to ship the npm module with a few js files.
Currently I just assume no one is installing this with the global flag because I've "hard coded" the path to these dependencies in the npm module itself
example layout of my npm module
/
* /bin
* /lib/main.js
* /vendor/ember.js
Now inside main.js I want to use the ember.js file ... currently my hard coded approach looks like this
var emberjs = fs.readFileSync('node_modules/django-ember-precompile/vendor/ember.js', 'utf8');
Again -this only works because I assume you install it local but I'd like to think node.js has a more legit way to get locally embedded files
Anyone know how I can improve this to be more "global" friendly?
What you can do is get the directory of the current file and make your file paths relative to that.
var path = require('path')
, fs = require('fs');
var vendor = path.join(path.dirname(fs.realpathSync(__filename)), '../vendor');
var emberjs = fs.readFileSync(vendor + '/ember.js', 'utf8');
Hope that helps!
One of the great strengths of Node.js is how quickly you can get up and running. The downside to this approach is that you are forced to fit the design patterns it was build around.
This is an example where your approach differs too much from Nodes approach.
Node expects everything in a module to be exposed from the modules exports, including templates.
Move the readFileSync into the django-ember-precompile module, then expose the returned value via a module export in lib/main.js.
Example:
package.json
{
"name": "django-ember-precompile",
"main": "lib/main.js"
}
lib/main.js
module.exports.ember = readFileSync('vendor/ember.js')
vendor/ember.js
You obtain your template via
var template = require('django-ember-precompile').ember
This example can be refined, but the core idea is the same.

Including a .js file in node.js

I am trying to include this script in my app: http://www.movable-type.co.uk/scripts/latlong.html
I have saved it in a file called lib/latlon.js, and I am trying to include it like this:
require('./lib/latlon.js');
How should I go about including a JS library like this?
First of all, you should take a look at the Modules documentation for node.js:
http://nodejs.org/docs/v0.5.5/api/modules.html
The script you're trying to include is not a node.js module, so you should make a few changes to it. As there is no shared global scope between the modules in node.js you need to add all the methods you want to access to the exports object. If you add this line to your latlon.js file:
exports.LatLon = LatLon;
...you should be able to access the LatLon function like this:
var LatLonModule = require('./lib/latlon.js');
var latlongObj = new LatLonModule.LatLon(lat, lon, rad);

Resources