Sift Controller

loadView (state)

Invoked by the Red Sift client when a Sift has transitioned to its final size class.

Parameters:

  • state: previous and current size classes as well as view type and contextual data (email-thread only). For email-thread, the contextual data are the contents of the detail tag emitted by a Sift's DAG for the given thread id, more info here

Returns:

  • null or {html:'<string>', data: '< string | number | array | object | Promise >'}
// Example usage
loadView(state) {
  switch(state.type){
    case 'email-thread':
      return { html: 'email-thread.html', data: { key: state.params.detail.key}};
    case 'summary':
      return { html: 'summary.html', data: this.getCounts()};
    default:
      console.log('unknown Sift type:', state.type);
  }
}

getCounts(){
  return this.storage
    .getAll({ bucket: 'count' })
    .then(values => ({data: values}) );
}

this.storage.subscribe (value, handler)

The controller can subscribe for changes in Storage. The event that is fired, does not contain any data, just the name of the bucket that the changed occurred.

Parameters:

  • value: string (bucket name or * for all buckets) or array with bucket names
  • handler: function event handler function
loadView(state){
  this.storage.subscribe('count', this.onStorageUpdate);
  // ...
}

onStorageUpdate (value) {
  this.storage.getAll({
    bucket: 'count'
  }).then(values => {
    const j = JSON.parse(values[0].value);
    this.publish('onCount', j);
  });
};

this.view.subscribe (name, handler)

Accessible in the view namespace, allows the controller to register for view events.

Parameters:

  • name: string, the name of the event.
  • handler: function, event handler function.
constructor(){
  super();
  // ...
  this.view.subscribe('model-changed', this.whatChanged.bind(this));
}

whatChanged (what) {
  console.log('What changed in the model: ', what);
}

this.publish (name, value)

Notifies listeners for a particular event name.

Parameters:

  • name: string, the name of the event.
  • value: object or string, the object or string to send to the listeners.
this.publish('model-changed', {
  name: 'Jim Bailey Junior', 
  email: '[email protected]'
});

this.view.unsubscribe (name, handler)

Removes an event listener.

Parameters:

  • name: string, the name of the event.
  • handler: function, event handler function.
this.view.unsubscribe('model-changed', whatChanged);