Creates debounced (delayed) versions of properties listed in the debouncedProperties
array. For each of these properties, a debounced<property-name>
version is created with a
default delay of 1000ms. You can override this by defining <property-name>Delay
.
Useful when you want to delay a property update, for example when the user types into an input box, and you don't want to initiate a network- or CPU-intensive operation, such as an HTTP request until the user didn't finish typing.
$ npm install ember-debounced-properties --save-dev
-
Extend your
Ember.Object
(such as anEmber.Component
) with the mixin.import Ember from 'ember'; import DebouncedPropertiesMixin from 'ember-debounced-properties/mixin'; export default Ember.Component.extend(DebouncedPropertiesMixin, { debouncedProperties: ['value'] });
-
Use
debouncedValue
inside the component's template -
debouncedValue
will followvalue
after a short delay. You can set the delay withvalueDelay
.
// components/gravatar-image.js
import DebouncedPropertiesMixin from 'ember-debounced-properties/mixin';
var computed = Ember.computed;
var alias = Ember.computed.alias;
export default Ember.Component.extend(DebouncedPropertiesMixin, {
tagName: 'img',
attributeBindings: ['src'],
debouncedProperties: ['email'],
emailDelay: 2000, // optional, 1000ms by default
src: alias('gravatarUrl'),
gravatarUrl: computed('debouncedEmail', function() {
return '//www.gravatar.com/avatar/'+md5(this.get('email'));
})
});
Inspired by @rwjblue's awesome ember-computed-decorators, it is possible to use the ES7 decorator syntax to define debounced properties without using the mixin. All you have to do is just put a decorator over the property you want to have a debounced version. It can be a regular or a computed property, both will work.
import Ember from 'ember';
import debounced from 'ember-debounced-properties/decorator';
export default Ember.Component.extend({
firstName: 'John',
lastName: 'Doe',
@debounced(2000)
fullName: Ember.computed('firstName', 'lastName', function() {
return `${firstName} ${lastName}`;
})
});
The result is the same as with the mixin: both debouncedFullName
and fullNameDelay
are accessible on the object. For convenience, the delay can be set from the decorator, but the if fullNameDelay
exist, it will have precedence, since it could be set at runtime as well. When you call the decorator without arguments (just @decorator
), the default 1000ms delay will be used.
Refer to ember-computed-decorators
' Babel Setup chapter.
It is, of course, could be used together with ember-computed-decorators
!
@debounced
@computed('firstName', 'lastName')
fullName(firstName, lastName) {
return `${firstName} ${lastName}`;
}
git clone
this repositorynpm install
bower install
ember server
- Visit your app at http://localhost:4200.
ember test
ember test --server
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.
Copyright (c) 2014 Gabor Babicz (MIT License)