Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access a backbone model via chrome console

Say for example, I have the following Javascript,

var User=Backbone.Model.extend({
});
var jt=new User({name:"jonathan"});

How would I access jt in the Chrome console?

>jt   // doesn't work
>Backbone....  ?

thx

like image 218
timpone Avatar asked Sep 04 '13 20:09

timpone


2 Answers

you have to make the jt variable global:

jt = new User({name:"jonathan"});

or

window.jt = new User({name:"jonathan"});

or just use the console while in the debugger and set a breakpoint on that line

like image 117
Peter Lyons Avatar answered Oct 15 '22 07:10

Peter Lyons


Peter's answer will do the trick, but it is generally considered bad practice to start making things global. If it's just a quick thing that you plan on reverting, it's ok, but you have to be careful to remember to fix it.

Since you're using Chrome, you can make use of the powerful debugger. Just add a line with debugger right after you've set something you want to inspect.

var User=Backbone.Model.extend({
});
var jt=new User({name:"jonathan"});
debugger;

If you have the developer tools open, refreshing the page will basically set a break point at the debugger. You can then hit Esc to open up a console at the exact scope at which you put your debugger line.

like image 29
bilalq Avatar answered Oct 15 '22 05:10

bilalq