VMware {code} Community
VaibhavParkhi
Contributor
Contributor

Global Auto Refresh in vsphere web client

Hi All,


There are two way to handle the global refresh event in vsphere web client.

1) If you want to click on global refresh button and you want execute your custom code for this you need to call event handler with "EventHandler" metadata tag.

[EventHandler(name="{com.vmware.core.events.DataRefreshInvocationEvent.EVENT_ID}")]

   public function onGlobalRefreshRequest(event:DataRefreshInvocationEvent):void {

    requestData();

  }

2) But some time we need to handle auto global refresh on submit of form and wizard completion or some long operation in web client like taking snapshot and list those snapshots on vm table or custom list without click on refresh button.

There are some work around i found for this. Basically vsphere uses a event bus mechanism to dispatch any event in vsphere web client to communicate to other components or modules.

For this first we have to tell to compiler that which event are going to dispatch in used components or view as shown in following example.

package com.vmware.samples.views {


import com.vmware.core.events.DataRefreshInvocationEvent;

import com.vmware.core.events.ObjectEvent;

import com.vmware.core.model.IResourceReference;

import com.vmware.data.common.events.ModelChangeEvent;

import com.vmware.data.query.DataUpdateSpec;

import com.vmware.data.query.events.DataByModelRequest;

import com.vmware.data.query.events.DataRequestInfo;

import com.vmware.samples.events.EventCentral;

import com.vmware.samples.model.SummaryDetails;

import com.vmware.ui.IContextObjectHolder;


import flash.events.EventDispatcher;

import flash.events.MouseEvent;


import mx.core.FlexGlobals;

import mx.logging.ILogger;

import mx.logging.Log;


// Declares the data request events sent from the class.

[Event(name="{com.vmware.data.query.events.DataByModelRequest.REQUEST_ID}",

   type="com.vmware.data.query.events.DataByModelRequest")]


// Declares the DataRefreshInvocationEvent events sent from the class.

[Event(name="{com.vmware.core.events.DataRefreshInvocationEvent.EVENT_ID}",

     type="com.vmware.core.events.DataRefreshInvocationEvent")]

/**

* The mediator for the SummaryView, this view must extends by EventDispatcher class to dispatch event.

*/

public class SummaryViewMediator extends EventDispatcher implements IContextObjectHolder {


   private var _view:SummaryView;

   private var _contextObject:IResourceReference;


   private static var _logger:ILogger = Log.getLogger('SummaryViewMediator');


   [View]

   /**

    * The mediator's view.

    */

   public function get view():SummaryView {

      return _view;

   }


   /** @private */

   public function set view(value:SummaryView):void {

      _view = value;

   }


   [Bindable]

   public function get contextObject():Object {

      return _contextObject;

   }


   public function set contextObject(value:Object):void {

      _contextObject = IResourceReference(value);


      if (_contextObject == null) {

         clearData();

         return;

      }

      // Initialize the view's data once the current context is known.

      requestData();

  _view.refreshButton.addEventListener(MouseEvent.CLICK,refreshButtonClickHandler);

   }


   private function requestData():void {

      // Default data request option allowing implicit updates of the view

      var requestInfo:DataRequestInfo =

            new DataRequestInfo(DataUpdateSpec.newImplicitInstance());


      // Dispatch an event to fetch _contextObject data from the server

      // along the specified model.

      dispatchEvent(DataByModelRequest.newInstance(

            _contextObject,

            SummaryDetails,

            requestInfo));

   }


   /*

     click handler for re-dispatch the DataRefreshInvocationEvent

   */


   private function refreshButtonClickHandler(event:MouseEvent):void

   {   

       dispatchEvent(new DataRefreshInvocationEvent(DataRefreshInvocationEvent.EVENT_ID));

   }

  

   [EventHandler(name="{com.vmware.core.events.DataRefreshInvocationEvent.EVENT_ID}")]

   public function onGlobalRefreshRequest(event:DataRefreshInvocationEvent):void

   {

         requestData();

   }


   /**

    * Handles the server response to DataRequest above.

    *

    * The method name is not important, only its signature and the ResponseHandler tag

    * are, they must match the DataRequest in question. The result is returned with

    * the corresponding model type. The additional error argument was left out here.

    */

   [ResponseHandler(name="{com.vmware.data.query.events.DataByModelRequest.RESPONSE_ID}")]

   public function onData(event:DataByModelRequest, result:SummaryDetails):void {

      _logger.info(" summary data retrieved.");

      _view.chassisDetails = result;

   }


   /**

    * Resets the UI.

    */

   private function clearData() : void {

      _view.chassisDetails = null;

   }

}


}

In above actionscript i register the data refresh event as

[Event(name="{com.vmware.core.events.DataRefreshInvocationEvent.EVENT_ID}",

     type="com.vmware.core.events.DataRefreshInvocationEvent")]


re-dispatch the event in my custom function as


private function refreshButtonClickHandler(event:MouseEvent=null):void

{   

       dispatchEvent(new DataRefreshInvocationEvent(DataRefreshInvocationEvent.EVENT_ID));

}

In my example i taken a button when i click on this global refresh event get dispatched and then is handled by onGlobalRefreshRequest function. 

Thanks,

Vaibhav Parkhi

0 Kudos
0 Replies