RequireJS问题公用的具有依赖性2个模块之间的方法和可变

问题描述 投票:0回答:1

在需要JS在asp.net MVC我试图访问另一个模块使用的变量和方法。怎么做 ?提供如下我的榜样

  1. ParentPageA

下面JS对于ParentPageA

要求([ 'jquery的', '引导', '的jqGrid'],函数($){

 $(function () {

 var messageToAlert = 'Sample Message'; //Message Could be dynamic
    function DOWork(){
        alert('hi'); //Or other code using Jquery table
        alert(messageToAlert);
        messageToAlert = 'Change message ';
    }

 });


}
  1. PartialViewA在ParentPageA require([ 'jquery', 'bootstrap', 'jqGrid', 'ParentPageA'], function ($,ParentPageA){ //ParentPageA - is Undefined $(function () { //How to Access messageToAlert variable ? //How to Access DOWork() //Need to access back the changed MessageToAlert variable as well }); }

定义是唯一的选择?如果我使用定义 - 将它仍然能够加载内定义其他相关的模块?由于我的功能和变数取决于其他库能够运行(如funcions其中的Jquery,日历等)。

javascript asp.net-mvc requirejs amd
1个回答
0
投票

如下修正

define('MYFUNCMOD', ['https://code.jquery.com/jquery-3.3.1.min.js'] ,function(jQuery) {
  // Window settings 
  var Message = "Initial Message";
  function change() {
    this.Message = "Changed Triggered";
  }
  
  function docReadyDisp()
  {
  $(document).ready(function(){
  
  	alert('docReadyDisp Doc Ready');
  
  });
  }

  return {
    Message: Message,
    change:change,
    docReadyDisp : docReadyDisp
  };
		
});


// (is run on dom load)
require(['https://code.jquery.com/jquery-3.3.1.min.js','MYFUNCMOD'], function(jQuery,system) {
  $(document).ready(function() {
    alert(system.Message);
    system.change();
     alert(system.Message);
     system.docReadyDisp();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.