Skip to content

Commit

Permalink
DRYing up js a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
markoa committed Jan 12, 2009
1 parent bc1a009 commit fae9f60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
28 changes: 3 additions & 25 deletions app/views/pages/index.html.erb
@@ -1,32 +1,10 @@
<script type='text/javascript'>
function authToken() {
return 'authenticity_token=' + encodeURIComponent(AUTH_TOKEN);
}

document.observe('dom:loaded', function() {
// clear the classic href
$('sortDate').href = '#';
$('sortName').href = '#';

Event.observe('sortDate', 'click', function() {
var link = $('sortDate');
path = <%= "'#{sorted_by_date_pages_path}'" %>;
new Ajax.Request(path,
{ asynchronous: true, evalScripts: true, method: 'get',
parameters: authToken()
});
return false;
});

Event.observe('sortName', 'click', function() {
var link = $('sortName');
path = <%= "'#{sorted_by_name_pages_path}'" %>;
new Ajax.Request(path,
{ asynchronous: true, evalScripts: true, method: 'get',
parameters: authToken()
});
return false;
});
remote_get_link_to(<%= "'#{sorted_by_date_pages_url}'" %>, 'sortDate');
remote_get_link_to(<%= "'#{sorted_by_name_pages_url}'" %>, 'sortName');
});
</script>

Expand All @@ -39,7 +17,7 @@
<br />

<%= link_to 'New page', new_page_path %> |
View sorted by
Sort by
<%= link_to 'Date', sorted_by_date_pages_path, :id => 'sortDate', :class => 'remote' %> or
<%= link_to 'Name', sorted_by_name_pages_path, :id => 'sortName', :class => 'remote' %> |
<%= link_to 'Home', root_path %>
15 changes: 15 additions & 0 deletions public/javascripts/application.js
@@ -1,2 +1,17 @@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function authToken() {
return 'authenticity_token=' + encodeURIComponent(AUTH_TOKEN);
}

function remote_get_link_to(ajaxPath, linkId) {
Event.observe(linkId, 'click', function() {
var link = new Element(linkId);
new Ajax.Request(ajaxPath,
{ asynchronous: true, evalScripts: true, method: 'get',
parameters: authToken()
});
return false;
});
}

0 comments on commit fae9f60

Please sign in to comment.