Ember.run.bind()

bind (target, method, args*) Functionpublic

Defined in packages/ember-metal/lib/run_loop.js:124
Available since 1.4.0

Allows you to specify which context to call the specified function in while adding the execution of that function to the Ember run loop. This ability makes this method a great way to asynchronously integrate third-party libraries into your Ember application.

run.bind takes two main arguments, the desired context and the function to invoke in that context. Any additional arguments will be supplied as arguments to the function that is passed in.

Let's use the creation of a TinyMCE component as an example. Currently, TinyMCE provides a setup configuration option we can use to do some processing after the TinyMCE instance is initialized but before it is actually rendered. We can use that setup option to do some additional setup for our component. The component itself could look something like the following:

App.RichTextEditorComponent = Ember.Component.extend({
  initializeTinyMCE: Ember.on('didInsertElement', function() {
    tinymce.init({
      selector: '#' + this.$().prop('id'),
      setup: Ember.run.bind(this, this.setupEditor)
    });
  }),

  setupEditor: function(editor) {
    this.set('editor', editor);

    editor.on('change', function() {
      console.log('content changed!');
    });
  }
});

In this example, we use Ember.run.bind to bind the setupEditor method to the context of the App.RichTextEditorComponent and to have the invocation of that method be safely handled and executed by the Ember run loop.

Parameters:

target [Object]
target of method to call
method Function|String
Method to invoke. May be a function or a string. If you pass a string then it will be looked up on the passed target.
args* [Object]
Any additional arguments you wish to pass to the method.

Returns:

Function
returns a new function that will always have a particular context
doc_EmberJs
2016-11-30 16:51:39
Comments
Leave a Comment

Please login to continue.