簡體   English   中英

jQuery文檔准備好不在帶有ActionCable的Rails 5應用程序中觸發

[英]jQuery document ready not firing in a Rails 5 App with ActionCable

我剛剛在生產中部署了一個新的rails 5應用程序,但我無法啟動文檔就緒功能。 應用程序,包括文檔就緒語句中的ActionCable連接和rails控制台輸出在Chrome,Firefox等本地激活。

我的app / assets / javascripts / application.js的內容:

 // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require bootstrap //= require featherlight.min //= require jquery.growl //= require cable //= require base //= require tradewars //= require extensions //= require_tree . // TradeWars.io - application.js file var Mining_Loop_Iterator = false; $(document).ready(function() { console.log("Initializing new TradeWars class..."); TradeWars = new TradeWarsClass(); console.log("Applying click handlers..."); TradeWars.applyClickHandlers(); console.log("Attempting to create WebSocket..."); App.game = App.cable.subscriptions.create({ channel: "GameChannel" }, { connected: function() {}, disconnected: function() {}, received: function(data) { console.log('Received via ActionCable :: ' + data.message.method + ' :'); console.log(data); if (data.message.method == 'disable_mining') { TradeWars.disableMining(data.message); } else if (data.message.method == 'offer_chest') { TradeWars.offerChest(); } else { // Refresh TradeWars.animateDigging(data.message); TradeWars.refresh(data.message); } return true; }, mine: function(clicks_per_second) { return this.perform('mine', { clicks_per_second: clicks_per_second }); } }); TradeWars.miningLoop(); }); 

我也在正在加載的頁面的HTML中定義了內聯:

 <script> $(document).ready(function() { setTimeout(function() { TradeWars.planet_id = 21; }, 5000); }); </script> 

當這最終確實觸發時,我得到一個“ReferenceError:TradeWars未定義”javascript錯誤。

我在rails中啟用了資產管道,並通過命令行預編譯資產:

bin/rails assets:precompile

我也遇到了JavaScript錯誤(在本地開發和生產方面):

Uncaught Error: Bootstrap tooltips require Tether (http://tether.io/)

但我不認為那是它,加上我不想安裝Tether。

任何幫助將非常感激。

與另一位開發人員合作,我們嘗試了各種修復。

以下是我們首先要做的一些事情,以確保Action Cable正常運行。

在production.rb:

config.action_cable.url = "ws://tradewars.io/cable"

在應用程序布局(application.html.erb)中,添加了:

<%= action_cable_meta_tag %>

然后,最終解決問題(我們相信)解決了系繩錯誤!

Uncaught Error: Bootstrap tooltips require Tether (http://tether.io/)

通過將tether-rails gem添加到Gemfile:

gem 'tether-rails'

然后按照此處列出的步驟操作: https//github.com/jakegavin/tether-rails

“Uncaught Error”(tether)消失了,jQuery神奇地開始啟動文檔就緒事件!

天哪,我喜歡這個職業。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM