You thought you were building a proof of concept, but then that proof of concept went live. Or you had two weeks to build what should have taken two months. Or the handful of progressive enhancements you threw onto a page to make the user experience a little nicer somehow evolved into an entire single-page app. Whatever the reason, you find yourself with a full-blown application built around click events and a staggering number of plugins you can't even remember downloading. If you could rewrite it, you'd use a framework built with your scenario in mind, but it gets 17 zillion hits a day and there's only one of you and starting from scratch isn't an option. No, what you need is the philosophy of a framework broken into discrete pieces that fit into a one- or two-week release cycle. This talk aims to provide bite-sized strategies you can implement in a short amount of time with minimal disruption to unchain your application from the DOM.
Demo code on github: https://github.com/garann/cyoa
4. you have a problem.
☛ old code
☛ other people’s code
☛ written too fast
☛ scope creep
Sunday, April 17, 2011
5. but..
http://www.flickr.com/photos/vintagechica/5597949576/
Sunday, April 17, 2011
6. your baby is ugly.
http://www.flickr.com/photos/quinnanya/2885102816/in/photostream/
Sunday, April 17, 2011
7. your baby is ugly.
☛ window.everything
☛ data in HTML
☛ all the plugins ever!!
☛ $(...).click(manageState)
☛ jQuery 1.oldAndBusted
Sunday, April 17, 2011
9. rewrite from scratch?
☛ undocumented business logic
☛ new bugs for old bugs
☛ business people will freak out
☛ users will be impatient
☛ how long you got?
Sunday, April 17, 2011
10. code written under
duress is probably what
got you here
Sunday, April 17, 2011
11. why ‘sneaky’
☛ business people see their features
☛ users see bugs fixed
☛ you see something other than the back of a
giant boulder
Sunday, April 17, 2011
12. it’s gonna get dirty
http://www.flickr.com/photos/johnpaulgoguen/3359392738/
Sunday, April 17, 2011
13. it’s gonna get ridiculous
http://www.flickr.com/photos/mhaithaca/447863503/
Sunday, April 17, 2011
15. where you want to be
☛ namespacing
☛ data separate from DOM
☛ not dependent on plugins
☛ state management separate from DOM
☛ jQuery 1.newHotness
Sunday, April 17, 2011
16. (and then you can think
about..)
☛ MVC
☛ AMD
☛ unit tests
☛ event delegation
☛ deferreds
☛ etc.
Sunday, April 17, 2011
17. keep it simple
☛ one release at a time
☛ stick to the plan
☛ don’t get scared
Sunday, April 17, 2011
29. check for inline JS
<a href=”javascript:goToPage(9)”>click here!!1</a>
<script type=”text/javascript”>
if (newpage == “”) document.write(“no new page to load”);
</script>
Sunday, April 17, 2011
30. check external code
// r1: added this namespace
var cyoa = cyoa || {
! ! newpage: "",
! ! ...
! ! typingPause: null
! };
// r1: well it would have been cool, anyway..
var newpage = function() {
! return cyoa.newpage;
};
Sunday, April 17, 2011
31. now you have..
☛ your stuff is isolated
☛ group pieces of app
☛ good overview
Sunday, April 17, 2011
32. release 2
☛ hidden fields
☛ attributes
☛ add new objects at the right level
☛ stay out of display code.. for now
Sunday, April 17, 2011
41. not $.fn.everything
☛ known element type?
☛ known class?
☛ known properties?
☛ known length?
☛ that’s a widget, y’all
Sunday, April 17, 2011
42. now you have..
☛ easy plugin swap/upgrade
☛ app-specific stock of widgets
☛ your junk out of $.fn.*
☛ widgets within relevant state in...
Sunday, April 17, 2011
43. release 4
☛ state objects
☛ steps in arrays
☛ non-linear states: myApp.states[“thisState”]
☛ state functions
☛ init
☛ change state
☛ error
Sunday, April 17, 2011
44. confine state info
// TODO: fewer arguments
cyoa.goToPage = function(pagenum, pagetext, pageimg) {
! // TODO: put this logic someplace else
! if (!pagetext) {
! ! switch (parseInt(pagenum)) {
! ! ! case 0:
! ! ! ! cyoa.goToPage0();
! ! ! ! break;
! ! ! case 1:
! ! ! ! cyoa.goToPage1();
! ! ! ! break;
! ! ! case 2:
! ! ! ! cyoa.goToPage2();
! ! ! ! break;
! ! ! ...
Sunday, April 17, 2011
45. confine state info
// r4: added more structured state management
cyoa.state = {
! init: function() {
! ! var num = window.location.hash.substring(1) || 0;
! ! cyoa.state.goToPage(num);
! },
!
! goToPage: function(pagenum) {
! !
! ! // r4: check that page is valid
! ! pagenum = parseInt(pagenum);
! ! if (pagenum < 0 || pagenum >= cyoa.states.length)
! ! ! return;
! !
! ! cyoa.currentPage = pagenum;
! ! window.location.hash = pagenum;
Sunday, April 17, 2011
47. confine state info
// r4: created array with page/state info
cyoa.states = [
! {page: "title.html"},!
! {page: "whatToDo.html"},!
! {page: "youHaveDied.html", image: "explosion.jpg"},!
! {page: "youHaveDied.html", image: "sisyphus.jpg"},!
! {page: "sellIt.html"}
];
// extra credit: routing info
Sunday, April 17, 2011
48. now you have..
☛ state differences encapsulated
☛ single place to switch state
☛ clear definitions
☛ add states cleanly
☛ add in routing with less risk
Sunday, April 17, 2011
52. pub/sub and get/set
cyoa = {
! ! _currentPage: 0,
! ! // r5: getter and setter for currentPage
! ! get currentPage() { return this._currentPage; },
! ! set currentPage(n) {
! ! ! if (n < 0 || n >= cyoa.states.length) return;
! ! ! this._currentPage = n;
! ! ! cyoa.event.publish("pageChanged");
! ! }
}
...
cyoa.event.subscribe("pageChanged", function(r) {!!
! cyoa.state.goToPage();
});
Sunday, April 17, 2011
53. now you have..
☛ application events vs. DOM events
☛ no manual callback chains
☛ easily add features that observe events
☛ control state through properties
Sunday, April 17, 2011
54. release 6
☛ upgrade
☛ regression test
☛ swap out non-forward-compatible plugins
☛ regression test
☛ maybe roll back (sorries :( )
Sunday, April 17, 2011
56. upgrade and test
! // r6: don't keep binding this every time
! $("div.page_text")
! ! .delegate("div.continue a","click",function(e) {
! ! e.preventDefault();
! ! cyoa.currentPage += 1;
! });
Sunday, April 17, 2011
57. when you can’t upgrade
☛ bugs should be easier to find
☛ fix them
☛ keep trying
☛ ala carte features
Sunday, April 17, 2011
58. but once you do.. !!
☛ dependency management
☛ event delegation
☛ deferreds
☛ unit tests
☛ documentation
☛ ..framework? maybe?
Sunday, April 17, 2011
59. nobody saw a thing
☛ business people: “Oh I thought you finished
that five releases ago?”
☛ users: less “It’s too slow,” more “Why can’t I
make this have polka dots?”
Sunday, April 17, 2011
60. you:
☛ “:D”
☛ can develop faster
☛ can fix easier
☛ can get hit by all the buses you want
Sunday, April 17, 2011
61. hey, since you did such a
super job on that
refactor...
Sunday, April 17, 2011