tags:

views:

36

answers:

1

In trying to call a method on the CodeMirror javascript code editor. I'm new to javascript and trying to understand how object oriented stuff works. I'm having problems calling what I believe are methods. For instance,

var editor = CodeMirror.fromTextArea('code', options);
editor.grabKeys(function(e) { alert("Key event");});

This gives the Uncaught TypeError: Cannot call method 'grabKeys' of undefined. Looking at the editor object reveals that grabKeys seems to be located at editor.__proto__.grabKeys.

How should I be thinking about this?

A: 

Probably yoour code should be something like this:

var editor = new CodeMirror.fromTextArea('code', options);
editor.grabKeys(function(e) { alert("Key event");});

Notice the 'new' operator..

Here is a good explanation of what prototype method calls are for:

http://www.javascriptkit.com/javatutors/proto.shtml

Vlad
That doesn't seem to fix it.
Tristan
The linked-to documentation suggests otherwise.
harto
Sorry didn't see that fromTextArea is a utility function that constructs the object for you. So what is your 'options' object?Can you post the contents here?
Vlad
They're the default and work. For instance, adding the editor.grabKeys function to this example: http://marijn.haverbeke.nl/codemirror/contrib/python/index.html
Tristan
Very strange. If I add `editor.grabKeys(function(e) { alert("Asdf");})` in the javascript console it works. If I add it in the html file, right after the editor, it doesn't. Ideas?
Tristan
Seems this call is async. Need to use `initCallback:`.
Tristan