Passing Parameters to Require.js module

2019-04-13 01:18发布

问题:

I have a page that I need grab the id from and pass them to the require.js module.

So the link to page is something like this: www.website.com/SomeController/SomeAction/Id

This page currently calls the require.js like this:

<script data-main="../Scripts/app/administrator/app.index" src="../Scripts/lib/require.js"></script>

Where app.index.js has the following code:

requirejs.config({
    "baseUrl": "../Scripts/app/administrator",
    "paths": {
        "app.index": "app.index",
        "ko": "../../lib/knockout-2.2.1",
        'knockout.bindings': "../../lib/knockout.bindings",
        "jquery": "//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min",
        "toastr": "../../lib/toastr",
        "model.navigation" : "../models/model.navigation"
}
});

// Load the main app module to start the app
require(["main.index"], function (bs) { bs.run(); });

and the main.index.js has the following code:

define(['ko', 'indexViewModel', 'model.navigation'], function (ko, indexViewModel, Navigation) {
    var
        run = function () {
            var vm = new indexViewModel();

            var array = new Array();
            $.getJSON("/api/navigations/getmynavigation/", function (data) {
                $.each(data, function (key, val) {
                    var n = new Navigation();
                    n.navigationId(val.NavigationId);
                    n.name(val.Name);
                    array.push(n);
                });
            }).done(function(){
                vm.navigations(array);
            });

            ko.applyBindings(vm, document.getElementById('#administrator-home-view'));
        };
    return {
        run: run
    };
});

What I am confused about is that if I want to pass an parameter to this module, how do I do that?

The parameter's source could come from:

  1. Anchor: <a href="/administrator/user/3>Bob</a>
  2. Server Side: return View(3)

Either way how is it done in require.js?

回答1:

I would allow your module to accept a parameter and then pass it along from within the require callback. Something like:

module.js

define(['depA', 'depB'], function(depA, depB) {
    return {
        foo: function(param) {
            // Do something with param
        }
    }
});

main.js

require(['module'], function(module) {
    module.foo('myparam')
});

You could also grab it from a link if you liked, by attaching events in the require:

require(['module'], function(module) {
    document.querySelector('.link').addEventListener(function(event) {
        module.foo(this.href);
        event.preventDefault();
    });
});


回答2:

If you need an initialization phase more like a constructor, you can do the following:

engine.js

define('engine', [], function() {
    return function(param) {
        return {
            getParam: function() { return param; }
        }
    }
};

home.js

require('engine', function(engine) {
    var eng = engine('myparam')
    console.log(eng.getParam()); // should return 'myparam'
});

The difference here is that 'engine' returns a function, not an object, so you have to remember to call 'eng' as a function with the parameter.