SlideShare a Scribd company logo
1 of 15
Download to read offline
Hash Signaling Made Easy
   Implementing Hash Signaling on
      Existing Django Projects


            David Gouldin
             @dgouldin
What is hash signaling?

    /foo/bar/ ➛ /baz/

  /#/foo/bar/ ➛ /#/baz/
Why would you do that?!

 • Continuous media playback
 • Transport efficiency
 • Seamless page transitions
 • Page render speed
First Cut Goals

• Use existing views and templates
• No-JS graceful fallback
• SEO friendly
• No impact on DOM structure
Solution
 HTML Comments!
<!-- block foo -->
<div>content here</div>
<!-- endblock foo -->
{% ajaxblock %}
• Subclass of {% block %}
• Emits HTML comments
• Comments include:
 • The name of the block
 • The block’s base template name
 • A hash of the block’s content
{% ajaxblock %}
   my_app/foo.html
      {% ajaxblock foo %}
      <div>content here</div>
      {% endblock %}

                     ↓
<!-- block foo:my_app/foo.html
57a29c950da45a470d382a679b9b4abf -->
<div>content here</div>
<!-- endblock foo:my_app/foo.html -->
But there’s a catch ...

• Javascript is normally developed with the
  intention of a a single page lifespan
• Global state is implicitly unloaded by the
  browser on navigation
• Hash signaling abnormally prolongs the life
  of a page
jQuery.lifecycle.js
A helper plugin to deal with the effects of an
       unnaturally long page lifespan

$.lifecycle.register(
‘a_cycle’, [‘foo’], {
  load: function(cycle) {
     ...
  },
  unload: function(cycle) {
     ...
  }
});
jQuery.lifecycle.js
  Cycles provide utility functions which perform
     symmetric load and unload operations

cycle.bind           !   $.bind
cycle.setTimeout     !   window.setTimeout
cycle.setInterval    !   window.setInterval
cycle.addScript      !   <script></script>
Hijacking page rendering
Hook all events which request a new page
   $('a:not(.no-ajax)').live('click', function(){
     var href = $(this).attr('href');
     window.location.hash = '#' + href;
     return false;
   });


Replace old blocks with new content
   $(window).bind('hashchange', function(){
     $.get(location.hash.substr(1), function(html) {
         $.lifecycle.replaceBlocks(html);
     }
   });
lifecycle.replaceBlocks
• Get all blocks from the old and new
  documents
• Compare block hashes to determine which
  ones need replacement
• Run unloader for all cycles registered in
  blocks to be replaced
• Replace block contents (which registers
  new cycles) and run loaders for replaced
  blocks
Clean URLs
/lorem/ipsum/dolor/sit/amet/#/foo/   BAD

            /a/#/foo/                GOOD
Clean URLs
direct_to_template view from base template
   url(r'^a/$',
   'django.views.generic.simple.direct_to_template',
   {'template': 'base.html'})

redirect to your base view in javascript
   <script>
   var ajaxExemptPaths = ['/a/'];
   if (ajaxExemptPaths.indexOf
   (window.location.pathname) === -1) {
       window.location = '/a/#' +
   window.location.pathname + window.location.search;
   }
   </script>
To be released soon

github.com/dgouldin/django-hashsignal




   (Contingent upon the approval of my employer)

More Related Content

What's hot

Resource Registries: Plone Conference 2014
Resource Registries: Plone Conference 2014Resource Registries: Plone Conference 2014
Resource Registries: Plone Conference 2014Rob Gietema
 
루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날Sukjoon Kim
 
자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)
자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)
자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)DK Lee
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Anatoly Sharifulin
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and ModernizrJussi Pohjolainen
 
Web Tech & Architecture
Web Tech & ArchitectureWeb Tech & Architecture
Web Tech & Architecturedynamis
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...andreaslubbe
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjobMark Greenway
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDartLoc Nguyen
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjobMark Greenway
 
Building Web Apps with Express
Building Web Apps with ExpressBuilding Web Apps with Express
Building Web Apps with ExpressAaron Stannard
 
Responsive Design with WordPress
Responsive Design with WordPressResponsive Design with WordPress
Responsive Design with WordPressJoe Casabona
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.jsWebsecurify
 
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, CroatiaLeave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, CroatiaRobert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloRobert Nyman
 

What's hot (19)

Resource Registries: Plone Conference 2014
Resource Registries: Plone Conference 2014Resource Registries: Plone Conference 2014
Resource Registries: Plone Conference 2014
 
루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날
 
자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)
자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)
자바 웹 개발 시작하기 (1주차 : 웹 어플리케이션 체험 실습)
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
 
Web Tech & Architecture
Web Tech & ArchitectureWeb Tech & Architecture
Web Tech & Architecture
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
 
Apache ofbiz
Apache ofbizApache ofbiz
Apache ofbiz
 
JQuery
JQueryJQuery
JQuery
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjob
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDart
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Cut your hair and get an azure webjob
Cut your hair and get an azure webjobCut your hair and get an azure webjob
Cut your hair and get an azure webjob
 
Building Web Apps with Express
Building Web Apps with ExpressBuilding Web Apps with Express
Building Web Apps with Express
 
Responsive Design with WordPress
Responsive Design with WordPressResponsive Design with WordPress
Responsive Design with WordPress
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.js
 
Vidoop CouchDB Talk
Vidoop CouchDB TalkVidoop CouchDB Talk
Vidoop CouchDB Talk
 
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, CroatiaLeave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 

Similar to Hash Signaling Made Easy

Web Components v1
Web Components v1Web Components v1
Web Components v1Mike Wilcox
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterpriseDave Artz
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patternsStoyan Stefanov
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance PatternsStoyan Stefanov
 
Rich Portlet Development in uPortal
Rich Portlet Development in uPortalRich Portlet Development in uPortal
Rich Portlet Development in uPortalJennifer Bourey
 
Build Your Own CMS with Apache Sling
Build Your Own CMS with Apache SlingBuild Your Own CMS with Apache Sling
Build Your Own CMS with Apache SlingBob Paulin
 
Attractive HTML5~開発者の視点から~
Attractive HTML5~開発者の視点から~Attractive HTML5~開発者の視点から~
Attractive HTML5~開発者の視点から~Sho Ito
 
Web Standards Support in WebKit
Web Standards Support in WebKitWeb Standards Support in WebKit
Web Standards Support in WebKitJoone Hur
 
Web Components With Rails
Web Components With RailsWeb Components With Rails
Web Components With RailsBoris Nadion
 
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0Arun Gupta
 
JavaScript front end performance optimizations
JavaScript front end performance optimizationsJavaScript front end performance optimizations
JavaScript front end performance optimizationsChris Love
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 
Developing High Performance Web Apps
Developing High Performance Web AppsDeveloping High Performance Web Apps
Developing High Performance Web AppsTimothy Fisher
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Thinqloud
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
Persistent mobile JavaScript
Persistent mobile JavaScriptPersistent mobile JavaScript
Persistent mobile JavaScriptYorick Phoenix
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 

Similar to Hash Signaling Made Easy (20)

Web Components v1
Web Components v1Web Components v1
Web Components v1
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patterns
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance Patterns
 
Rich Portlet Development in uPortal
Rich Portlet Development in uPortalRich Portlet Development in uPortal
Rich Portlet Development in uPortal
 
Build Your Own CMS with Apache Sling
Build Your Own CMS with Apache SlingBuild Your Own CMS with Apache Sling
Build Your Own CMS with Apache Sling
 
Attractive HTML5~開発者の視点から~
Attractive HTML5~開発者の視点から~Attractive HTML5~開発者の視点から~
Attractive HTML5~開発者の視点から~
 
Web Standards Support in WebKit
Web Standards Support in WebKitWeb Standards Support in WebKit
Web Standards Support in WebKit
 
Sanjeev ghai 12
Sanjeev ghai 12Sanjeev ghai 12
Sanjeev ghai 12
 
Web Components With Rails
Web Components With RailsWeb Components With Rails
Web Components With Rails
 
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
 
JavaScript front end performance optimizations
JavaScript front end performance optimizationsJavaScript front end performance optimizations
JavaScript front end performance optimizations
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Developing High Performance Web Apps
Developing High Performance Web AppsDeveloping High Performance Web Apps
Developing High Performance Web Apps
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Persistent mobile JavaScript
Persistent mobile JavaScriptPersistent mobile JavaScript
Persistent mobile JavaScript
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 

Hash Signaling Made Easy

  • 1. Hash Signaling Made Easy Implementing Hash Signaling on Existing Django Projects David Gouldin @dgouldin
  • 2. What is hash signaling? /foo/bar/ ➛ /baz/ /#/foo/bar/ ➛ /#/baz/
  • 3. Why would you do that?! • Continuous media playback • Transport efficiency • Seamless page transitions • Page render speed
  • 4. First Cut Goals • Use existing views and templates • No-JS graceful fallback • SEO friendly • No impact on DOM structure
  • 5. Solution HTML Comments! <!-- block foo --> <div>content here</div> <!-- endblock foo -->
  • 6. {% ajaxblock %} • Subclass of {% block %} • Emits HTML comments • Comments include: • The name of the block • The block’s base template name • A hash of the block’s content
  • 7. {% ajaxblock %} my_app/foo.html {% ajaxblock foo %} <div>content here</div> {% endblock %} ↓ <!-- block foo:my_app/foo.html 57a29c950da45a470d382a679b9b4abf --> <div>content here</div> <!-- endblock foo:my_app/foo.html -->
  • 8. But there’s a catch ... • Javascript is normally developed with the intention of a a single page lifespan • Global state is implicitly unloaded by the browser on navigation • Hash signaling abnormally prolongs the life of a page
  • 9. jQuery.lifecycle.js A helper plugin to deal with the effects of an unnaturally long page lifespan $.lifecycle.register( ‘a_cycle’, [‘foo’], { load: function(cycle) { ... }, unload: function(cycle) { ... } });
  • 10. jQuery.lifecycle.js Cycles provide utility functions which perform symmetric load and unload operations cycle.bind ! $.bind cycle.setTimeout ! window.setTimeout cycle.setInterval ! window.setInterval cycle.addScript ! <script></script>
  • 11. Hijacking page rendering Hook all events which request a new page $('a:not(.no-ajax)').live('click', function(){ var href = $(this).attr('href'); window.location.hash = '#' + href; return false; }); Replace old blocks with new content $(window).bind('hashchange', function(){ $.get(location.hash.substr(1), function(html) { $.lifecycle.replaceBlocks(html); } });
  • 12. lifecycle.replaceBlocks • Get all blocks from the old and new documents • Compare block hashes to determine which ones need replacement • Run unloader for all cycles registered in blocks to be replaced • Replace block contents (which registers new cycles) and run loaders for replaced blocks
  • 14. Clean URLs direct_to_template view from base template url(r'^a/$', 'django.views.generic.simple.direct_to_template', {'template': 'base.html'}) redirect to your base view in javascript <script> var ajaxExemptPaths = ['/a/']; if (ajaxExemptPaths.indexOf (window.location.pathname) === -1) { window.location = '/a/#' + window.location.pathname + window.location.search; } </script>
  • 15. To be released soon github.com/dgouldin/django-hashsignal (Contingent upon the approval of my employer)