Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ember-debounced-properties with all npm packages installed. Try it out:

var emberDebouncedProperties = require("ember-debounced-properties")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ember-debounced-properties v0.0.5

Simple way to define debounced properties

Ember Debounced Properties Mixin Build Status

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

  • ember server
  • Visit your app at http://localhost:4200.

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)

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free