How to link related js files (asp.net mvc4) in Require.js?

I am studying the John Papa Multiple Selection Course at the SPA.

In his main.js he gave a name to every js library that is included in the kit.

 (function () { var root = this; define3rdPartyModules(); function define3rdPartyModules() { // These are already loaded via bundles. // We define them and put them in the root object. define('jquery', [], function () { return root.jQuery; }); define('ko', [], function () { return root.ko; }); define('amplify', [], function () { return root.amplify; }); define('infuser', [], function () { return root.infuser; }); define('moment', [], function () { return root.moment; }); define('sammy', [], function () { return root.Sammy; }); define('toastr', [], function () { return root.toastr; }); define('underscore', [], function () { return root._; }); } })(); 

But what is root here?


Thus, we can name these short names in the define statement:

 define('vm.session', ['ko', 'datacontext', 'config', 'router', 'messenger', 'sort'], function (ko, datacontext, config, router, messenger, sort) { 

Current, I do not know how to do this. So my working define statement is ugly:

 define('vm.admin.outfitters', ['/Scripts/lib/jquery-1.8.1.js', '/Scripts/lib/jsrender.js', ...], function(){... 

I know it's better to be better. All these js files are already included in the script package. How can I reference these scripts?

+4
source share
1 answer

RE: root

RequireJS and AMD's ready-made libraries remove objects from the global scope (e.g. ko). Some plugins want them to be in the global scope, so we can either customize these plugins or return objects back to the global scope. The latter is what happens in this code. This is done for plugins for knockout in the first place.

RE: your definition instructions

The first parameter is the name of the module, so you're fine. The second parameter is a list of modules that RequireJS knows about. The third parameter is a comparable variable to represent it. So in your code you might have something like this ...

 define('vm.admin.outfitters', ['jquery', 'jsrender'], function($, jsrender) { 
+4
source

Source: https://habr.com/ru/post/1434072/


All Articles