Fix js not reload bug.

This commit is contained in:
BoHung Chiu 2022-06-17 21:27:04 +08:00
parent f8264413f0
commit 93ef387b68
4 changed files with 72 additions and 5 deletions

View File

@ -148,9 +148,19 @@ class CancerpredictsController < ApplicationController
tags = OrbitHelper.widget_tags tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || [] categories = OrbitHelper.widget_categories || []
@table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt') @table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt')
preidct_js_url = '/assets/cancer_predict.js'
if File.exist?('public/cancer_tool_js_filename.txt')
js_filename = File.read('public/cancer_tool_js_filename.txt')
if js_filename.match(/cancer_predict\.js$/)
asset = Rails.application.assets[js_filename]
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
else
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
end
end
{ {
"cancerpredict" => [], "cancerpredict" => [],
"extras"=>{"table"=> @table_str} "extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url}
} }
end end
def widget def widget
@ -158,9 +168,19 @@ class CancerpredictsController < ApplicationController
tags = OrbitHelper.widget_tags tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || [] categories = OrbitHelper.widget_categories || []
@table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt') @table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt')
preidct_js_url = '/assets/cancer_predict.js'
if File.exist?('public/cancer_tool_js_filename.txt')
js_filename = File.read('public/cancer_tool_js_filename.txt')
if js_filename.match(/cancer_predict\.js$/)
asset = Rails.application.assets[js_filename]
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
else
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
end
end
{ {
"cancerpredict" => [], "cancerpredict" => [],
"extras"=>{"table"=> @table_str} "extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url}
} }
end end
def create_first_field def create_first_field

View File

@ -246,6 +246,50 @@ class Cancerpredictfields
self.lpv_calc = get_years_settings_dict self.lpv_calc = get_years_settings_dict
self.generate_eval_formula self.generate_eval_formula
end end
def reload_any_asset(path, type=nil, force_reload=false)
env = Rails.application.assets
new_env = Sprockets::Environment.new(Rails.root.to_s) do |env|
env.version = ::Rails.env
tmp_path = "#{Rails.application.config.root}/tmp/cache/assets/#{::Rails.env}"
env.cache = ::Sprockets::Cache::FileStore.new(tmp_path)
env.context_class.class_eval do
include ::Sprockets::Rails::Helper
end
end
new_env.config = env.config
stats = env.cached.instance_variable_get(:@stats)
new_path = path.sub(/\.erb$/,'')
if force_reload || (stats && stats[path]) #Need reload asset
if type.nil?
ext = File.extname(new_path)
env.mime_types.each do |t, h|
if h[:extensions].include?(ext)
type = t
break
end
end
end
if type
full_path = 'file://'+path+'?type='+type
uris = env.cached.instance_variable_get(:@uris)
keys = uris.keys.select{|k| k.include?(full_path)}
asset = new_env.load(full_path)
if keys.count != 0
keys.each do |k|
uris[k] = asset
end
end
new_path = new_path.sub(/\.[^.]+$/){|ext| '-' + asset.digest + ext}
File.binwrite(new_path, asset.to_s)
yield new_path if block_given?
end
end
end
def reload_js_asset(path, force_reload=false)
reload_any_asset(path, 'application/javascript', force_reload) do |new_path|
File.open("#{Rails.root}/public/cancer_tool_js_filename.txt",'w+'){|f| f.write(new_path)}
end
end
def generate_eval_formula def generate_eval_formula
eval_hidden_variables = "def eval_hidden_variables(result); #{self.tmp_hidden_variables_for_ruby}; end" eval_hidden_variables = "def eval_hidden_variables(result); #{self.tmp_hidden_variables_for_ruby}; end"
CancerpredictsController.module_eval(eval_hidden_variables) CancerpredictsController.module_eval(eval_hidden_variables)
@ -371,7 +415,7 @@ class Cancerpredictfields
org_str.insert(st, replace_str) org_str.insert(st, replace_str)
org_str org_str
end end
def auto_write_predict_js def auto_write_predict_js(force_reload=true)
js_codes = generate_jscode js_codes = generate_jscode
module_app_path = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s module_app_path = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s
save_path = module_app_path + '/app/assets/javascripts/cancer_predict.js' save_path = module_app_path + '/app/assets/javascripts/cancer_predict.js'
@ -437,6 +481,9 @@ class Cancerpredictfields
end end
if need_write if need_write
File.write(save_path,file_texts) File.write(save_path,file_texts)
reload_js_asset(save_path, force_reload)
elsif !(File.exist?("#{Rails.root}/public/cancer_tool_js_filename.txt"))
File.open("#{Rails.root}/public/cancer_tool_js_filename.txt",'w+'){|f| f.write(save_path)}
end end
end end
def get_years_settings_dict def get_years_settings_dict

View File

@ -1,6 +1,6 @@
<script type="text/javascript" src="/assets/jquery.smartmenus.min.js"></script> <script type="text/javascript" src="/assets/jquery.smartmenus.min.js"></script>
<script type="text/javascript" src="/assets/jquery.smartmenus.bootstrap.min.js"></script> <script type="text/javascript" src="/assets/jquery.smartmenus.bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/cancer_predict.js"></script> <script type="text/javascript" src="{{preidct_js_url}}"></script>
{{table}} {{table}}
<%= stylesheet_link_tag "cancer_predict", media: "screen" %> <%= stylesheet_link_tag "cancer_predict", media: "screen" %>
<%= stylesheet_link_tag "bootstrap.min.print", media: "print" %> <%= stylesheet_link_tag "bootstrap.min.print", media: "print" %>

View File

@ -1,6 +1,6 @@
<script type="text/javascript" src="/assets/jquery.smartmenus.min.js"></script> <script type="text/javascript" src="/assets/jquery.smartmenus.min.js"></script>
<script type="text/javascript" src="/assets/jquery.smartmenus.bootstrap.min.js"></script> <script type="text/javascript" src="/assets/jquery.smartmenus.bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/cancer_predict.js"></script> <script type="text/javascript" src="{{preidct_js_url}}"></script>
{{table}} {{table}}
<%= stylesheet_link_tag "cancer_predict", media: "screen" %> <%= stylesheet_link_tag "cancer_predict", media: "screen" %>
<%= stylesheet_link_tag "bootstrap.min.print", media: "print" %> <%= stylesheet_link_tag "bootstrap.min.print", media: "print" %>