如何在 .slim Javascript 函数中使用 rails 条件?
How to use a rails condition within a .slim Javascript function?
在我的 .slim 文件中,我有一个包含 URL 的 javascript 块,这个 URL 需要根据渲染环境(暂存/生产等)而有所不同.这是我第一次使用这种格式,所以我不确定我尝试做的事情在逻辑上是否可行。
我的目标是创建一个三元运算符来检查 Rails 环境,如果是 'Production' 则执行 X,否则执行 Y。
代码如下:
file.slim
javascript:
function zopimChat() {
window.zEmbed || function (e, t) {
var n, o, d, i, s, a = [],
r = document.createElement("iframe");
window.zESettings = {
webWidget: {
offset: {
vertical: "85px"
}
}
};
window.zEmbed = function () {
a.push(arguments)
}, window.zE = window.zE || window.zEmbed, r.src = "javascript:false", r.title = "", r.role = "presentation", (r.frameElement || r).style.cssText = "display: none", d = document.getElementsByTagName("script"), d = d[d.length - 1], d.parentNode.insertBefore(r, d), i = r.contentWindow, s = i.document;
try {
o = s
} catch (c) {
n = document.domain, r.src = 'javascript:var d=document.open();d.domain="' + n + '";void(0);', o = s
}
o.open()._l = function () {
var o = this.createElement("script");
n && (this.domain = n), o.id = "js-iframe-async", o.src = e, this.t = +new Date, this.zendeskHost = t, this.zEQueue = a, this.body.appendChild(o)
}, o.write('<body onload="document._l();">'), o.close()
}
# condition below
("https://assets.zendesk.com/embeddable_framework/main.js",
Rails.env.production? "testwebsite.zendesk.com" : "testwebsite-staging.zendesk.com" );
}
可以使用插值:
javascript:
const railsEnv = "#{Rails.env}";
https://rdoc.info/gems/slim/frames#text-interpolation
你的情况:
("https://assets.zendesk.com/embeddable_framework/main.js",
"#{Rails.env.production? ? "testwebsite.zendesk.com" : "testwebsite-staging.zendesk.com"}" );
在我的 .slim 文件中,我有一个包含 URL 的 javascript 块,这个 URL 需要根据渲染环境(暂存/生产等)而有所不同.这是我第一次使用这种格式,所以我不确定我尝试做的事情在逻辑上是否可行。
我的目标是创建一个三元运算符来检查 Rails 环境,如果是 'Production' 则执行 X,否则执行 Y。
代码如下:
file.slim
javascript:
function zopimChat() {
window.zEmbed || function (e, t) {
var n, o, d, i, s, a = [],
r = document.createElement("iframe");
window.zESettings = {
webWidget: {
offset: {
vertical: "85px"
}
}
};
window.zEmbed = function () {
a.push(arguments)
}, window.zE = window.zE || window.zEmbed, r.src = "javascript:false", r.title = "", r.role = "presentation", (r.frameElement || r).style.cssText = "display: none", d = document.getElementsByTagName("script"), d = d[d.length - 1], d.parentNode.insertBefore(r, d), i = r.contentWindow, s = i.document;
try {
o = s
} catch (c) {
n = document.domain, r.src = 'javascript:var d=document.open();d.domain="' + n + '";void(0);', o = s
}
o.open()._l = function () {
var o = this.createElement("script");
n && (this.domain = n), o.id = "js-iframe-async", o.src = e, this.t = +new Date, this.zendeskHost = t, this.zEQueue = a, this.body.appendChild(o)
}, o.write('<body onload="document._l();">'), o.close()
}
# condition below
("https://assets.zendesk.com/embeddable_framework/main.js",
Rails.env.production? "testwebsite.zendesk.com" : "testwebsite-staging.zendesk.com" );
}
可以使用插值:
javascript:
const railsEnv = "#{Rails.env}";
https://rdoc.info/gems/slim/frames#text-interpolation
你的情况:
("https://assets.zendesk.com/embeddable_framework/main.js",
"#{Rails.env.production? ? "testwebsite.zendesk.com" : "testwebsite-staging.zendesk.com"}" );