AJAX and MVC Template

Please redirect me if there is a similar question. I could not find anything, although I am sure that my problem is quite common ...

I have a page with a 5-6 div that can be individually loaded via Ajax requests. Through the prototype ajax.request (), the server (php) sends back the HTML code for the division before the client updates the internal divHTML tags.

Here is my question: What is the best practice for storing the server side MVC pattern regarding the HTML code that it throws?

For now, my models are returning database data to the controller, allowing it to initiate a really long var containing HTML code, which it then repeats. My problem is that I end up with a lot of HTML code in my controller classes ...

+4
source share
5 answers

You can use JSON to transport data on the client side and build there. This way you will have an abstract data source that is not related to your markup. JSON can be directly evaluated into a javascript object.

+4
source

Are you sure you want to use MVC? C can mostly be removed by / RESTful URLs.

As Andy said, you should use JSON to transfer data to the client side. XML is also a widely used alternative (since it works much better if other applications should use your services). XML can easily translate to JSON! And the JSON code is valid JavaScript object code. This way, you can use it along with the client side templates of the verse.

You should try EJS for browser / client templates! If you do this, you do not have an HTML template in your controllers! Just business logic. This follows many of the best SOA practices. The architecture pattern is called SOFEA or SOUI (which is the same).

I wrote my homepage. Evaluation of powerful template engines has confirmed that EJS is the best candidate.

Because: 1. Fast! 2. It's free (MIT license)! 3. It works well with JQuery 4. It really modifies the DOM, so other methods can access the templates used ( JS Repeater ).

Other frameworks:

  • JSmarty : Not so easy to use, but it can use Smarty templates. It is not an entreprise prooven and is still in heavy development.

  • Javascript Templates for Trimpath : Doesn't work with JQuery / Prototype ... Also under development.

  • jQSmarty : Nice, but it looks like development has stopped. The last change was in 2008.

  • seethrough_js : Invasive template layouting. Nice for the people of Erlang.

  • JsonML : also an invasive template format based on JSON. What do you think about it? I think that designers should stay on their HTML / CSS elements, so no knowledge is lost.

  • JS Repeater : Reminds me of my own bad attempts. I checked this and used it .. but it does not cope with many things very well. (Such empty fields, etc.)

  • Pure : time to start a relegios war on how to design pages? I think Pure is not the answer. This is bloating if you determine what you really need to do and it does not scale like JSF. It has no invasive syntax, it is very good. But the price of hard rules to solve problems is not for me. He just feels that he is wrong. I met other people who think completely different! Test it and let me know what you think.

+3
source

This is what I do for MVC + AJAX ...

Really simple implementation if you asked me.

http://jarrettatwork.blogspot.com/2009/02/aspnet-mvc-ajax-brief-introduction.html

+1
source

If you think that the most important letter in MVC is V for working with AJAX. AJAX with HTML and JS is part of the presentation layer, so in theory this is the place for View - part.

View is responsible for what you send to the end user, and MVC-patter is not only the separation of the model, view and controller, but also the ability to use multiple views for the same data model.

Therefore, it is best to encapsulate the code in the class and use the same controller code to render different views. In the first case, it can be a drawing of a static page, but in the other case it is a look specially designed for AJAX calls, and the data can be in JSON or another standard format, it does not matter if you respect the responsibilities that each level has.

+1
source

If HTML mainly consists of string literals, as I understand it, you probably should move the HTML outside of <?? > tags and insert dynamic content from the database with small built-in PHP fragments that reference the variables set by the controller.

This is an efficient template engine. Remember that PHP is basically a template engine.

Example:

<?php include 'controller.php'; // set variables used below ?> <div> <h1>Hi there, <?=$UserName?></h1> <p>Since you've been here, <?=$numberOfDays?> days have gone by</p> </div> 

etc .. It also returns you the syntax highlighting in your HTML code and eliminates the need to concatenate all long string literals inside your PHP code, which often makes code difficult to read.

0
source

All Articles