在rails中,可以設置:remote => true
來遠程調用Web應用程序。然後可以使用.js.erb
來響應以更改元素。我如何在Sinatra中創建這個?我想在資源上做一個遠程刪除,如果成功,更新Dom,否則拋出錯誤。sinatra javascript call
回答
在Rails,使用link_to
幫手結果如下輸出設置:remote => true
:
<a href="#yoururl" data-remote="true">Linktext</a>
此鏈接在rails.js
通過類似觀察:
$(document).on('click.remote', '[data-remote]', function() {
$.ajax({
url: $(this).attr('href'),
type: $(this).data('method') || 'GET',
// and so on...
});
});
它所因此實際上做的是標誌您的HTML
鏈接與data-remote="true"
屬性,然後通過js
處理它。您可以回覆的js.erb
模板然後通過DOM
撤銷。 (我個人認爲這是一個壞主意)。
在西納特拉,你可以做幾乎相同:先用你的鏈接,例如remote
類(提供更快的查找對JS不是一個導軌通過[data-remote]
用途:
<a href="#yoururl" class="remote">Your Link Text</a>
然後結合其通過jQuery遠程類的鏈接:觀察從始發元素與遠程類鐵軌一路點擊事件整個文檔做的:
$(document).on('click.remote', '.remote', function() {
// a similar ajax call to the one rails uses
});
然後,與JSON
迴應和Sinatra::JSON
:
get '/yoururl' do
json :foo => 'bar'
end
做什麼,你在Ajax調用成功處理程序中的數據一樣。或者,你也可以寫在ERB的腳本(這是什麼軌與.js.erb
一樣)迴應:
get '/yoururl' do
erb :"answer.js"
end
還沒有嘗試過,但看起來正確。感謝您挖掘rails.js並找到答案。我不知道該在哪裏尋找。你能告訴我這條線是什麼嗎? on('click.remote','.remote'=> click.remote?我知道.remote的目標是遠程類 – Edward 2013-02-18 18:06:16
'$(document).on('click.remote','.remote',function(){ })'安裝一個觀察者來尋找''click''事件,這些事件源於類'remote'冒泡到'document'的元素。事件類型字符串中的'remote'('click.remote')是一個幫助你能夠使用'off('click.remote')'解除綁定該處理程序。沒有名稱空間,只能解除綁定到文檔的所有單擊事件,這不是很靈活。 – 2013-02-19 06:48:05
- 1. call javascript
- 2. call()和apply()Javascript?
- 3. iframe call parent javascript
- 4. from page * .aspx.cs call javascript function
- 5. javascript textbox call value when value changes
- 6. Sinatra,JavaScript跨域請求JSON
- 7. jquery post call call not works
- 8. Array to string conversion while ajax call from javascript
- 9. 什麼是JavaScript結構`{}`和`call()`?
- 10. 'call'如何在JavaScript中工作?
- 11. Call類
- 12. Call對象屬性
- 13. 在Sinatra幫助程序中生成JavaScript
- 14. mutidimensional數組從javascript/jquery到ruby/sinatra
- 15. Sinatra - 如何在分支中添加javascript
- 16. Sinatra + haml + jQuery,從javascript渲染haml/jquery
- 17. HTTParty崩潰Sinatra
- 18. .call做什麼?
- 19. GParse call BackendService async
- 20. 優化WCF CAll
- 21. CALL在爲
- 22. Can not call praw.helpers
- 23. jQuery Mobile Call Page
- 24. Render-call performance drain
- 25. insertRowsAtIndexPaths Doesnot call cellForRowAtIndexPath
- 26. jquery modular ajax call
- 27. Call second Activity Android
- 28. uibinder onload not call
- 29. Sinatra和MacRuby無效
- 30. Rails 3 vs Sinatra
你可以添加刪除路徑的資源,並用狀態代碼回答。您可以使用客戶端的ajax調用來調用此路由。 – three 2013-02-18 10:13:04