Fix js not reload bug. (not finish)
This commit is contained in:
parent
5fe13fe367
commit
77dd9cc169
|
|
@ -9,6 +9,7 @@ class Coloncancerpredictfields2s
|
|||
AdvanceFields = ["revert_value","map_values","coloncancer_predict_mapping_file2"]
|
||||
TherapyFields = ["variable","name","hint","comment_text","choice_fields","lpv_impact","active_choice","disable_condition"]
|
||||
TherapyOnly = ["lpv_impact","active_choice","disable_condition"]
|
||||
JSFileName = "cc2_tool_js_filename.txt"
|
||||
field :title ,type:String ,default:""
|
||||
field :advance_mode, type: Boolean, default: false
|
||||
field :form_show , :type=> Hash ,default: {
|
||||
|
|
@ -218,6 +219,50 @@ class Coloncancerpredictfields2s
|
|||
self.lpv_calc = get_years_settings_dict
|
||||
self.generate_eval_formula
|
||||
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/#{JSFileName}",'w+'){|f| f.write(new_path)}
|
||||
end
|
||||
end
|
||||
def generate_eval_formula
|
||||
eval_hidden_variables = "def eval_hidden_variables(result); #{self.tmp_hidden_variables_for_ruby}; end"
|
||||
Coloncancerpredict2sController.module_eval(eval_hidden_variables)
|
||||
|
|
@ -343,7 +388,7 @@ class Coloncancerpredictfields2s
|
|||
org_str.insert(st, replace_str)
|
||||
org_str
|
||||
end
|
||||
def auto_write_predict_js
|
||||
def auto_write_predict_js(force_reload=true)
|
||||
js_codes = generate_jscode
|
||||
module_app_path = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s
|
||||
save_path = module_app_path + '/app/assets/javascripts/colon_cancer_predict2.js'
|
||||
|
|
@ -409,6 +454,9 @@ class Coloncancerpredictfields2s
|
|||
end
|
||||
if need_write
|
||||
File.write(save_path,file_texts)
|
||||
reload_js_asset(save_path, force_reload)
|
||||
elsif !(File.exist?("#{Rails.root}/public/#{JSFileName}"))
|
||||
File.open("#{Rails.root}/public/#{JSFileName}.txt",'w+'){|f| f.write(save_path)}
|
||||
end
|
||||
end
|
||||
def get_years_settings_dict
|
||||
|
|
|
|||
Loading…
Reference in New Issue