Ext JS - Learning Center

Manual:Intro:Patterns:Flyweight

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: Patterns:Flyweight
Author: jsakalos
Published:
Ext Version: 1.1
Languages: en.png English kr.png Korean

Definition

Use sharing to support large numbers of fine-grained objects efficiently.

Used in Ext

  • Ext.fly

Discussion

N.B. The following is taken directly from jsakalos's forum post on the Flyweight pattern.

Ext.Element wraps a lot of functionality around DOM element/node, for example functions like hide, show, all animation stuff, dimensions getting and setting function and a lot more.

Ext.Element keeps reference to DOM element it is wrapped around in dom property. Once you have an Ext.Element (e.g. you call Ext.get('some-id') it is an instance of Element class and you can work with it as such.

Now, imagine that you need to hide 1000 DOM nodes, you call 1000 times Ext.get('some-one-of-1000-id').hide() so you create 1000 instances of Element just to call one function: hide.

Ext.fly is one instance of Ext.Element with "replaceable" DOM node it is wrapped around. If you call 1000 times Ext.fly('some-one-of-1000-id').hide() you 1000 times replace dom property of one instance of Ext.Element.

Result: higher performance, lower memory usage.

You only need to keep in mind that you cannot keep Element returned by Ext.fly for later use as it's dom will sooner or later gets replaced by another one.

  • This page was last modified 09:28, 12 July 2008.
  • This page has been accessed 5,930 times.