Udostępnij za pośrednictwem


Releasing Component Resources

Your custom client component such as a behavior, control, or base component might have to release resources before the component is disposed. If it does, you can override the Sys.Component.dispose method and release the resources in the overridden method. This makes sure that the resources are released immediately before the component is disposed. Your component inherits the dispose method from the Sys.Component, Sys.UI.Behavior, or Sys.UI.Control base class, depending on the component's base type.

In your component's dispose method, follow these steps:

  • Dispose or reset any resources used by the component.

  • Stop any processes that the component might queue internally, and disable any functionality that might be called by the component consumer.

  • Invoke the dispose method of the base class.

    Note

    It is useful to provide a public property that indicates the component's enabled status and that raises a property-changed event in its set accessor. Then in the dispose method, set the property to indicate that the component is disabled. This enables a page developer to detect the availability of the component.

The following example show how to override the dispose method that is defined in the prototype of a component class.

dispose: function() {
    // Call set_enabled so the property-changed event is raised 
    //  in order to notify any attached listeners.
    this.set_enabled(false);

    // Stop any component processes so that 
    // they are not called after disposal.
    this._stopSimpleComponentProcesses();

    // Call the base dispose method.
    Samples.SimpleComponent.callBaseMethod(this, 'dispose');
}

The component in this example provides an enabled property that is set to false in the dispose method. The enabled property's set accessor method raises a property-changed event that a page developer can bind to, which enables the developer to test the component's availability. This component also has a private _stopSimpleComponentProcess method that is responsible for stopping any queued process that could still raise a tick event. The method also disables functionality that might be invoked by the component consumer. As a last task, the base dispose method is called by invoking the inherited Type.callBaseMethod method.

Note

All registered classes inherit the callBaseMethod method. For more information, see Type.callBaseMethod Method.

See Also

Tasks

Creating Custom Non-Visual Client Components

Concepts

Defining Custom Component Properties and Raising PropertyChanged Events