Monday, June 11, 2007

Two styles of Ajax


Ajax started quietly materet technology, and it has already processed Patterns different approaches, schools (and there are mandatory and holy war). In particular, several people have (for example note Frank Sommers, which spodvigla me to write this), I read that shaped two fundamentally different approaches to writing ajax-prilozheny :

* A ready HTML on the server automatically implement it in the right place pages
* transfer to the only structured data, and change them HTML'a script on the client

Though Frank says that the two approaches are incompatible, I discovered that it was their very confusing, and I think that good :-).

More approaches

Sending HTML
The approach to the transfer HTML'a ideal for pages which, in conjunction with the server change slightly. For example some form, which after sabmita server again appears on the screen, and with it, or paints a picture that all is well or painted list of mistakes. The ajax reduces the amount of data transmitted on the network server is not taking any form of display, and only the assay.
The undoubted advantages of this approach include exceptional ease of ideas from which directly follows that on the client side can automate virtually all :

* substitution sabmita forms processor javascript'ovym
* formation of the scripting processor data in a form suitable for sabmita server (name1 = value1 & name2 = value2)
* receive an answer and introducing it into the page

Virtually all that remains to be done programmer is :

* tknut finger in it is what button he wants "to make ajax" and what block from the then save the result
* Server explain SWT, which is part of the answer must be cut to requests ajax'nyh

The cons approach, I would include that it is worse than working for "vysokodinamicheskih" page, where one of the changed parts. For example a long thread of comments where sabmitu form with the server to be replaced with a new commentary and, perhaps refresh line "N comment, which is quite a different page. Here, in the automatic model, you should refer to the part, which includes all the necessary pieces, even if there are data that are not expected to change. In practice, this may mean that the network running nemalenkie amounts of data comparable to the size of the actual page, and the whole purpose ajax'a comes down to lack animation browser icon in the upper right-hand corner.
And I underline "may", unfortunately, is true more often than I would like to, because this fully automated approach attracts many developers who do not have the time and / or willingness to deal with scripting, and rely on the fact that SWT help them do so small blood. They could never say on time or properly correct the problem at a lower level.

Transfer structural data

The transfer of structured data was essentially ajax'a even when he called the XmlHttpRequest. So it is a creation designed to appeal to the server and receive a description of the status of the system, expressed in XML, a language that is convenient for computer processing. Immediately I say that despite the fact that this was intended, a light wedge at XML-otvetah not Tsiolkovsky, and the structure can be made differently, as a little later.
Plus, this approach to maximize flexibility. You can make absolutely any format and dataset, which was requested from the server, which is exactly the type of information that is not enough for the customer, and nothing more. At the same customer through Javascript'a page can be changed completely, and as you see fit.
Take the same example with commentary : commentary sent to the server, and the response back like this : What does that comment has been validated and are now on page 12. Everything else can do for you : pririsovat new unit commentary, there perekopirovat text moulds, fomochku delete, update of the new figures.
The most obvious disadvantage of the transfer is much greater volume and complexity of programming. And it is several things.
The first difficulty is to use XML'a. Although the format of a clear and remarkable, in Javascript'e it can be operated only through DOM. That unable to write and very hard to read : too much text for simple things. Fortunately, this particular flaw in the recent move by JSON format : the data returned from the server in the form of XML, but as a piece of code creates Javascript'ovogo object with your structure : He immediately eval'om performed in the script and you get a response facility, which has response.status and response.quantity. It is very easy and without

The emotions of random XML-otveta remains the second issue : a full HTML manually DOM-metodam that, again, and difficult to raise.
Even worse, that most of these are two very different places, which is defined as pages : HTML-shablon and Javascript'ovy code. That means changes to accurately duplicate here and there. And it means that for writing HTML'a you now need a person who can write and HTML, Javascript and heavy. "Maintenance hell, in other words.
In fact, when I began with XmlHttpRequest'om played, it seemed very natural to me : every trick able parsit XML, then this is the correct way to use. The result of the exercises was fairly songs custom ajax-interfeys to chat with many without fenechek. Then, in 2003, he was terribly technologically advanced, but drilling it into something known, we never spodobilis. I was surprised even now that he is still working. Now here is the link to the history :-)

Combining the best features

The incompatibility, which said Frank Sommers, present only in terms of programming, which selects the transfer HTML, and does not want to be immersed in scripting at all. However, if the programmer is not opposed to this in principle, the transfer of the structure can be combined with the transfer HTML'a. The principle, in fact, it is very simple :

* response from the server goes to JSON-obekta
* things, not to generate HTML (eg move something somewhere, hide / open, etc.), performed by the script, using the data from the facility
* if you want to change something trivial pieces of HTML, they passed rows in the same facility and which must be inserted
* Variable part of HTML on the server are formed as small shablonchiki included, and used to start a page and ajax-otvetah

In other words, all the "great idea" is that the need to abandon dogma "Only XML'om" and enjoy what is convenient.
The only thing that I have not liked this approach is that on the server side, the work of a few. Without ajax'a code, which serves as POST form, is only two answers - either the same form with errors, or redirecting, when all is well, since ajax'om to each response option is added to the issuance JSON-objects.