Changes between Initial Version and Version 1 of Cookbook/Fusion/Events


Ignore:
Timestamp:
Jun 19, 2008, 7:22:10 AM (16 years ago)
Author:
pdeschamps
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Cookbook/Fusion/Events

    v1 v1  
     1= Adding Events In Fusion Widgets =
     2
     3I  though I would give a little walk through on how to add events in Fusion Widgets for those of you who want to "extend" events in some of the widgets.
     4
     5For this example lets use the legend widget.
     6
     7First add the new event obj, before the "OpenLayers.Class(Fusion.Widget"
     8
     9{{{
     10// CODE BLOCK [legend.js] ---------------------------------
     11Fusion.Event.LEGEND_LAYER_VIS_ON = Fusion.Event.lastEventId++;
     12Fusion.Event.LEGEND_LAYER_VIS_OFF = Fusion.Event.lastEventId++;
     13
     14Fusion.Widget.Legend = OpenLayers.Class(Fusion.Widget,  {
     15
     16//END CODE BLOCK ---------------
     17}}}
     18
     19Next bind this event to the init section of the class around line 70:
     20
     21{{{
     22// CODE BLOCK [legend.js] ---------------------------------
     23//console.log('Legend.initialize');
     24Fusion.Widget.prototype.initialize.apply(this, [widgetTag, true]);
     25       
     26var json = widgetTag.extension;
     27
     28this.registerEventID(Fusion.Event.LEGEND_LAYER_VIS_ON);
     29this.registerEventID(Fusion.Event.LEGEND_LAYER_VIS_OFF);
     30
     31//END CODE BLOCK ---------------
     32}}}
     33
     34Now trigger the event in the legend class,  for this we want to trigger it when the layer checkbox is clicked: around line 499:
     35
     36{{{
     37// CODE BLOCK [legend.js] ---------------------------------
     38    stateChanged: function(obj) {
     39        if (obj.legend && obj.legend.checkBox) {
     40            if (obj.legend.checkBox.checked) {
     41                this.triggerEvent(Fusion.Event.LEGEND_LAYER_VIS_ON, obj);
     42                obj.show();
     43            } else {
     44                this.triggerEvent(Fusion.Event.LEGEND_LAYER_VIS_OFF, obj);
     45                obj.hide();
     46            }
     47        }
     48    }
     49//END CODE BLOCK ---------------
     50}}}
     51
     52Now in your supporting js for the Fusion application you should have a function called when fusion is initialized. inside this function you will need to registerForEvent on the legend widget to handle this trigger. :
     53
     54{{{
     55// CODE BLOCK ---------------------------------
     56var legend = Fusion.getWidgetsByType('Legend');
     57      if (legend.length > 0) { // 0 is the first legend widget.
     58            legend[0].registerForEvent(Fusion.Event.LEGEND_LAYER_VIS_ON, layerON);
     59            legend[0].registerForEvent(Fusion.Event.LEGEND_LAYER_VIS_OFF, layerOFF);
     60      }
     61// END CODE BLOCK ---------------------------------
     62}}}
     63
     64and finally add your layerON and layerOff functions:
     65
     66{{{
     67// CODE BLOCK ---------------------------------
     68function layerON(obj,resp){
     69    console.dir(resp);
     70}
     71
     72function layerOFF(obj,resp){
     73    console.dir(resp);
     74}
     75// END CODE BLOCK ---------------------------------
     76
     77}}}
     78That's it you should see the object passed back from the widget to the application.