-
CASSIS
-
2008
-
Bulletproof AJAX
-
more responsive =
better UX
-
href="#"
breaks the web
-
-
-
writing code twice:
JavaScript & PHP
-
DRY principle
(Don't Repeat Yourself)
-
tired of rewriting
in different languages
-
can we pick a winner?
-
has JavaScript (JS) won?
(yet)
-
most # of programmers
-
most # of companies optimizing it
-
JS on client & server
= write logic once
-
JS subset that works
on client & server?
-
- Client
- And
- Server
- Scripting
- Implementation
- Subset
-
avoid unintentionally enabling SkyNet
-
JS on the server
not supported by default
-
check out Node.js
on web host or install?
-
otherwise,
all web hosts run PHP
-
intersection of JS & PHP?
-
2009
-
falsy values research
-
false 0 "0" "00"
-
function js() {
return ("00"==false);
}
-
runtime switching good
need: parsetime switch
-
-
-
principle of separation of script and markup
-
hypertext script docs
external JS with HTML
-
<!-- /*-->
markup ...
<!--*/ //-->
script
//<!--
/*-->
more markup
<!--*/ //-->
-
but for JS and PHP files
-
back to 2009
-
<!-- <?php // -->
/* cassis.js v0 file */
/* both JS and PHP */
// ?> -->
-
<!-- HTML include -->
<script src="cassis.js"></script>
-
/* PHP include */
ob_start();
include 'cassis.js';
ob_end_clean();
-
challenge:
variable names
-
PHP variables
must start with $
-
jQuery popularized
$ in JS
-
CASSIS convention:
$ variables e.g. $x
-
challenge: string concat
'a'+'b'
vs 'a'.'b'
-
common function instead
-
function strcat($s1, $s2) {
if (js()) {
return $s1 + $s2;
}
else {
return $s1 . $s2;
}
}
-
challenge: PHP-only code
date_default_timezone_set("UTC");
-
need: PHP-only block
-
<!-- <?php // -->
/* cassis.js v0 file */
/* both JS and PHP */
// ?> -->
-
/* <!-- <?php // -->
echo 'PHP only';
// */
/* cassis.js v0 file */
/* both JS and PHP */
// ?> -->
-
challenge: JS-only code
e.g. PHP function equivalents
-
function strlen(s) {
return s.length;
}
function ord(s) {
return s.charCodeAt(0);
}
-
need JS and/or PHP
-
comment inverter
/*/
-
stateless comment closer
/**/
-
/* <!-- <?php // -->
echo 'PHP only';
// */
/* both JS and PHP */
// ?> -->
-
/* <!-- <?php // -->
echo 'PHP only';
/*/
alert("JS only");
/**/
/* both JS and PHP */
// ?> -->
-
2011
-
-
challenge:
variable scope
-
function strcat($s1, $s2) {
if (js()) {
return $s1 + $s2;
}
else {
return $s1 . $s2;
}
}
-
strcat vs operators:
any number of strings
-
$s1 + $s2 + $s3 + ... // JS
$s1 . $s2 . $s3 . ... // PHP
strcat($s1, $s2, $s3, ...);
-
CASSIS v0 strcat
function strcat() {
$r = "";
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
block scope in PHP
globals in JS :(
-
want:
function scope PHP&JS
-
CASSIS v0 strcat
function strcat() {
$r = "";
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
CASSIS strcat with var
function strcat() {
var $a, $i, $r;
$r = "";
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
CASSIS strcat with var
function strcat() {
var $a, $i, $r; // PHP syntax error :(
$r = "";
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
need:
hide "var" from PHP
-
// ?> <!--
/* JS only code */
// --> <?php
-
CASSIS strcat with var
function strcat() {
var $a, $i, $r;
$r = "";
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
CASSIS strcat with JS-only var
function strcat() { // ?> <!--
var $a, $i, $r; // --> <?php
$r = "";
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
CASSIS v0.1 strcat
function strcat() { /// ?> <!-- ///
var $a, $i, $r; /// --> <?php ///
$r = ""; $i = 0;
$a = js() ? arguments : func_get_args();
for ($i=count($a)-1; $i>=0; $i-=1) {
$r = js() ? $a[$i] + $r : $a[$i] . $r;
}
return $r;
}
-
CASSIS function scope variables
var
first thing in function
- hide
var
declaration from PHP
- initialize all variables next
- function scoped variables in JS & PHP!
-
good: no globals!
can we do better?
-
JavaScript: The Good Parts
-
-
CASSIS functions now
pass JSLint* & JSHint
-
Releasing Today:
CASSIS v0.1a
-
What's CASSIS good for?
- functional programming
- math, datetime computations
- string processing/parsing
- form validation routines
-
I'm using CASSIS:
-
Get Started With CASSIS
-
Get Started With CASSIS
-
Writing CASSIS code
-
Writing new CASSIS core functions
- if PHP has it, reimplement JS-only
- else if C has it, reimplement as CASSIS
- else try other re-use (HyperTalk)
- underscore_separated_words
- check JSLint & JSHint
- contribute!
-
Contribute to CASSIS
-
JS: present and future
of web programming
-
Until JS is ubiquitous,
CASSIS is a step forward.
-