12

I got this error while loading the requirejs file for the backbone. I tried loading the r.js, the requirejs optimizer, but I'm still stuck with it.

Uncaught Error: Mismatched anonymous define() module: function definition(name, global){

"use strict";

var PubSub = {
        name: 'PubSubJS',
        version: '1.3.1-dev'

Following is my js:

define([
'jquery',
'underscore',
'backbone'
],function(){
subAccountRouter = Backbone.Router.extend({
  routes: {
  // Defining the routes
    'sub-accounts': 'subAccountList',
    '*actions': 'defaultAction'
  },
});

Seems there have been some changes made to requirejs define() call function, somehow cant figure it out. Does anyone have ideas??

EDIT:::

Below is the router.js file.

    define([
       'jquery',
       'underscore',
       'backbone'
      ],function($, _, Backbone){
          SubAccountRouter = Backbone.Router.extend({
              routes: {
               'sub-accounts': 'subAccountList',
               '*actions': 'defaultAction'
              },


           initialize: function () {
              this.appContainer = $("#subaccount");
    //collections and models
              this.subAccountCollection = null;
            this.subAccountModel = null;
          },

      subAccountList: function(){
        var self = this;
        },
     defaultAction: function(){
        this.subAccountList();
      },
      });

    return {
       initialize: function() {
           Backbone.history.start();

          }
        };
     }); //main func

What im i doing wrong here?? I check al my paths and they seem to be correct, i still dont get why this issue is still bugging me..:( I have tried changing the paths for the routes, and also passing arguments to the function($, _, Backbone)(as shown below in 1 of the sol'n). However i still seem to see the error. Does any one have any other ideas???

OrangeCMS
  • 55
  • 4
user2942566
  • 455
  • 4
  • 10
  • 22

1 Answers1

18

UPDATE

After checking the docs - this is actually the first error they discuss:

"If you manually code a script tag in HTML to load a script with an anonymous define() call, this error can occur."

So make sure the only <script> tag (at least for any scripts which call define()) in your index.html is the one to requirejs.

END UPDATE

You need to pass in parameters to your function() like so:

define([
'jquery',
'underscore',
'backbone'
],function(jquery, underscore, backbone){
subAccountRouter = Backbone.Router.extend({
  routes: {
  // Defining the routes
    'sub-accounts': 'subAccountList',
    '*actions': 'defaultAction'
  },
});

I wrote a super-simple post on setting up requirejs recently, if you're still stuck.

cs_stackX
  • 1,407
  • 2
  • 19
  • 27
  • Thanks, it should have worked ideally, but again doesnt work for me..:( – user2942566 May 22 '14 at 06:34
  • 1
    is there any way i can get away with this issue??, I mean i can still be able to load my html page and other stuff?? – user2942566 May 22 '14 at 06:39
  • [This article](http://backbonetutorials.com/organizing-backbone-using-modules/) might help. – cs_stackX May 22 '14 at 07:27
  • Can you elaborate on this explanation please? I don't understand it. FOR INSTANCE: "load a script with an anonymous define" What does that mean? Can you show an example? – Prisoner ZERO Oct 16 '15 at 02:01
  • Anonymous define calls define() with no string ID - more here: http://stackoverflow.com/questions/15371918/mismatched-anonymous-define-module – cs_stackX Oct 19 '15 at 11:50