ExtJS 4.1 TabPanel dynamically load the page and execute the script [convert], extjstabpanel
ExtJS 4.1 TabPanel dynamically loads pages and executes scripts
According to the official example, the page can be dynamically loaded, but the script is not executed, so I checked the SDK and google and found that scripts needs to be set to true, so I set this attribute. The entire code is as follows:
TabPanel. add ({
Title: 'dynamic page ',
Closable: true,
Loader: {'test.htm', loadMask: 'loading... ', autoLoad: true, scripts: true}
});
At first, I thought there was a problem with writing the script for loading the page. Because I checked a lot of information, some people said they wanted to write it in <body>. Some people said they could only write it on the page, js files cannot be referenced with src, but it cannot be used in any case. loading a page is very simple, and a pop-up window is displayed as long as the page is loaded.
<Script type = "text/javascript">
Alert ("addd ");
</Script>
Later I thought about whether it was necessary to manually load the event activated on the tab, instead of autoLoad. So I tried again and finally succeeded. I was excited!
TabPanel. add ({
Title: 'dynamic page ',
Closable: true,
Loader: {'test.htm', loadMask: 'loading... ', autoLoad: true, scripts: true}
Listeners :{
Activate: function (tab ){
Tab. loader. load ();
}
}
});
I was thinking about this until now. I finally got this tab in one day, but I was excited and found that this was a problem. Every time I click the tab, the activation event will be triggered to read the background page once, the result I want is that after the front-end is loaded once, switch the tab and do not access the back-end. So I checked the SDK and google and wanted to judge that the tab has been loaded in the activate event, but it did not
The next day, I removed listeners, and somehow wanted to test the page loading effect. I specifically blocked the page thread and added the following code:
System. Threading. Thread. Sleep (5000 );
This time it was unintentional, and I found that the page loaded every five seconds was able to execute the script normally!
The procedure is as follows: Click the Load button and then click the Dynamically Loaded tab. The tab has no content. After five seconds, the content appears and the script is executed.
However, the loaded page cannot always be sleep, so you can continue to check the information and try again, or even start to debug the extjs source code. Here we will talk about how to debug it and switch the following script on the page.
<Script type = "text/javascript" src = "/Scripts/ext/ext-all.js"> </script>
<Script type = "text/javascript" src = "/Scripts/ext/ext-debug.js"> </script>
I won't explain how to debug it. google has many problems: Debugging for a day is fruitless. However, the Code for extjs was the first time in close contact.
In addition, we also found a strange thing. Even when debugging, even if Sleep (5000) is added, the loaded page script cannot be executed.
On the third day, I joined the QQ group and posted posts on various forums to continue google.
On the fourth day, I was lucky to use Microsoft's bing search autoload: true, scripts: true. The first line is described as follows, it only talks about how the panel of extjs4.0 loads the page and executes the script, so it tests according to its code.
Ext. onReady (function (){
Var panel = Ext. create ('ext. Panel ',{
Title: 'anchorlayout ',
RenderTo: Ext. getBody (),
Loader: {url: "test.htm", autoLoad: true, scripts: true} // 1.htm page
});
});
This code can be successfully executed! I'm so excited, because the tab can load any component, and the panel can also be loaded, so I modified my code.
Var panel = Ext. create ('ext. Panel ',{
Title: 'dynamic page ',
RenderTo: Ext. getBody (),
Closable: true,
Loader: {'test.htm', loadMask: 'loading... ', autoLoad: true, scripts: true}
});
TabPanel. add (panel );
Successful! It is really ecstasy. After four days, there is a good result. This joy can only be understood by our technicians.
Comparing code differences, I found that such a configuration is missing, that is, the above marked with yellow, renderTo: Ext. getBody (), actually, the script can be displayed normally only after being render. Why is there no SDK !!
But ...... This is not a perfect solution. Careful friends will know that the script will load the page to the home page first and switch the tab to disappear. This is a serious problem, I was so excited that I didn't find it, alas.
I had to stop typing the keyboard and think carefully. I loaded the page three times and executed the script successfully.
1. The event where the tab is activate
2. Click the tab after sleep and wait for the page to load.
3. Add renderTo Configuration
After a long time of thinking, I finally found that the three successful times all have something in common. The loaded page is displayed. That is to say, if the tab first loads the page, then, the script will not be executed.
In order to verify my idea, I immediately started the test, set sleep to 100 milliseconds, click the Load button, and check the loaded tab after one second, sure enough, the script is not executed !!!
Finally, find the cause: the tab must be displayed first, and then load, which is simple. Check the SDK immediately. It is not difficult to find the show method, So modify the code.
Tabs. add ({
Title: 'dynamic page ',
// RenderTo: Ext. getBody (),
Loader :{
Url: 'test.htm ',
LoadMask: 'loading ...',
AutoLoad: true,
Scripts: true
}
}). Show ();
OK. The script runs normally. At this point, my problem is solved perfectly.