0

In the following code, I am trying to trigger an event using dynamic require. For some reason I am not able to access app object in the eventRouter method. I am getting "TypeError: app is undefined" error. I have implemented listener on show event in respective controller files.

My question is similar to this post except my listeners are in different controller files and I am not able to access app object as suggested in the post.

Help appreciated !!!!

define(["app",
    "tpl!templates/nav/nav.tpl",
    "tpl!templates/nav/navMenuItem.tpl",
    "entities/navEntity"
],
function(app, navTpl, navMenuItem, navEntity){
    navMenuItem =  Backbone.Marionette.ItemView.extend({
        template: navMenuItem,
        events: {
            "click a": "eventRouter"
        },
        eventRouter:function(ev)
        {
            var that = this;
            var moduleName = $(ev.currentTarget).text().toLowerCase();
            require(['controllers/' + moduleName + 'Controller'], function(controller){
            app.trigger(moduleName + ':show');
        });
    }
  });

    navMenu = Backbone.Marionette.CollectionView.extend({
        tagName: 'ul',
      itemView: navMenuItem,
      collection: navEntity.navItems,
    });

    return {
        navMenu:    navMenu,
        navMenuItem: navMenuItem
    }
});
Community
  • 1
  • 1
Me Unagi
  • 615
  • 2
  • 7
  • 18
  • "TypeError: app is undefined" is the error. I should have been more specific. I am trying to get app object in eventRouting method. – Me Unagi Mar 18 '14 at 16:09
  • When I check in Chrome what kind of error message I'd get depending on various scenarios what I get is `ReferenceError: app is not defined` or `TypeError: Cannot call method 'trigger' of undefined`. The first one should not happen since you have `app` as a function parameter. Are you sure the actual error you are getting is not the second one? And does the line number correspond to where `app.trigger` is in your question? – Louis Mar 18 '14 at 17:17
  • Sounds like a possible circular dependency. Does your app require the module above? – T Nguyen Mar 18 '14 at 17:57
  • Look like my problem is the view not able to access the app object. I am getting "TypeError: app is undefined" error with the following code too. `define(["app"], function(app){ console.log(app); })` – Me Unagi Mar 19 '14 at 04:54
  • T Nguyen is correct about circular dependency. I am calling the view in app.js before returning app. – Me Unagi Mar 19 '14 at 05:00

1 Answers1

0

To overcome Circular dependencies you can check the Following :

https://stackoverflow.com/a/4881496/2303999

Manage your modules accordingly and avoid dependencies. Make common js file for functions you use use now and then. You can even use Marionette Vent object to pass events and do according on that event.

Community
  • 1
  • 1