JavaScript/Notes/CustomEvents

From Noisebridge
< JavaScript(Difference between revisions)
Jump to: navigation, search
(Objects Talking)
 
Line 1: Line 1:
 
== What are they? ==
 
== What are they? ==
 
An event is a function call that signifies something happened.  
 
An event is a function call that signifies something happened.  
 +
 +
== Data Received ==
 +
 +
Example: [http://garretts.github.io/ape-javascript-library/example/widget/HSVPicker/ HSVPicker].
 +
 +
<source lang="javascript">
 +
var transport = new AjaxTransport(url);
 +
transport.onsuccess = transportSuccessHandler;
 +
 +
function transportSuccessHandler(ev) {
 +
  var data = ev.data;
 +
  updateTable(data);
 +
}
 +
</source>
  
 
Custom calls notify subscribers. The function is either defined by the "class" (default) or shadowed on the instance, by the client of the API.
 
Custom calls notify subscribers. The function is either defined by the "class" (default) or shadowed on the instance, by the client of the API.

Latest revision as of 23:32, 18 January 2014

Contents

[edit] What are they?

An event is a function call that signifies something happened.

[edit] Data Received

Example: HSVPicker.

var transport = new AjaxTransport(url);
transport.onsuccess = transportSuccessHandler;
 
function transportSuccessHandler(ev) {
  var data = ev.data;
  updateTable(data);
}

Custom calls notify subscribers. The function is either defined by the "class" (default) or shadowed on the instance, by the client of the API.

[edit] Objects Talking

  • Class - defines methods and default handlers
  • Instance - shadows class
  • Instance usage (from other objects)

[edit] Why?

Centralized dispatch. Method called from many places and when it is done, shared notifications fire.

var tableSort = new Factory(function() {
 
  function _getSortFunction(sortType) {
    if(sortType == "number") {
     return function() { };
    }
  }
 
  function _isSortedBy(tableSort, sortType) {
 
  }
 
  var configData = {};
 
  function TableSort(id, config) {
    this.id = id;
    configData[id] = Object.create(config);
  }
 
  TableSort.prototype.sortBy = function(sortType) {
    var config = configData[this.id];
 
    if(config.currentSort != sortType) {
      config.sortFunction(this);
      this.onsort(sortType);
    }
  };
 
  return TableSort;
});
 
 
 
function Factory(getConstructor){
  var i = 0, ctor;
  this.getById = getById;
 
  function getById(id, config) {
    var instances = this.instances;
    if(!instances) { // First time.
      instances = this.instances = {};
      // Get the constructor.
      ctor = getConstructor(this);
    }
    return instances[id] || (instances[id] = new ctor(id, config));
  }
}
Personal tools