Thursday, September 26, 2019

php - Code separation in AJAX applications



What are some strategies to achieve code separation in AJAX applications?



I am building a PHP application that I would like to have a nice AJAX front end on. I have long since learned to use some kind of templating in my PHP code to make sure I maintain good separation between my logic and display code, but I'm having trouble finding good ways to do this with the JavaScript code on the front end. I'm using jQuery to make the fetching of XML data and DOM manipulation easy, but I'm finding that the logic and layout code is starting to intermix.



The real problem is that I get XML data from the back end that then has to be reformatted and helpful text has to be wrapped around it (directions and the like). I've thought about sending over already formatted HTML, but that would require extensive reworking of the backend, and there must be a better way than what I have come up with on my own.


Answer





  • write your templates in the HTML page, probably within a hidden element.

  • fetch data via AJAX calls, i find easier to use JSON, so you don't have to 'reformat' XML, it's only data.

  • apply the template to the data to generate HTML and insert into the displayed page.



check some jQuery templating plugins: jsRepeater, jTemplates, noTemplate, Template


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...