Last active
August 29, 2015 14:10
-
-
Save jasonmit/05ec9e993f87a0601151 to your computer and use it in GitHub Desktop.
Ember.observerOnce - combines Ember.observer + Ember.run.once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Example: http://jsbin.com/welusizema/1/edit | |
// | |
// Since observers are not async, it's usually a good | |
// practice, when observing numerous properties, to | |
// wrap the observer function with an Ember.run.once. | |
// ``` | |
// fooBarChanged: Ember.observer('foo', 'bar', function () { | |
// Ember.run.once(this, this.scheduledChange); | |
// }), | |
// scheduledChange: function () { | |
// console.log('Will only be invoked once if foo and bar are changed within the same runloop'); | |
// } | |
// ``` | |
// This just removes having to create two functions, one for the sole purpose of | |
// invoking Ember.run.once. | |
// | |
// The above code would now become: | |
// ``` | |
// fooBarChanged: Ember.observerOnce('foo', 'bar', function () { | |
// console.log('Will only be invoked once if foo and bar are changed within the same runloop'); | |
// }) | |
// ``` | |
Ember.observerOnce = function observeOnce () { | |
var a_slice = [].slice; | |
var func = a_slice.call(arguments, -1)[0]; | |
if (typeof func !== 'function') { | |
throw new Ember.Error("Ember.observer called without a function"); | |
} | |
var paths = a_slice.call(arguments, 0, -1); | |
var out = function () { | |
Ember.run.once(this, func); | |
} | |
out.__ember_observes__ = paths; | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment