gpt4 book ai didi

javascript - gem Assets 内的配置不起作用

转载 作者:行者123 更新时间:2023-12-03 12:20:38 25 4
gpt4 key购买 nike

我创建了一个名为 Supportator 的 gem当浏览器版本不受支持时,会发出警告模式。目前它接受配置哈希,您可以在其中传递每个浏览器不支持的最新版本。

代码

这是我的应用程序内的初始化程序config/initializers/supportator.rb

   Supportator.configure({:chrome => 30, :ie => 11})

这是gem的配置文件lib/supportator.rb:

   require "supportator/version"
require "engine"

module Supportator

@config = {
:chrome => 14,
:safari => 5,
:firefox => 3,
:ie => 9,
:opera => 12
}

@valid_config_keys = @config.keys

def self.configure(opts = {})
opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
end

def self.config
@config
end

end

这是触发 gem 模式的 js.erb 文件的一部分 lib/assets/javascripts/supportator.js.erb:

    ...

if (browserName == "Chrome" && majorVersion >= <%= Supportator.config[:chrome] %>)
supported = true;
else if (browserName == "Safari" && majorVersion >= <%= Supportator.config[:safari] %>)
supported = true;
else if (browserName == "Firefox" && majorVersion >= <%= Supportator.config[:firefox] %>)
supported = true;
else if (browserName == "Microsoft Internet Explorer" && majorVersion >= <%= Supportator.config[:ie] %>)
supported = true;
else if (browserName == "Opera" && majorVersion >= <%= Supportator.config[:opera] %>)
supported = true;
else
supported = false;

....

问题

当我看到预编译后的代码时,结果是:

    ...

if (browserName == "Chrome" && majorVersion >= 14)
supported = true;
else if (browserName == "Safari" && majorVersion >= 5)
supported = true;
else if (browserName == "Firefox" && majorVersion >= 3)
supported = true;
else if (browserName == "Microsoft Internet Explorer" && majorVersion >= 9)
supported = true;
else if (browserName == "Opera" && majorVersion >= 12)
supported = true;
else
supported = false;

....

但是当我输入控制台 Supportator.config[:ie] 时,结果是 12。

显然,在 js.erb 的预编译中,配置被忽略。有人知道为什么会发生这种情况吗?

最佳答案

我知道这可能没有帮助,但我无法重现您的错误。当我RAILS_ENV=development bundle exec rake assets:precompile然后rails s

时,你的gem按预期运行

enter image description here

关于javascript - gem Assets 内的配置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com