Search code examples
javascriptbackbone.jsunderscore.jsrequirejsjs-amd

How a plugin AMD-application can communicate with a Host non-AMD application?


I have a Host Javascript Application, which not use AMD, BackboneJS or RequireJS but use jQuery, and i have a small application with requireJS + BackboneJS, which should be a plugin of the Host.

Now the plugin AMD-Application :

  1. should become some data from the Host application
  2. should be started from the Host application(into a jquery Dialog)

My problem is that i dont have any globals variables(functions) in the plugin AMD-application, so my big non-AMD App can't see it this way.

How can i set the communication between this both, so that the non-AMD App can see and pass data to the plugin AMD App ?

Entry point my plugin AMD App :

<script data-main="app/js/main" src="app/js/Libs/require/require.js"></script>

main.js

require.config({
  paths: {
    jquery: 'http://code.jquery.com/jquery-1.7.1.min',
    jqueryui: 'http://code.jquery.com/ui/1.8.17/jquery-ui',
    underscore: 'Libs/underscore/underscore-min',
    backbone: 'Libs/backbone/backbone-min',

    //Require Plugins
    text: 'Libs/require/text',
    use: 'Libs/require/use',
    domReady: 'Libs/require/domReady'
  },
  use: {
    backbone: {
      deps: ["use!underscore", "jquery"],
      attach: function() {
        return Backbone;
      }
    },    
    underscore: {
      attach: "_"
    }
  }
});


require([
    'jquery',
    'use!backbone',
    'app'
    ], function(App){

    });

app.js

define([
 'jquery',
 'router'
 ], function($, Router){
    var initialize = function(){
     // Pass in our Router module and call it's initialize function
     Router.initialize();
    }
    return {
      initialize: initialize
    };
 });

Solution

  • Not sure I completely understand, but to expose a global from your requirejs code:

    require([
      'jquery',
      'use!backbone',
      'app'
    ], function(App){
      window.App = App;
    });
    

    Now window.App is visible to all the code.