Mixin: DebouncedModel

DebouncedModel

Adds debouncing to $save operations so rapid consecutive calls result in only one server request.

Usage:

First add mixin to a model's mixin chain, the following config variables are available: DM_TIMEOUT: sets the debounce timeout, if 0 then debouncing is deactivated. Defaults to 500 DM_ADJOURN: if true, then save operation is rescheduled on every $save call. Default to false

var Bike = restmod.model('api/bikes', 'DebouncedModel', {
      // This is optional.
      DM_TIMEOUT: 100 // change timeout!
    }),
    bike = Bike.build({ id: 1, brand: 'Yeti' });

Then use $save as always

// The following code will just generate 1 request
bike.$save();
bike.$save();
bike.$save();

Or with options

bike.$save({ timeout: 0, adjourn: false });
// same as
bike.$saveNow();
Source:
  • plugins/debounced.js, line 1

Methods

<static> $save(_opt) → {Model}

Debounced $save implementation

IDEA: think of a way of separating the scheduling-rescheduling logic from the async save implementation, this way it can be used for other actions. Something like:

this.$debounce('$save', fun, timeout, adjourn);

This would call fun with a promise in the model context.

Parameters:
Name Type Description
_opt object

Same as setDebounceOptions options.

Source:
  • plugins/debounced.js, line 73
Returns:

self

Type
Model

<static> $saveNow() → {Model}

Convenience method that will cancel any pending save and save inmediatelly.

Source:
  • plugins/debounced.js, line 141
Returns:

self

Type
Model