Created
April 13, 2009 04:50
-
-
Save btakita/94270 to your computer and use it in GitHub Desktop.
Page-specific js
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
class ApplicationController < ActionController::Base | |
protected | |
def update_page(callback_base_name = @action_name, &block) | |
callback_name = "on_#{callback_base_name}" | |
render :update do |page| | |
args = block ? instance_eval(&block) : [] | |
args = [args] unless args.is_a?(Array) | |
page << "page.#{callback_name}.apply(page, #{args.to_json})" | |
end | |
end | |
end |
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
module ApplicationHelper | |
def init_specific_javascript | |
javascript_tag("Pages.activatePage('#{@controller.controller_path.camelcase}', '#{@controller.action_name}')") | |
end | |
end |
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
var Pages = new Object(); | |
Pages.all = {}; | |
Pages.add = function(name, methods) { | |
var page = new Pages.Page(name); | |
$.extend(page, methods); | |
return page; | |
} | |
var page; | |
Pages.activatePage = function(controllerName, actionName) { | |
page = Pages.all[controllerName]; | |
if(page) { | |
$(document).ready(function() { | |
if(page.all) page.all(); | |
if(page[actionName]) page[actionName].call(page); | |
}); | |
} | |
} | |
Pages.Page = function(name) { | |
this.name = name; | |
Pages.all[name] = this; | |
} |
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
Pages.add("Users", { | |
all: function() { | |
// Run this onload for all pages in the UsersController | |
}, | |
show: function() { | |
// Run this onload for the UsersController#show action | |
}, | |
on_show: function() { | |
// The UsersController#show xhr response runs this | |
} | |
}); |
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
class UsersController < ActionController::Base | |
def show | |
@user = User.find(params[:id]) | |
respond_to do |format| | |
format.html | |
format.js do | |
update_page { render(:partial => 'users/show') } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment