Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何以正确的方式在Rails中运行history.js? 大家好,,_Ruby On Rails_History.js - Fatal编程技术网

Ruby on rails 如何以正确的方式在Rails中运行history.js? 大家好,,

Ruby on rails 如何以正确的方式在Rails中运行history.js? 大家好,,,ruby-on-rails,history.js,Ruby On Rails,History.js,这是我第四次尝试在Rails应用程序中实现history.js。我有一种方法运行得很好,但是代码太难看了。今天我又看了一遍代码,心想:我怎样才能让它变得更好、更简单、更干燥 到目前为止,我所做的(工作还算不错,不是很完美): 将remote:true设置为我的链接 jquery ujs获取js.erb 我的HTML看起来像: <body> <div id="content"> some content with buttons, etc

这是我第四次尝试在Rails应用程序中实现history.js。我有一种方法运行得很好,但是代码太难看了。今天我又看了一遍代码,心想:我怎样才能让它变得更好、更简单、更干燥

到目前为止,我所做的(工作还算不错,不是很完美):
  • remote:true
    设置为我的链接
  • jquery ujs获取
    js.erb
我的HTML看起来像:

<body>
  <div id="content">
    some content with buttons, etc.
  </div>
</body>
然后history.js获取函数并将数据提供给它。因此,它用新生成的代码替换
内容
-div。它还更新了URL。这个代码我必须放在每个(!)
js.erb
文件中

我最后的想法是让它不那么难看:
  • remote:true
    设置为我的链接
  • 当点击一个链接时,它会获取一些
    js.erb
    ,它会替换
    内容
    -div
  • 所有带有
    data remote=“true”
    的链接都将获得一个
    ajax:success
    -handler
  • ajax:success
    上,新URL被推送到history.js
但其中还有一个问题。然后我有JavaScript代码:

$(document).on('ajax:success', 'a[data-remote="true"]', function() { ... });
问题是:
ajax:success
如果我替换链接(应该触发事件)所在的
div
-标记,就永远不会触发

也许有人能解决我的问题…

还是有更好的办法?你试过了吗? 它将是Rails 4中的默认值。

关于:

History.Adapter.bind(window, 'statechange', function() {
    var state = History.getState();
    ...
});
我使用beforeSend事件作为所有远程数据的全局侦听器来更改浏览器历史记录的句柄。 我更喜欢beforeSend,因为我希望链接一点击就改变,不管ajax请求的结果如何

$(document).bind('ajax:beforeSend', function(event,data){
    history.pushState(null, '', event.target.href)
});

这可以解决您的问题,因为事件是在对DOM进行任何修改之前触发的。

我只使用jquery ujs和pushState,popState

请看我的回答:


我已经看过history.js页面很多次了,但从未真正尝试过。我希望看到这个问题得到明确的回答。PJAX只是HTML5。HTML4浏览器只获得普通链接。。。使用history.js,我们可以得到适用于HTML4浏览器的AJAX,但也许PJAX代码才是真正的答案,唯一的区别应该是,PJAX使用history.pushState,而我必须使用history.pushState;)我明天会看一看你有没有找到解决办法?你第一次按照什么教程得出这个结论?谢谢嗨,我没有遵循真正的教程。我只是抓取了所有关于history.js的信息,并将它们组合起来;)对于你的第一个问题:不,仍然没有解决方案:(
$(document).bind('ajax:beforeSend', function(event,data){
    history.pushState(null, '', event.target.href)
});