Skip to content

Instantly share code, notes, and snippets.

@michiel
Created July 22, 2014 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michiel/772198ab3e6c89ed6624 to your computer and use it in GitHub Desktop.
Save michiel/772198ab3e6c89ed6624 to your computer and use it in GitHub Desktop.

Ace editor component for Ember.

  • A two-way binding is set up between value and the text in the editor.
  • Configure Ace in the aceInit callback.

Template

{{ace-editor value=value aceInit=aceInit class="editor"}}

Controller

App.MyController = Ember.ObjectController.extend({
  value: "Text to be manipulated with the Ace editor."
  aceInit: function(editor) {
    editor.setHighlightActiveLine(false);
    editor.setShowPrintMargin(false);
    editor.getSession().setTabSize(2);
    editor.getSession().setMode("ace/mode/javascript");
  }
});

Remember that, as usual with Ace, you must set css width and height on the element for it to be displayed. You can add class and id to the element as usual, as shown in the example template above.

App.AceEditorComponent = Ember.Component.extend({
didInsertElement: function() {
this.editor = window.ace.edit(this.get('element'));
this.get('aceInit')(this.editor);
this.editor.on('change', function(){
this.set('value', this.editor.getSession().getValue());
}.bind(this));
},
valueChanged: function () {
if (!this.get('value'))
this.editor.getSession().setValue('');
else if (this.editor.getSession().getValue() !== this.get('value'))
this.editor.getSession().setValue(this.get('value'));
}.observes('value')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment