Last active
December 20, 2015 22:48
-
-
Save dux/6207234 to your computer and use it in GitHub Desktop.
Simple routed pjax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Pjax = | |
skip_on: [], | |
push_state: false, | |
load_is_safe: false, | |
refresh: (func) -> Pjax.load(location.pathname+location.search, { func:func, no_scroll:true }) | |
init: (@full_page=false) -> | |
return alert "#full_page ID referece not defined in PJAX!\n\nWrap whole page in one DIV element" unless @full_page | |
if window.history && window.history.pushState | |
Pjax.push_state = true | |
# u chrome se automatski trigerrira popstate na kraju page rendera, i evo ti novog requesta. disable 1sec pjax load | |
setTimeout -> | |
Pjax.load_is_safe = true | |
, 1000 | |
window.onpopstate = (event, href) -> | |
if event.state && event.state.body | |
console.log "Pjax.load (history cache hit): #{event.state.href}" | |
if /<form/.test(event.state.body) | |
Pjax.load(event.state.href, history:true); | |
else | |
Pjax.replace event.state.title, event.state.body | |
else | |
Pjax.load(levent.state.href, history:true); | |
$(document).trigger 'pjax:get' | |
$(document).trigger 'pjax:get' | |
console.log "Pjax.init() on #{@full_page} OK" | |
skip: -> | |
for el in arguments | |
Pjax.skip_on.push el | |
redirect: (href) -> | |
location.href = href | |
false | |
replace: (title, body) -> | |
try | |
document.title = title | |
$(@full_page).html body | |
$(document).trigger 'pjax:get' | |
load: (href, opts={}) -> | |
return alert "Pjax.init() #full_page ID referece not defined in PJAX!\n\nWrap whole page in one DIV element" unless @full_page | |
return false unless Pjax.load_is_safe | |
return false unless href | |
return if href == '#' | |
return @redirect(href) if /^http/.test(href) | |
return @redirect(href) if /#/.test(href) | |
return @redirect(href) unless Pjax.push_state | |
for el in Pjax.skip_on | |
switch typeof el | |
when 'object' then return @redirect(href) if el.test(href) | |
when 'function' then return @redirect(href) if el(href) | |
else return @redirect(href) if el == href | |
speed = $.now() | |
xhr = $.get(href).done( (data) => | |
console.log "Pjax.load #{if opts.history then '(back trigger)' else ''} #{$.now()-speed}ms: #{href}" | |
console.log xhr.getAllResponseHeaders() | |
if loc = xhr.getResponseHeader('Location') | |
alert loc | |
obj = $ "<div>#{data}</div>" | |
body = obj.find(@full_page).html() || data | |
Pjax.replace obj.find('title').first().html(), body | |
unless opts['history'] | |
if href == location.href | |
window.history.replaceState({ body:body, href:href, title:document.title}, document.title, href) | |
else | |
window.history.pushState({ body:body, href:href, title:document.title}, document.title, href) | |
# Google Analytics support | |
_gaq.push ['_trackPageview'] if window._gaq | |
opts.func() if opts.func | |
unless opts['no_scroll'] | |
window.scrollTo(0, 0) | |
).error (ret) -> | |
Info.error ret.statusText | |
false | |
on_get: (func) -> | |
$(document).on 'pjax:get', -> func() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment