Skip to content

zeppelin/ember-debounced-properties

Repository files navigation

Ember Debounced Properties Mixin Build Status Ember Observer Score

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.

Installation

$ npm install ember-debounced-properties --save-dev

Usage

  1. Extend your Ember.Object (such as an Ember.Component) with the mixin.

    import Ember from 'ember';
    import DebouncedPropertiesMixin from 'ember-debounced-properties/mixin';
      
    export default Ember.Component.extend(DebouncedPropertiesMixin, {
      debouncedProperties: ['value']
    });
  2. Use debouncedValue inside the component's template

    <h2>{{debouncedValue}}</h2>
  3. debouncedValue will follow value after a short delay. You can set the delay with valueDelay.

    {{input value=value}} <- after you done typing it will appear 1.5 seconds later below
    {{my-component value=value valueDelay=1500}}

Gravatar Example

// 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'));
  })
});
{{gravatar-image email='hello@example.com'}}

Experimental ES7 decorator syntax

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.

Usage

Refer to ember-computed-decorators' Babel Setup chapter.

One More Thing™

It is, of course, could be used together with ember-computed-decorators!

@debounced
@computed('firstName', 'lastName')
fullName(firstName, lastName) {
  return `${firstName} ${lastName}`;
}

Contributing

Installation

  • git clone this repository
  • npm install
  • bower install

Running

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit http://www.ember-cli.com/.

License

Copyright (c) 2014 Gabor Babicz (MIT License)

About

Simple way to define debounced properties

Resources

License

Stars

Watchers

Forks

Packages

No packages published