问题描述
我有一个网页,我需要抓住从ID,并将其传递给require.js模块。
I have a page that I need grab the id from and pass them to the require.js module.
于是链接到页面是这样的: www.website.com/SomeController/SomeAction/Id
So the link to page is something like this: www.website.com/SomeController/SomeAction/Id
该网页目前调用require.js是这样的:
This page currently calls the require.js like this:
<script data-main="../Scripts/app/administrator/app.index" src="../Scripts/lib/require.js"></script>
在哪里app.index.js具有以下code:
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(); });
和main.index.js具有以下code:
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?
参数的来源可能来自:
- 主持人:
&LT; A HREF =/管理员/用户/ 3 GT;鲍勃LT; / A&GT;
- 服务器端:返回查看(3)
无论哪种方式,它是如何在require.js做?
Either way how is it done in require.js?
推荐答案
我可以让你的模块接受一个参数,然后从要求
回调中传递下去吧。是这样的:
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();
});
});
这篇关于将参数传递给Require.js模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!