Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to debounce an observer

Tags:

ember.js

I have an observer in a controller that saves the object as changes are made. The problem is it is happing too often.

changed: ( ->
  #save code goes here 
).observes("description")

I am thinking something like http://underscorejs.org/#debounce is needed?

Also It seems to save the object twice once when the attribute changes via a key input and then again when the attribute is set from the returned server value.

Any help would be great I am trying to wrap my head around ember.

like image 367
Aaron Renoir Avatar asked Sep 19 '12 01:09

Aaron Renoir


People also ask

How do you use debounce RxJS?

RxJS debounce() Filtering Operator RxJS debounce() operator is a filtering operator that emits a value from the source Observable only after a while (exactly after a particular period). The emission is determined by another input given as Observable or promise.

What is debounce interval?

The amount of time, in seconds, that elapses before the downlink interfaces are brought up after a state change of the uplink interfaces.

What does debounce active mean?

5 min. A debounce system is a set of code that keeps a function from running too many times. It comes from the idea of mechanical switch bounce, where a pushed switch bounces and creates multiple signals. In Roblox, this problem occurs mainly with the BasePart.

What is request Debouncing?

The debounce function delays the processing of the keyup event until the user has stopped typing for a predetermined amount of time. This prevents your UI code from needing to process every event and also drastically reduces the number of API calls sent to your server.


1 Answers

From Ember 1.0.0, you can get a debounced observer in any View or Object by wrapping the debounce call inside another function that observes. Ember.run.debounce doesn't return a function but instead adds the function handle to a dictionary. Every subsequent time Ember.run.debounce gets called with that function handle it will check the dictionary to see the last time the function was called and debounce it as expected.

var MyView = Ember.View.extend({
    calledRarely: function() {
        console.log("This will log rarely.");
    },

    calledOften: function() {
        console.log("This will log often.");
        Ember.run.debounce(this, this.calledRarely, 1000);
    }.observes("propertyThatChangesOften")
});

Here, this.calledOften isn't debounced at all so Ember.run.debounce will actually be called as often as the property is changed. It won't call this.calledRarely until our debounce timeout has completed.

like image 54
Felix Fung Avatar answered Oct 20 '22 17:10

Felix Fung