31
Server-Side, PHP-Generated AJAX Mike Ho, Quasidea Development Zend/PHP Conference Thursday, October 22, 2009 Soli Deo Gloria John 3:16 Romans 12 Thursday, October 22, 2009

Server-Side, PHP-Generated AJAX

Embed Size (px)

Citation preview

Page 1: Server-Side, PHP-Generated AJAX

Server-Side,PHP-Generated AJAX

Mike Ho, Quasidea Development

Zend/PHP ConferenceThursday, October 22, 2009

Soli Deo GloriaJohn 3:16 • Romans 12

Thursday, October 22, 2009

Page 2: Server-Side, PHP-Generated AJAX

a bit about me...

• Quasidea Development

• 10+ years of IT consulting experience

• Lead Developer,Qcodo Development Framework

Thursday, October 22, 2009

Page 3: Server-Side, PHP-Generated AJAX

a bit about you...

• Developers vs. Others

• AJAX

• Frameworks

Thursday, October 22, 2009

Page 4: Server-Side, PHP-Generated AJAX

a bit of history...

• PHP 3 and PHP 4

Database

PHP

Thursday, October 22, 2009

Page 5: Server-Side, PHP-Generated AJAX

a bit of history...

• PHP 3 and PHP 4

• PHP 5, ORM and frameworks

Database

PHP View / Controller

PHP Model / ORM

Thursday, October 22, 2009

Page 6: Server-Side, PHP-Generated AJAX

a bit of history...

• PHP 3 and PHP 4

• PHP 5, ORM and frameworks

• PHP with client-side AJAX

Database

PHP View / Controller

JavaScript

PHP Model / ORM

Thursday, October 22, 2009

Page 7: Server-Side, PHP-Generated AJAX

old school examples

• 3 Files Required

• HTML page

• Data provider (e.g. a PHP webservice that generates XML or JSON)

• Client JavaScript(webservice client anda response handler)

Thursday, October 22, 2009

Page 8: Server-Side, PHP-Generated AJAX

something with a bit more data...

• Even though this is a more sophisticated page, the three-file minimum stays the same:HTML File, Data WebService and Client JavaScript

Thursday, October 22, 2009

Page 9: Server-Side, PHP-Generated AJAX

java pseudo script!var summary = document.getElementById("summary").value;

var status = document.getElementById("status").value;

// etc...

XmlRpcCall("http://server/get_issues?sm=" + summary + "&st=" + status + etc...,

"filterIssuesListCallback");

function filterIssuesListCallback(issueArray) {

var issueTable = document.getElementById("table");

issueTable.removeAllRows();

foreach (issueArray as issueObject) {

var issueRow = new HtmlTableRow();

issueRow.addTableCell(issueObject.id);

issueRow.addTableCell(issueObject.summary);

issueRow.addTableCell(issueObject.assignedTo);

// etc...

issueTable.addRow(issueRow);

};

};

Thursday, October 22, 2009

Page 10: Server-Side, PHP-Generated AJAX

issues?

• Structure of “three files” completely dependent on structure of the data feed (e.g. the fields of an Issue object); HTML File, Data WebService and Client JavaScript all need to understand what an “Issue” is

• Confusion as to where Business Logic or Security would go

Thursday, October 22, 2009

Page 11: Server-Side, PHP-Generated AJAX

client-side ajax

• Code maintenance:3 languages, 4 tiers

• Ripple effect

• Security concerns;Business logic stored on the client

Thursday, October 22, 2009

Page 12: Server-Side, PHP-Generated AJAX

server-side ajax

• PHP generatesclient JavaScript(via traditional MVC approach)

• Code generation is the key

• More secure; Significant ease of maintenance

Thursday, October 22, 2009

Page 13: Server-Side, PHP-Generated AJAX

keep javascript “dum”• JavaScript should do

the absolute bare minimum

• The ECMA “Standard”

• Shout-out to Douglas CrockfordYahoo! Sr. JavaScript Architect

• Minimizes security holes and exposure of business logic

Thursday, October 22, 2009

Page 14: Server-Side, PHP-Generated AJAX

the strategy• Implement a true MVC

approach

• componentize your HTML Controls on the server

• utilize event-driven methodologies

• Add some Ajaxifying “Secret Sauce”

<h3>Please select a country</h3>

<select id="country">

<option>China</option>

<option>Japan</option>

<option>Taiwan</option>

<option>UK</option>

<option>USA</option>

....

</select>

<p>Lorem ipsum dolorem est...</p>

Thursday, October 22, 2009

Page 15: Server-Side, PHP-Generated AJAX

the strategy• Implement a true MVC

approach

• componentize your HTML Controls on the server

• utilize event-driven methodologies

• Add some Ajaxifying “Secret Sauce”

$countrySelect = new Select();

$countrySelect->addOption("China");

$countrySelect->addOption("Japan");

$countrySelect->addOption("Taiwan");

$countrySelect->addOption("UK");

$countrySelect->addOption("US");

<h3>Please select a country</h3>

<?php $countrySelect->render(); ?>

<p>Lorem ipsum dolorem est...</p>

Thursday, October 22, 2009

Page 16: Server-Side, PHP-Generated AJAX

$countrySelect->addChangeAction("countryChanged");

function countryChanged() {

$stateArray = States::LoadArrayForCountry

($countrySelect->selectedValue);

$stateSelect->removeAllOptions();

foreach ($stateArray as $state) {

$stateSelect->addOption($state);

}

}

the strategy• Implement a true MVC

approach

• componentize your HTML Controls on the server

• utilize event-driven methodologies

• Add some Ajaxifying “Secret Sauce”

Thursday, October 22, 2009

Page 17: Server-Side, PHP-Generated AJAX

<?php

/* Listing for address.php */

$form = new Form();

if ($form->isNew()) {

$countrySelect = new Select($form);

$countrySelect->

addChangeAction("countryChanged");

...

$stateSelect = new Select($form);

$stateSelect->enabled = false;

...

}

function countryChanged() {...}

include("address.tpl.php");

?>

event-driven in detail

• Use “Form State”

• Every HTML Control is stored in Form State

• Form State is serialized, and restored on submit

Thursday, October 22, 2009

Page 18: Server-Side, PHP-Generated AJAX

<html><body>

<?php $form->render(); ?>

Country:<br/>

<?php $country->render(); ?>

State:<br/>

<?php $state->render(); ?>

</body></html>

event-driven in detail

• Use “Form State”

• Every HTML Control is stored in Form State

• Form State is serialized, and restored on submit

Thursday, October 22, 2009

Page 19: Server-Side, PHP-Generated AJAX

<html><body>

<form action="address.php">

<input type="hidden" id="formState"

value="abcd1234..."/>

Country:<br/>

<select id="country"

onchange="form.submit();">

...

</select>

State:<br/>

<select id="state">...</select>

</body></html>

event-driven in detail

• Use “Form State”

• Every HTML Control is stored in Form State

• Form State is serialized, and restored on submit

Thursday, October 22, 2009

Page 20: Server-Side, PHP-Generated AJAX

<html><body>

<form action="address.php">

<input type="hidden" id="formState"

value="abcd1234..."/>

Country:<br/>

<select id="country"

onchange="form.submit();">

...

</select>

State:<br/>

<select id="state">...</select>

</body></html>

event-driven in detail

• Use “Form State”

• Every HTML Control is stored in Form State

• Form State is serialized, and restored on submit

<html><body>

<?php $form->render(); ?>

Country:<br/>

<?php $country->render(); ?>

State:<br/>

<?php $state->render(); ?>

</body></html>

Thursday, October 22, 2009

Page 21: Server-Side, PHP-Generated AJAX

<?php

/* Listing for address.php */

$form = new Form();

if ($form->isNew()) {

$countrySelect = new Select($form);

$countrySelect->

addChangeAction("countryChanged");

...

$stateSelect = new Select($form);

$stateSelect->enabled = false;

...

}

function countryChanged() {...}

include("address.tpl.php");

?>

event-driven in detail

• Use “Form State”

• Every HTML Control is stored in Form State

• Form State is serialized, and restored on submit

Thursday, October 22, 2009

Page 22: Server-Side, PHP-Generated AJAX

the strategy

• Implement a true MVC approach

• componentize your HTML Controls

• utilize event-driven methodologies

• Add some Ajaxifying “Secret Sauce”

Thursday, October 22, 2009

Page 23: Server-Side, PHP-Generated AJAX

what’s the “secret sauce”

• HTML wrapper for each AjaxifiedDOM element

<span id="_state">

<select id="state" disabled="disabled">

<option>Select One...</option>

</select>

</span>

Thursday, October 22, 2009

Page 24: Server-Side, PHP-Generated AJAX

<?php

/* Listing for address.php */

$form = new Form();

if ($form->isNew()) {

$countrySelect = new Select($form);

$countrySelect->

addChangeAction("countryChanged");

...

$stateSelect = new Select($form);

$stateSelect->enabled = false;

...

include("address.tpl.php");

}

function countryChanged() {...}

?>

event-driven with mayo

• Make the form smarter

• Form should track which controls have changed and only return the updated HTML for those controls

• Client JavaScript does simple DOM replacements

Thursday, October 22, 2009

Page 25: Server-Side, PHP-Generated AJAX

<html><body>

<form action="address.php">

<input type="hidden" id="formState"

value="abcd1234..."/>

<input type="hidden" id="methodName"

value=""/>

Country:<br/>

<select id="country"

onchange="AjaxCall('countryChanged');">

...

</select>

State:<br/>

<select id="state">...</select>

</body></html>

event-driven with mayo

• Make the form smarter

• Form should track which controls have changed and only return the updated HTML for those controls

• Client JavaScript does simple DOM replacements

Thursday, October 22, 2009

Page 26: Server-Side, PHP-Generated AJAX

function AjaxCall(methodName) {

document.getElementById.methodName.value =

methodName;

XmlRpcCall(document.form.action,

"defaultCallback");

};

function defaultCallback(updatedControlArray) {

foreach (updatedControlArray as updatedControl) {

var control = document.getElementById("_" +

updatedControl.id);

control.innerHTML = updatedControl.html;

};

};

event-driven with mayo

• Make the form smarter

• Form should track which controls have changed and only return the updated HTML for those controls

• Client JavaScript does simple DOM replacements

<updatedControlArray>

<updatedControl id="state">

<!CDATA[

<select id="state">

<option>...</option>

<option>...</option>

</select>

]]>

</updatedControl>

</updatedControlArray>

Thursday, October 22, 2009

Page 27: Server-Side, PHP-Generated AJAX

coding demo

Thursday, October 22, 2009

Page 28: Server-Side, PHP-Generated AJAX

Thursday, October 22, 2009

Page 29: Server-Side, PHP-Generated AJAX

so what about this?

• Now, only one file is dependent on structure of the data feed

Thursday, October 22, 2009

Page 30: Server-Side, PHP-Generated AJAX

qcodo

• Developed in 2001Open Sourced in 2005MIT License

• Code Generation ORMComprehensive MVCServer-Side AJAX

• 120,000+ Downloads1500+ Registered Users

Thursday, October 22, 2009