Ember.run.scheduleOnce()

scheduleOnce (queue, target, method, args*) Objectpublic

Defined in packages/ember-metal/lib/run_loop.js:364

Schedules a function to run one time in a given queue of the current RunLoop. Calling this method with the same queue/target/method combination will have no effect (past the initial call).

Note that although you can pass optional arguments these will not be considered when looking for duplicates. New arguments will replace previous calls.

function sayHi() {
  console.log('hi');
}

run(function() {
  run.scheduleOnce('afterRender', myContext, sayHi);
  run.scheduleOnce('afterRender', myContext, sayHi);
  // sayHi will only be executed once, in the afterRender queue of the RunLoop
});

Also note that passing an anonymous function to run.scheduleOnce will not prevent additional calls with an identical anonymous function from scheduling the items multiple times, e.g.:

function scheduleIt() {
  run.scheduleOnce('actions', myContext, function() {
    console.log('Closure');
  });
}

scheduleIt();
scheduleIt();

// "Closure" will print twice, even though we're using `run.scheduleOnce`,
// because the function we pass to it is anonymous and won't match the
// previously scheduled operation.

Available queues, and their order, can be found at run.queues

Parameters:

queue [String]
The name of the queue to schedule against. Default queues are 'sync' and 'actions'.
target [Object]
The target of the method to invoke.
method Function|String
The method to invoke. If you pass a string it will be resolved on the target at the time the method is invoked.
args* [Object]
Optional arguments to pass to the timeout.

Returns:

Object
Timer information for use in cancelling, see `run.cancel`.
doc_EmberJs
2016-11-30 16:51:41
Comments
Leave a Comment

Please login to continue.