Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing backbone objects inside requireJS module from console

I'm working on my first large-scale Backbone/RequireJS app, and I have a simple question.

When I have a view open on a page, and I'm in the console, how do 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 don't have a global object like this anymore. What I've done for debugging is just create a new object on window that I can then access from the console. Is there a way to dig into this without having to create new variables on window? (I obviously would remove this global obj before production, just would prefer to save the step & go straight to console).

Cheers.

like image 925
Scott Silvi Avatar asked Dec 17 '12 19:12

Scott Silvi


1 Answers

I haven't found a great solution for this, but here's what I do.

If I just want to access a single module, I type out the whole require spell:

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

Sometimes, if I need access to multiple modules, I define a one-liner for helper first

> 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 already existing instance of something, I just set a breakpoint in the debugger and use the locals, which are available in the console in break mode.

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

like image 121
jevakallio Avatar answered Oct 04 '22 20:10

jevakallio