Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

browserify and document ready?

I'm struggling with using Browserify and document ready events. How do I craft a module that exports content only available after the document ready event has fired? How do I depend on such a module?

My first stab was to try to set module.exports asynchronously -- fail out of the box. My nextx stab at this was for the module to return a function that took in a callback, and called the callback when document ready fired. Third attempt returned a promise. This seems to make dependent modules not very modular as now dependent modules and their dependencies (and theirs, turtles all the way down) must also leverage this pattern.

What's a good pattern for using Browserify and document ready events?

like image 943
robrich Avatar asked Jun 12 '14 16:06

robrich


People also ask

What do I need to start using Browserify?

What You’ll Need. To get started with Browserify, the bare minimum you’ll need is: node.js. npm – this comes installed with node by default. Browserify – I’ll explain how to install this one. A pack of JavaScript modules you’re ready to tame!

What is document ready in jQuery?

$( document ).ready() A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

How does Browserify work with dependencies?

We define dependencies and then Browserify bundles it all up into a single neat and tidy JavaScript file. You include your required JavaScript files using require ('./yourfancyJSfile.js') statements and can also import publicly available modules from npm.

Should I use Browserify in my next JavaScript project?

If you haven’t given Browserify a shot, try it in your next JavaScript project and see if it rocks your world. There are a ton of other Browserify resources out there. A few handy bits and pieces you might want to check out:


1 Answers

Try this:

var domready = require("domready");

domready(function () {
    exports.something = /* whatever you want */
});

You'll need to download the domready package:

npm install domready

Then just use browserify:

browserify input.js -o output.js

Yes. It's that simple.


Consider that we have two files: library.js and main.js.

// library.js

var domready = require("domready");

domready(function () {
    exports.test = "Hello World!";
});

// main.js

var library = require("./library");
var domready = require("domready");

domready(function () {
    alert(library.test);
});

As long as you require your library before you register your main domready function you should be able to use your library seamlessly.


Sometimes you may wish to set module.exports to a function. In that case you can use the following hack:

// library.js

var domready = require("domready");

module.exports = function () {
    return exports._call.apply(this, arguments);
};

domready(function () {
    exports._call = function () {
        alert("Hello World!");
    };
});

// main.js

var library = require("./library");
var domready = require("domready");

domready(function () {
    library();
});

Note that the _call property is not in any way special.

like image 151
Aadit M Shah Avatar answered Oct 11 '22 17:10

Aadit M Shah