Otis Wright
Otis Wright

Reputation: 2110

Getting CodeMirror value from an object function

I have the following code:

 var examples = {
    'style': {
        create: function (div, ref) {
            var codeMirror = CodeMirror(div, {
                lineNumbers: true,
                mode: 'css'
            });

            this.firepad = Firepad.fromCodeMirror(ref, codeMirror);

            var self = this;
            this.firepad.on('ready', function () {
                if (self.firepad.isHistoryEmpty()) {
                    self.firepad.setText('.red {color: red;}');
                }
            });
        },
        dispose: function () {
            this.firepad.dispose();
        }
    }
};

Now usually I would go codeMirror.getValue() to get the contents of the CodeMirror instance unfortuantely I have no idea how to access a variable in a function in a object (have I even said that right?)

I tried examples.style.getValue() but that of course returns an error.

Any ideas?

Upvotes: 1

Views: 360

Answers (1)

Felix Kling
Felix Kling

Reputation: 816462

Well, you can't. Local variables cannot be accessed outside the function.

You'd have to use the Firepad API to get the value:

var text = examples.style.firepad.getText();
// or
var html = examples.style.firepad.getHtml();

Alternatively you can assign the CodeMirror instance to a property and use getValue.

However you probably want to add another method to the object, for convenience:

getText: function() {
  return this.firepad.getText();
}

Upvotes: 1

Related Questions