Skip to content

Instantly share code, notes, and snippets.

@lamp
Created March 11, 2010 16:51
Show Gist options
  • Save lamp/329334 to your computer and use it in GitHub Desktop.
Save lamp/329334 to your computer and use it in GitHub Desktop.
Old:
<a href="/presentations/new">New presentation!!!1</a>
jQuery:
// --------------------------------------------
// in application.js
// --------------------------------------------
function anchorizeLinks() {
var linkset = $("a[href^='/']");
// Reset event bindings
linkset.unbind('.anchorize');
linkset.bind('click.anchorize', runAnchorizedLink);
}
// Page load chain
function runAnchorizedLink(event) {
$(this).blur();
loadView($(this).attr("href"));
return false;
}
function loadView(url) {
// apply anchor to document location e.g. /#/presentations/new
// run request to url appending parameter ?load_view=true
// note: controllers all respond to ?load_view=true by omitting layout
// on successful return:
// run unloadView
// run globalUnloadView
// update document with new content
// run initView
// run globalInitView (anchorizeLinks and other basic functions)
}
function globalUnloadView() {
}
function globalInitView() {
anchorizeLinks();
}
// --------------------------------------------
// in a <script> tag within each template
// --------------------------------------------
// Page boot chain - this is called immediately after a view has been added to the DOM
function initView() {
// do things to make this view work
}
// Page unload chain - this is called immediately before a view is removed from the DOM
// include a function like this at the bottom of each template:
function unloadView() {
// code to unload this view - unbind events etc.
}
// example controller action
class Presentations < Application
def new
@presentation = Presentation.new
if params[:load_view] == "true"
display({
:html=>render(:new, :layout=>false),
:presentation=>@presentation
})
else
display @presentation #regular render
end
end
end
class Application
def layout_for_request
(params[:layout]=="false")? false : "application"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment