- Ext JS 3.0 Cookbook
- Jorge Ramon
- 756字
- 2021-04-01 13:43:46
Building custom JavaScript classes that inherit the functionality of Ext JS
You can incorporate features of Ext JS into your own JavaScript classes. For example, the ObservableList
class created in this recipe will use the features of the framework's Ext.util.Observable
class to fire notifications when items are added, removed, or when the list is cleared. The list's interface will be as follows:
add(object):
A function that inserts an item in the list and returns the position into which the item was insertedinsert(index, object):
A function that inserts an item to theList
at the specified indexitem(index):
A function that returns the element at the specified indexremove(object):
A function to remove the first occurrence of a specific objectremoveAt(index):
A function in charge of removing the item at the specified indexeach(fn, scope):
A method that executes the specified function once for every item in the listclear(): A function
to remove all items from the listadd: An
event signaling that an element was addedremove: An
event notifying that an element was removedclear:
An event signaling that the list was cleared
How to do it...
Let's proceed to build and test the ObservableList
class as shown in the following steps:
- Define the
ObservableList
class:Ext.namespace("Samples"); Samples.ObservableList = function() { this.items = []; this.length = 0; // The events our custom class will expose. // The parent Class, Observable, will handle event publishing //for us. this.addEvents("add", "remove", "clear"); Samples.ObservableList.superclass.constructor.call(this); };
- Inherit the
Observable
class's functionality by establishing our class as an extension ofObservable:
- Now, implement our class's interface:
Ext.extend(Samples.ObservableList, Ext.util.Observable, { allowFunctions: false, //Adds an item to the list and //returns the position into which the new element was inserted. add: function(o) { this.items.push(o); this.length++; // Fire the add event, returning the position // into which the new element was inserted. pos = this.length - 1; this.fireEvent("add", pos); return pos; }, // Inserts an item to the List at the specified index. insert: function(index, o) { //If the index is outside the list, insert the element at // the end of the list. if (index >= this.length) { return this.add(o); } this.length++; this.items.splice(index, 0, o); this.fireEvent("add", index); }, // Removes all items from the list. clear: function() { this.length = 0; this.items = []; this.fireEvent("clear"); }, // Determines the index of a specific item in the list. indexOf: function(o) { return this.items.indexOf(o); }, // Determines whether the List contains a specific value. contains: function(o) { return this.indexOf(o) != -1; }, // Our enumerator function. Executes the specified function //once for every element in the list. each: function(fn, scope) { var items = [].concat(this.items); for (var i = 0, len = items.length; i < len; i++) { if (fn.call(scope || items[i], items[i], i, len) === false) { break; } } }, // Removes the item at the specified index. removeAt: function(index) { if (index < this.length && index >= 0) { this.length--; var o = this.items[index]; this.items.splice(index, 1); this.fireEvent("remove", o); } }, // Removes the first occurrence of a specific object. remove: function(o) { this.removeAt(this.indexOf(o)); }, // Return the element at the specified index. item: function(index) { var item = this.items[index]; return item; } }); Samples.ObservableList.prototype.get = Samples.ObservableList.prototype.item;
- It's time to test our class. Let's do it as follows:
Ext.onReady(function() { list = new Samples.ObservableList(); for (var i = 0; i < 15; i++) { pos = list.add("test " + i); } // Add handlers for the list's events. list.on("remove", function(o) { alert("Removed: " + o); }); list.on("add", function(index) { alert("Added at position: " + index); }); list.on("clear", function() { alert("List length is: " + list.length); }); document.write("List length is " + list.length + "<br/>"); // Insert an additional element and //check that the add event fires. var index = 2; list.insert(index, "A new item"); document.write("Just inserted: " + list.item(index) + "<br/>"); document.write("List length is: " + list.length + "<br/>"); // Remove an item an verify that the remove event fires. index = 5; document.write("Removing item at position" + index + "<br/>"); list.removeAt(index); document.write("List length after removal: " + list.length + "<br/>"); document.write("Clearing list...<br/>"); // Remove all items and check that the clear event fires. list.clear(); document.write("List length after clear: " + list.length + "<br/>"); });
A powerful mechanism for extending classes is provided by Ext JS with Ext.extend(subclass, superclass, [overrides])
. This function allows you to extend one class with another class and, optionally, to override the superclass's members.
Our example first defines the custom ObservableList
class and passes its events to the parent, Ext.Observable
. It then uses Ext.extend(subclass, superclass, [overrides])
not only to establish that the custom class implements Ext.Observable
, but also to define the bulk of its own interface—the add(object), insert(index, object), clear(), indexOf(object), each(fn, scope), removeAt(index), remove(object)
, and item(index)
functions.
Multiple versions of this approach are used by Ext JS to define its own class hierarchy. I encourage you to examine the source code of the library in order to get familiar with them.
- The Adding features to the Ext JS classes recipe, covered earlier in this chapter, explains how to add new functions to the Ext JS classes
- The A custom column layout recipe from Chapter 2 is an example of how to extend the native Ext JS layouts
- The A three-panel application layout with one line of code recipe from Chapter 2 shows how to build a reusable Ext JS component that encapsulates a three-panel layout
- 從零開始:Flash CS6中文版基礎(chǔ)培訓教程
- AutoCAD Civil 3D 2018 場地設(shè)計實例教程
- Cinema 4D 2024+AI工具詳解與實戰(zhàn)(視頻微課·全彩版)
- Excel 2013電子表格處理
- 斯科特·凱爾比的零基礎(chǔ)攝影后期課 Lightroom數(shù)碼照片調(diào)修技法
- Adobe創(chuàng)意大學Illustrator產(chǎn)品專家認證標準教材(CS6修訂版)
- Midjourney AI案例實戰(zhàn):繪本+動漫+插畫
- 攝影輕松入門:Photoshop后期處理
- VR策劃與編導
- Origin科技繪圖與數(shù)據(jù)分析
- Excel革命!超級數(shù)據(jù)透視表Power Pivot與數(shù)據(jù)分析表達式DAX快速入門
- Configuring IPCop Firewalls: Closing Borders with Open Source
- Cinema 4D/After Effects印象 影視包裝技法精解基礎(chǔ)篇
- Flash ActionScript 3.0互動設(shè)計項目教程
- Altium Designer18電路板設(shè)計入門與提高實戰(zhàn)