我有一个页面,我需要从中获取 id 并将它们传递给 require.js 模块。
所以页面的链接是这样的:www.website.com/SomeController/SomeAction/Id
此页面当前调用 require.js,如下所示:
<script data-main="../Scripts/app/administrator/app.index" src="../Scripts/lib/require.js"></script>
其中 app.index.js 具有以下代码:
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 具有以下代码:
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
};
});
我感到困惑的是,如果我想向这个模块传递一个参数,我该怎么做?
参数的来源可能来自:
- 锚:
<a href="/administrator/user/3>Bob</a>
- 服务器端:返回视图(3)
无论哪种方式,它是如何在 require.js 中完成的?