With the increasing number of Ajax applications, various Ajax libraries (Prototype), Ajax Framework (DWR), and Ajax Toolkit (Dojo and YUI) are increasingly enriched, is there a way to combine them? Similar to Spring, of course, I cannot build an IoC microkernel to "Bond" Ajax, but it is no problem to integrate these reusable Ajax components, in this way, we can avoid repeated invention of the wheel, and develop strengths and circumvent weaknesses for various Ajax methods to form a comprehensive Ajax solution. It also increases the flexibility for developers to familiarize themselves with Ajax components.
At present, our company has formed a complete set of Ajax-based products, encapsulated its own Ajax frontend and backend communication mechanisms, and provided reusable client components, I tried to integrate our product with the Dojo Toolkit. The following is my practice. It integrates the Dojo ComboBox Widget, which is actually an Auto Completion component, similar to Google Suggest.
Start with the dojo test class test_combox.html, add a debugger for tracking and debugging, and clarify the loading process of the Dojo Widget.
After tracking and debugging, I have a general understanding of the Dojo widgets: first, load the JavaScript file that is currently needed, and then parse the entire html page. There are three ways to use widgets on pages: one is to add some attributes that can be parsed by dojo on html elements, such
OnValueChanged = "setVal1">Among them, dojoType, autocomplete, and onValueChanged are all attributes that can be recognized by dojo, which is similar to typestry. The second method is to use DojoML:OnValueChanged = "setVal1"/>This writing style is a little abnormal. It is basically the same as the custom tag in jsp. It just moves the parsing process from the backend to the front-end. Later I saw some frameworks doing the same, so I had nothing to say.Another method is to use javascript to create a widget on the specified html element after the page is loaded:Var combo;Dojo. addOnLoad (init );Function init (){Combo = dojo. widget. createWidget ("dojo: ComboBox", {name: "prog", autocomplete: false, dataUrl: "comboBoxData. js "}, dojo. byId ("progCombo "));}When creating element parsing, use the combobox html template defined by dojo and the css template to insert the final combobox control into the page.Next, let's take a look at how Dojo ComboBox communicates with the background through ajax. Dojo ComboBox provides two Automatic completion methods: one is to download all the data to the front-end cache, then, on the frontend, the required data list is automatically completed from the cache based on the user input data. The other is to send a request to the background in real time based on the data entered by the user to obtain the data to be completed automatically. Of course, the data will also take the content entered by the user as the key, the obtained data is cached as value. For the two methods, Dojo is implemented through different DataProvider (dojo. widget. incrementalComboBoxDataProvider and dojo. widget. basicComboBoxDataProvider). This is exquisite and I admire it very much. These two classes are implemented in the form of dojo. declare ("className", "parentClassName", constructor, and declarationBody), which is different from our previous practices. In short, it is more subtle!The process of sending a request from Dojo to the background is encapsulated in dojo. io. in the bind () method, we have our own front-and back-end communication mechanism, because we must find a way. io. bind () is replaced with our practice to achieve final integration, because the data interaction of Dojo ComboBox is encapsulated in DataProvider, because we only need to implement our own DataProvider, so we don't need to modify the Dojo source, and we can also use the Inheritance Mechanism of Dojo, from the existing DataProvider integration, I need to replace the method, which makes me feel like writing Java.Dojo. declare ("Dojo. widget. incrementalDoradoComboBoxDataProvider ",Dojo. widget. incrementalComboBoxDataProvider,Null,{// Method to be replaced. Use your own communication mechanism.StartSearch: function (/* String */searchStr,/* Function */callback ){If (this. _ inFlight ){// FIXME: implement backoff!}Var cmd = getControl (this. searchUrl );Cmd. parameters (). setValue ("searchString", searchStr );Var _ this = this;EventManager. addDoradoEvent (cmd, "onSuccess", function (command ){_ This. _ inFlight = false;// Convention:// 1.the key must be "result"// 2.the data format must be [["Alabama", "AL"], ["Alaska", "AK"] or [{"Alabama": "AL "}, {"Alaska": "AK"}]Var data = dj_eval (command. outParameters (). getValue ("result "));If (! Dojo. lang. isArray (data )){Var arrData = [];For (var key in data ){ArrData. push ([data [key], key]);}Data = arrData;}_ This. _ addToCache (searchStr, data );Callback (data );});Cmd.exe cute ();This. _ inFlight = true;}});Through the above processing, we can use our own frontend and backend communication mechanism to complete the request data.The next step is to generate our page and add the Javascript script for dojo loading:
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.