Last active
August 29, 2015 14:06
-
-
Save knownasilya/4d28a81d2835228c4dc6 to your computer and use it in GitHub Desktop.
Ember observerThrottled
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
'use strict'; | |
var Ember = require('ember'); | |
module.exports = function () { | |
var args = argsToArray(arguments); | |
var argsLength = args.length; | |
var funcIndex, keys, throttled; | |
if (typeof args[argsLength - 2] === 'function') { | |
funcIndex = argsLength - 2; | |
} | |
else if (typeof args[argsLength - 3] === 'function') { | |
funcIndex = argsLength - 3; | |
} | |
else { | |
throw Error('Invalid arguments'); | |
} | |
throttled = args.slice(funcIndex); | |
keys = args.slice(0, funcIndex); | |
return Ember.observer.apply(Ember, keys.concat(function() { | |
Ember.run.throttle(this, throttled[0], throttled[1], throttled[2] || false); | |
})); | |
}; | |
function argsToArray(args) { | |
var len = args.length; | |
var arr = new Array(len); | |
var i = -1; | |
while (++i < len) { | |
arr[i] = args[i]; | |
} | |
return arr; | |
} |
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
'use strict'; | |
var Ember = require('ember'); | |
var observerThrottled = require('../utils/observer-throttled'); | |
var SearchController = Ember.ArrayController.extend({ | |
searchQueryChanged: observerThrottled('searchQuery', function () { | |
var searchQuery = this.get('searchQuery'); | |
console.log(searchQuery); | |
}, 400, true) | |
}); | |
module.exports = SearchController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment