Trouble linking form2js file to project with require.js

232 views Asked by At

I want to use form2js to convert a form's info to json to post. The problem is, the framework I am using uses require.js and it seems like I am not linking the form2js file with the requirejs config properly. I am getting an

Uncaught ReferenceError: form2js is not defined

error.

form2js is a function within the form2js.js file.

Here is the config file:

 require.config({
  baseUrl: "assets/js/lib/"
  , shim: {
    'backbone': {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    'underscore': {
      exports: '_'
    },
    'bootstrap': {
      deps: ['jquery'],
      exports: '$.fn.popover'
    },
    'form2js': {
      exports: 'form2js'
    }
  }
  , paths: {
    app         : ".."
    , collections : "../collections"
    , data        : "../data"
    , models      : "../models"
    , helper      : "../helper"
    , templates   : "../templates"
    , views       : "../views"
  }
});
require([ 'app/app'], function(app){
  app.initialize();
});

And in the main html page I run this:

<script data-main="assets/js/main.js" src="assets/js/lib/require.js" ></script>

Any guidance to the right resources would be very appreciated!

1

There are 1 answers

0
potatopeelings On

Including form2js in require.config just tells it where to find the form2js module (and other information, like it's dependencies, etc.)

To actually load form2.js you have (and hence define the form2js global), you have to list it as a dependency in the module where you are using it.

For example assuming your initialize() function uses it, your last statement would be

require([ 'app/app', 'form2js' ], function(app){
  app.initialize();
});

Note - if app/app.js uses form2js, you have to include it in the define for that module. You might also want to check (in your browser developer tools network tab if form2.js was actually loaded, once you list it as a dependency somewhere)