Access to basic objects inside the requireJS module from the console

I am working on my first large-scale Backbone / RequireJS application, and I have a simple question.

When I have a view on the page, and I’m in the console, how can I access the properties of my Backbone object (Models, Views, etc.).

Traditionally in Backbone I do this:

var myApp : { models: {}, views: {}, etc... } 

Using require, I no longer have a global object. What I did for debugging is just to create a new object in a window that I can get from the console. Is there a way to delve into this without having to create new variables in the window? (I will obviously delete this global object before production, I just would rather keep the step and go straight to the console).

Greetings.

+6
source share
2 answers

I have not found a great solution for this, but here is what I am doing.

If I just want to access one module, I type in all the required spell:

 > require(['models/foo'], function(foo) { window.foo = foo; }); > foo.something(); 

Sometimes, if I need access to several modules, I first define a one-line helper

 > var req = function(module, name) { require([module], function(m) { window[name] = m; });} > req('models/foo', 'foo'); > req('models/bar', 'bar'); > foo.something(bar); 

And if I need an existing instance of something, I just set a breakpoint in the debugger and use the locales that are available in the console in break mode.

I would definitely be interested in a better way as well.

+5
source

Have you tried var m = require('your_module') ?

I have to work fine.

0
source

All Articles