2

I have written a web application which uses requirejs for AMD:

require(dependencies, function(dependencies) { 
    (function($) {
        $.fn.plugin = function(options) { return this; };
    })(window.jQuery);
});

I then used almond and r.js to compile it into a single javascript file, using the build config:

({
    baseUrl: ".",
    paths: {
        'jquery' : 'vendor/jquery-1.9.1.min'
        'almond' : "../node_modules/almond/almond"
    },
    name : "almond",
    include : "main",
    out : "plugin.js",
    wrap : true
})

Next, I want to use this plugin in another app, also based on requirejs. Here I have:

require.config({ paths: { 'jquery': 'vendor/jquery-1.9.1.min' } });
require(['jquery','plugin'], function ($) {
    $('#plugin').plugin(options);
});

However, the code loads out of order:

  1. Load jquery and plugin js files.
  2. Execute $('#plugin').plugin(options).
  3. Run (function($) { $.fn.plugin = function(options) { return this; }; })(window.jQuery);

Obviously, (2) is an error since (3) needs to be run first. The question is, why?

UPDATE 1

One proposed solution is to change the first block of code to:

define(dependencies, function(dependencies) { 
    (function($) {
        $.fn.plugin = function(options) { return this; };
    })(window.jQuery);
});

Once compiled, this becomes:

define("main", dependencies, function(dependencies) { 
    (function($) {
        $.fn.plugin = function(options) { return this; };
    })(window.jQuery);
});

Unfortunately, now the code inside this block is NEVER executed.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.