My Meteor project has grown to about 800 lines of code and it's starting to become unwieldy. Today, I decided to clean things up by modularizing the code, but now I'm encountering some errors that I need help resolving.
Here's a specific example:
I'm using d3 to create a force layout in my project. In one of my files (/client/views/force/forceLayout.js), I have defined the following variable:
var force = d3.layout.force()
I've separated the controls for this force layout into their own .html and .js files. Here is an excerpt from one of those files (/client/views/force/controls/sliders/charge.js):
initChargeSlider = function() {
d3.select("#charge-slider")
.call(d3.slider()
.min(-301)
.max(-1)
.step(5)
.value(Session.get("charge"))
.on("slide", function(evt, value) {
Session.set("charge", value);
// force.stop();
force = force.charge(value);
force.start();
}));
}
Template.charge.rendered = function() {
initChargeSlider();
};
The issue arises when loading these files due to Meteor's directory loading order. Specifically, I get an error at force = force.charge(value)
because forceLayout.js
hasn't instantiated force
yet.
I would like to know the best approach to handling this situation. Rearranging files and changing loading orders would undo the modularization work I've done so far. I'm considering using a singleton, object, or monad, but I'm unsure of which option would be most suitable and why. Any guidance on resolving these errors would be greatly appreciated.
Thank you,
Chet