Feng Forum

Other Topics => Development => Architecture => : sosamv April 07, 2009, 01:42:17 AM

: Ajax patterns?
: sosamv April 07, 2009, 01:42:17 AM
Hi guys i've just installed opengoo on our company's server, we're using it to keep track of our projects and i'm amazed!  :o (i'm a team lead of a mexican software company)  :)

I didnt know of the existence of ExtJS :S and its great how you guys used it to make opengoo. Right now we are playing with Extjs at the office cuz we'll use it on our next project (i recommended it LOL). So, can you guys tell me an easy way or the best practice to use PHP and the Extjs framework, i looked at the opengoo source code and you guys are using php objects, are you using some sort of a javascript-php framework to interprete the objects?, I'm looking for word of advise.

I'm totally new making full Ajax applications, i've done a lot of javascript ajax enabled components using jquery with php services that return JSON's, but not a whole app hehe.


: Re: Ajax patterns?
: ignacio April 07, 2009, 03:53:16 PM
I can tell you our experience, but I don't know if it is a 'best practice'. We had an application with no ajax support and hacked it to support Ajax requests. What we did is use the same MVC framework we were using, but we added a GET parameter to the request called 'ajax' that tells the framework that the request is an ajax request. When this happens, the framework instead of rendering the view and outputing it to the browser (like it used to do), it saves it into a string and returns it as an attribute of a JSON response. This JSON response also includes information about errors and page actions (features that were previously available with the framework but were treated differently). We later noticed other needs, like some actions don't need to render anything, but just return error or success messages, and so we added the option to not return html content. We also noted the need to be able to reload the current view after an action and to go back, so we added those two options. We also added support for events coming from the server. For example, when a workspace is deleted a "workspace deleted" event is fired from the server (it is added to the "events" attribute of the JSON response). When the request returns to the browser, the Workspace Panel catches the event and removes the deleted workspace. Also, some actions could add some extra data to the response that is later processed on the client side, like in the case of the ExtJS grids used in OpenGoo which expect an array of items and the total amount of items.

So basically, we have a function that we call whenever we want to fetch something through AJAX. This function receives two parameters: an URL and a config parameter to allow defining options like whether the loading sign should be shown, what function to call when the request finishes, etc. This function performs the ajax request and processes the json response. If the response contains error messages it shows them. If the response contains events it fires them. If the response contains contents to load into a panel it loads them, etc. When HTML content is rendered, its links are converted so that instead of browsing to another URL they call this function passing it the URL. The same happens with forms, instead of submitting they call this same function to send the forms contents. You will face a tricky problem when you want to upload a file through ajax. It just can't be done. You have to use iframes to upload files without reloading the page.

I hope you get an idea of how our ajax works, but take into account that we didn't design it from the ground up, we added it to an existing project with no ajax support. It is working pretty well for us nonetheless, but maybe you can find a framework designed from the beginning to support AJAX.
: Re: Ajax patterns?
: sosamv April 07, 2009, 04:21:51 PM
Hi Ignacio! and thanx for your quick replay! Yep im very concerned about the architecture we're going to use, i think we're going to start a lil framework of ours cuz we'll be using Extjs very much. Thanx for the insight!

Saludos desde Monterrey, Mexico =)