从 ROR 控制器 ruby​​ 调用 javascript 函数

发布于 2024-12-20 14:12:20 字数 2191 浏览 1 评论 0 原文

我有一个用 javascript 编写的函数,我想在 ruby​​ on Rails 控制器(.rb)中调用(使用)该函数。该函数动态创建并使用 optgroup 填充选择。

我如何将其转换为 Ruby 代码?我的问题是寻找像 document.createElement("optgroup"); 这样的东西的替代品,

这是 javascript 代码:

enter function nbFct() {
var clipLists = ["Default", "Recent", "Sticky lists"];
var option1 = ["a", "b"];
var option2 = ["a", "x", "c"];
var option3 = ["f", "e", "c", "d"];
var listType = [];
var optionType = [];
for (var i = 0; i < clipLists.length; i++) {
// create dynamic optgroup from clipLists
listType[i] = document.createElement("optgroup");
listType[i].label = clipLists[i];   
//  alert(listType[i].label) ;
if(listType[i].label ==  "Default"){
    for (var j = 0; j < option3.length; j++) {
// create options and attach to optgroups
    optionType[j] = document.createElement("option");
    optionType[j].value = option3[j];
//      alert(optionType[j].value) ;
    optionType[j].appendChild(document.createTextNode(option3[j]));
    listType[i].appendChild(optionType[j]); 
    }
}
else if(listType[i].label ==  "Recent"){
    for (var j = 0; j < option2.length; j++) {
// create options and attach to optgroups
    optionType[j] = document.createElement("option");
    optionType[j].value = option2[j];
//      alert(optionType[j].value) ;
    optionType[j].appendChild(document.createTextNode(option2[j]));
    listType[i].appendChild(optionType[j]); 
    }
   }
   else{
      for (var j = 0; j < option1.length; j++) {
  // create options and attach to optgroups
        optionType[j] = document.createElement("option");
        optionType[j].value = option1[j];
  //            alert(optionType[j].value) ;
        optionType[j].appendChild(document.createTextNode(option1[j]));
        listType[i].appendChild(optionType[j]); 
      }
    }
  }
 // set the default
 optionType[1].selected = true;
 // clear select menu and append optgroups
 var selectMenu = document.getElementById("clipListOption");
while (selectMenu.hasChildNodes()) {
selectMenu.removeChild(selectMenu.firstChild);
}
 for (var i = 0; i < clipLists.length; i++) {
    if (listType[i].hasChildNodes()) { selectMenu.appendChild(listType[i]); }
  }
} 

I have a function written in javascript that i would like to call (use) in my ruby on rails controller(.rb). The function dynamically creates and populates a select with optgroup

How would I go about translating this into Ruby code? My problem was finding a replacement for stuff like document.createElement("optgroup");

Here's the javascript code:

enter function nbFct() {
var clipLists = ["Default", "Recent", "Sticky lists"];
var option1 = ["a", "b"];
var option2 = ["a", "x", "c"];
var option3 = ["f", "e", "c", "d"];
var listType = [];
var optionType = [];
for (var i = 0; i < clipLists.length; i++) {
// create dynamic optgroup from clipLists
listType[i] = document.createElement("optgroup");
listType[i].label = clipLists[i];   
//  alert(listType[i].label) ;
if(listType[i].label ==  "Default"){
    for (var j = 0; j < option3.length; j++) {
// create options and attach to optgroups
    optionType[j] = document.createElement("option");
    optionType[j].value = option3[j];
//      alert(optionType[j].value) ;
    optionType[j].appendChild(document.createTextNode(option3[j]));
    listType[i].appendChild(optionType[j]); 
    }
}
else if(listType[i].label ==  "Recent"){
    for (var j = 0; j < option2.length; j++) {
// create options and attach to optgroups
    optionType[j] = document.createElement("option");
    optionType[j].value = option2[j];
//      alert(optionType[j].value) ;
    optionType[j].appendChild(document.createTextNode(option2[j]));
    listType[i].appendChild(optionType[j]); 
    }
   }
   else{
      for (var j = 0; j < option1.length; j++) {
  // create options and attach to optgroups
        optionType[j] = document.createElement("option");
        optionType[j].value = option1[j];
  //            alert(optionType[j].value) ;
        optionType[j].appendChild(document.createTextNode(option1[j]));
        listType[i].appendChild(optionType[j]); 
      }
    }
  }
 // set the default
 optionType[1].selected = true;
 // clear select menu and append optgroups
 var selectMenu = document.getElementById("clipListOption");
while (selectMenu.hasChildNodes()) {
selectMenu.removeChild(selectMenu.firstChild);
}
 for (var i = 0; i < clipLists.length; i++) {
    if (listType[i].hasChildNodes()) { selectMenu.appendChild(listType[i]); }
  }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-12-27 14:12:20

除非通过一些极其痛苦的黑客攻击,否则你不可能轻易做到这一点。在 Rails 中,控制器无法看到视图内部。即使可以,它也无法从中提取 Javascript 并以某种方式在其自己的本地绑定上下文中运行它。即使可以,您也不想这样做,因为这严重违反了 MVC 模式。

如果您绝对想这样做,可能会涉及以下组合:

  • 从客户端,POST 包含 JS 函数的字符串。

  • 评估并解析 JS。

  • 将 JS 转换为 Ruby 字符串。

  • 在控制器的本地上下文中计算 Ruby 字符串。

这是很多工作。相反,模型应该在这里完成繁重的工作,并依靠控制器为其提供指令,然后将其输出传递给视图。

You can't do that easily, except through some exceptionally tortured hacking. In Rails, the controller can't see inside the view. Even if it could, it can't extract Javascript from it and somehow run that in its own local binding context. And even if it could, you wouldn't want to anyway since that's an egregious violation of the MVC pattern.

If you absolutely wanted to do this, it would probably involve a combination of:

  • From the client, POST the string comprising the JS function.

  • Evaluate and parse the JS.

  • Transform the JS into a Ruby string.

  • Evaluate the Ruby string in the controller's local context.

That's a lot of work. Instead, a model should be doing the heavy lifting here, and relying on the controller to give it instructions, whose output is then passed to a view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文