大家好,
現在我第四次嘗試在Rails應用中實現history.js。我有一種方法運行得很好,但代碼太難看了。今天我又看了一遍代碼並思考:我怎樣才能讓這個更好,更容易,更幹??如何以正確的方式在rails中執行history.js?
我迄今所做的(和工作相當好,不完美):
- 設置
remote: true
到我的鏈接 - jQuery的UJS取的
js.erb
我的HTML看起來像:
<body>
<div id="content">
some content with buttons, etc.
</div>
</body>
該js.erb
包含:
History.pushState(
{
func: '$(\'#content\').html(data);',
data: '<%= j(render template: "news/index", formats: [:html]) %>'
},
'test title',
'<%= request.url %>'
);
然後history.js需要的功能,並賦予它的數據。所以它用新生成的代碼替換content
-div。並且它也更新了URL。這個代碼我必須放在每個(!)js.erb
文件中。
我最後的想法,使其少一點醜陋分別爲:
- 設置
remote: true
到我的鏈接 - 當一個鏈接被點擊它提取一些
js.erb
它取代了content
-div - 所有鏈接與
data-remote="true"
將得到ajax:success
處理程序 - 在
ajax:success
新的URL被推送到history.js
但內部仍然存在一個問題。然後,我有JavaScript代碼:
$(document).on('ajax:success', 'a[data-remote="true"]', function() { ... });
的問題是:ajax:success
永遠不會觸發如果我更換div
-tag其中鏈路(即應觸發事件)是
也許有人可以解決我的問題...
你見過jquery.pjax? – 2012-04-10 21:33:54
我已經看了很多次histoy.js頁面,但從未嘗試過嘗試實際嘗試它。我很想看到這個問題以一種明確的方式回答。 – Ashitaka 2012-04-10 21:48:30
PJAX僅適用於HTML5。 HTML4瀏覽器只是得到正常的鏈接...隨着history.js我們也得到AJAX的HTML4瀏覽器 – 2012-04-11 05:07:48