Hello everybody,
now my fourth attempt to implement history.js in a Rails application. I have one approach that works quite fine, but the code is so ugly. Today I looked at the code again and thought: “How can I do it better, easier, more DRY?”
What I have done so far (and work quite fine, not perfect):
- Install
remote: truein my links - jquery-ujs retrieves
js.erb
My HTML looks like this:
<body>
<div id="content">
some content with buttons, etc.
</div>
</body>
js.erb contains:
History.pushState(
{
func: '$(\'
data: '<%= j(render template: "news/index", formats: [:html]) %>'
},
'test title',
'<%= request.url %>'
);
And then history.js takes this function and gives it the data. Thus, it replaces content-div with the new generated code. And it also updates the URL. I need to put this code in each (!) js.erbFile.
My last thoughts to make it a little less ugly were:
- Install
remote: truein my links js.erb, content -divdata-remote="true" ajax:success -handlerajax:success URL- history.js
. JavaScript:
$(document).on('ajax:success', 'a[data-remote="true"]', function() { ... });
: ajax:success , div -tag, ( ).
, - ...
?