Ben*_*n M 5 ruby-on-rails history.js
它现在是我第四次尝试在Rails应用程序中实现history.js.我有一种方法运行得很好,但代码是如此丑陋.今天我再次查看代码并思考:如何让这更好,更容易,更干?!
remote: true为我的链接js.erb我的HTML看起来像:
<body>
<div id="content">
some content with buttons, etc.
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
该js.erb包含:
History.pushState(
{
func: '$(\'#content\').html(data);',
data: '<%= j(render template: "news/index", formats: [:html]) %>'
},
'test title',
'<%= request.url %>'
);
Run Code Online (Sandbox Code Playgroud)
然后history.js接受函数并为其提供数据.所以它用content新生成的代码替换-div.它还会更新URL.这段代码我必须放在每个(!)js.erb文件中.
remote: true为我的链接js.erb替换content-div的链接data-remote="true"都将获得ajax:success-handlerajax:success新URL上被推送到history.js但是内部仍然存在一个问题.然后我有JavaScript代码:
$(document).on('ajax:success', 'a[data-remote="true"]', function() { ... });
Run Code Online (Sandbox Code Playgroud)
问题是:ajax:success如果我替换div了链接(应触发事件)所在的-tag,则永远不会触发.
也许有人可以解决我的问题......