ExtJS beginner notes --- Ext. Msg. alert ineffective

Source: Internet
Author: User
Tags define function
I recently started to learn ExtJS. The first example in the book is: ExtonReady (function () {ExtMsgalert (& amp; 39; Hello & amp; 39;, & amp; 39; hello & amp; 39;) ;}); this is the simplest example of ExtJS. A dialog box is displayed on the page, but I have not recently started to learn ExtJS while running it, the first example in the book is Ext. onReady (function () {Ext. msg. alert ('Hello. ', 'Hello');}); this is the simplest example of ExtJS. A dialog box is displayed on the page, but nothing is displayed when I run it... In the console of the browser developer tool, the following error message is displayed: (Cannot call method 'alert 'of undefined), which makes me speechless, because I can be sure that the js file required for ExtJS has been correctly introduced, but why is the alert function not defined? I think it may be because the book is old (the version used in the book is 3.0, and I used 4.0). So I checked the Ext document and found some information: 1. Ext. msg is Ext. window. messageBox. alias 2, the new class definition method introduced in extjs4: 1Ext. define (className, members, onClassCreated); example: Ext. define ('My. sample. person ', {name: 'unknown', constructor: function (name) {if (name) {this. name = name ;}, eat: function (foodType) {alert (this. name + "is eating:" + foodType) ;}}); var aaron = Ext. create ('My. sample. person', 'Aaron '); Aaron. eat ("Salad"); // alert ("Aaron is eating: Salad"); the define method can be used to define classes in Ext, And the create method can be used to instantiate classes. After learning how to instantiate the class, we can try to instantiate Ext. window. messageBox, test the following code: Ext. onReady (function () {var msg = Ext. create ('ext. window. messagebox'); msg. alert ('Hello. ', 'Hello') ;}); running successful! However, Ext. msg should be a global singleton. After all, it feels awkward to use this method. Let's look at the Ext source code and look at Ext. where will Msg be initialized? Ext. define ('ext. window. messageBox ',{.....}, function () {Ext. messageBox = Ext. msg = new this () ;}); in the third parameter of the define function, Ext. msg is defined. When will this function be called? Please refer to the description of onClassCreated, the third parameter of the define method: Callback to execute after the class is created, the execution scope of which (this) will be the newly created class itself. that is, this function will be called when the class is instantiated. In this function, new this () is the new class itself. It is estimated that the global Singleton like Ext. Msg after Ext4 will not be defined at the beginning of the program, but will be defined only after you instantiate the class once. So there is the following code: Ext. onReady (function () {Ext. create ('ext. window. messagebox'); // you only need to call Ext once globally. msg. alert ('Hello. ', 'Hello');}); other global Singleton examples in Ext are also true.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.