0
votes

In require JS in asp.net MVC i am trying to access the variable and method used in another module. How to do it ? Provided my example as below

  1. ParentPageA

Below JS For ParentPageA

require([ 'jquery', 'bootstrap', 'jqGrid'], function ($){

 $(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 In 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
     });
    
    
    }
    

Is define the only option ? if i use Define - will it still be able to load other dependent modules inside the define ? Since my functions & variables depend on other libraries to be able to run (like funcions which Jquery, Calendar etc...).

1

1 Answers

0
votes

Fixed as below

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>