我最近一直在嘗試為壁球比賽創建一個實時計分系統。我已經設法將 ActionCable 與 Rails 5 一起使用來自動更新頁面上的分數,但我想知道是否可以告訴 Rails 在滿足特定條件時刷新頁面。例如,如果游戲已經結束,則會顯示一個不同的頁面,說明玩家在游戲之間休息。我需要頁面完全刷新才能發生這種情況。在我的數據庫中,布爾值“break”在游戲結束時被標記為 true,然后視圖使用條件 if/else 語句來決定顯示什么。我用來更新分數的代碼附在下面,我在想一些類似的東西然后if data.break == true頁面會自動刷新。// match_channel.js (app/assets/javascripts/channels/match_channel.js)$(function() { $('[data-channel-subscribe="match"]').each(function(index, element) { var $element = $(element), match_id = $element.data('match-id') messageTemplate = $('[data-role="message-template"]'); App.cable.subscriptions.create( { channel: "MatchChannel", match: match_id }, { received: function(data) { var content = messageTemplate.children().clone(true, true); content.find('[data-role="player_score"]').text(data.player_score); content.find('[data-role="opponent_score"]').text(data.opponent_score); content.find('[data-role="server_name"]').text(data.server_name); content.find('[data-role="side"]').text(data.side); $element.append(content); } } ); });});我不知道這種事情是否可能,而且我不太擅長與 Javascript 相關的任何事情,所以我很感激這方面的任何幫助。
我可以使用 ActionCable 來刷新頁面嗎?
拉風的咖菲貓
2023-02-17 11:08:25