DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
        • afterPreviousEvents
        • collecting
        • debounce
        • dispatch
        • dispatching
        • queue
        • start
        • stop
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-event/batch/batch

  • Edit on GitHub

Adds task batching abilities to event dispatching.

Object

The can-event/batch/batch module adds task batching abilities to the can-event module. It:

  • Provides a queue method to add batched work.
  • Provides dispatch and overwrites can-event.dispatch to use the task queue when dispatching events.
  • Provides a start and stop method that can create a new task queue.
  • Provides collecting which returns the queue collecting tasks.
  • Provides dispatching which returns the queue dispatching tasks.
  • Dispatches batchEnd when a queue's tasks have been completed.

Use

To batch events, call start, then make changes that dispatch batched events, then call stop.

For example, a map might have a first and last property:

const Person = DefineMap.extend( {
    first: "string",
    last: "string"
} );

const baby = new Person( { first: "Roland", last: "Shah" } );

Normally, when baby's first and last are fired, those events are dispatched immediately:

baby.on( "first", function( ev, newFirst ) {
    console.log( "first is " + newFirst );
} ).on( "last", function( ev, newLast ) {
    console.log( "last is " + newLast );
} );

baby.first = "Ramiya";

// console.logs -> "first is Ramiya"
baby.last = "Meyer";

// console.logs -> "first is Meyer"

However, if a batch is used, events will not be dispatched until stop is called:

import canBatch from "can-event/batch/batch";

canBatch.start();
baby.first = "Lincoln";
baby.last = "Sullivan";
canBatch.stop();

// console.logs -> "first is Lincoln"
// console.logs -> "first is Sullivan"

Performance

CanJS synchronously dispatches events when a property changes. This makes certain patterns easier. For example, if you are utilizing live-binding and change a property, the DOM is immediately updated.

Occasionally, you may find yourself changing many properties at once. To prevent live-binding from performing unnecessary updates, update the properties within a pair of calls to canBatch.start and canBatch.stop.

Consider a todo list with a completeAll method that marks every todo in the list as complete and completeCount that counts the number of complete todos:

const Todo = DefineMap.extend( {
    name: "string",
    complete: "boolean"
} );

const TodoList = DefineList.extend( {
    "#": Todo,
    completeAll: function() {
        this.forEach( function( todo ) {
            todo.complete = true;
        } );
    },
    completeCount: function() {
        return this.filter( { complete: true } ).length;
    }
} );

And a template that uses the completeCount and calls completeAll:

<ul>
{{#each todos}}
    <li><input type='checklist' checked:bind="complete"/> {{name}}</li>
{{/each}}
</ul>
<button on:click="todos.completeAll()">
  Complete {{todos.completeCount}} todos
</button>

When completeAll is called, the {{todos.completeCount}} magic tag will update once for every completed count. We can prevent this by wrapping completeAll with calls to start and stop:

{
    completeAll: function() {
        canBatch.start();
        this.forEach( function( todo ) {
            todo.complete = true;
        } );
        canBatch.end();
    }
}

batchNum

All events created within a set of start / stop calls share the same batchNum value. This can be used to respond only once for a given batchNum.

var batchNum;
person.on("name", function(ev, newVal, oldVal) {
  if(!ev.batchNum || ev.batchNum !== batchNum) {
    batchNum = ev.batchNum;
    // your code here!
  }
});

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.14.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news