如何在HAML中将逻辑注入到javascript中?

发布于 2025-01-02 06:16:14 字数 1412 浏览 1 评论 0原文

我正在尝试添加 Mercury 编辑器,在修改它的布局时,它建议在视图布局中添加一些 javascript。显然,以下内容不太有效,但它表达了我试图完成的任务的要点。您需要查看 :javascript 过滤器中的部分,在下面我添加 - 来开始 if 语句以了解我的意思:

  ...
  %body{ :class => "#{controller_name} #{action_name}" }
    :javascript
      var saveUrl = null; // Set to the url that you want to save any given page to.
      var options = {
        saveStyle: null,  // 'form', or 'json' (default json)
        saveMethod: null, // 'POST', or 'PUT', (create, vs. update -- default POST)
        visible: null     // if the interface should start visible or not (default true)
      };

      //<!-- Mix in any configurations provided through Rails.application.config.mercury_config -->
      - if Rails.application.config.respond_to?(:mercury_config)
        jQuery.extend(Mercury.config,
        = Rails.application.config.mercury_config.to_json.html_safe
        );
      - end

      //<!-- Mix in any options for PageEditor provided through Rails.application.config.mercury_page_editor_config -->
      - if Rails.application.config.respond_to?(:mercury_page_editor_config)
        jQuery.extend(options,
        = Rails.application.config.mercury_page_editor_config.to_json.html_safe
        );
      - end

      //<!-- Instantiate the PageEditor -->
      new Mercury.PageEditor(saveUrl, options);
      ...

您能否提供一个示例来说明如何正确地做到这一点?

I'm attempting to add Mercury editor and while modifying the layout for it, it suggests to add some javascript into the view layout. Obviously the following won't quite work, but it expresses the gist of what I'm attempting to accomplish. You'll want to look at the section within the :javascript filter, on down where I add the - to begin the if statement for what I mean:

  ...
  %body{ :class => "#{controller_name} #{action_name}" }
    :javascript
      var saveUrl = null; // Set to the url that you want to save any given page to.
      var options = {
        saveStyle: null,  // 'form', or 'json' (default json)
        saveMethod: null, // 'POST', or 'PUT', (create, vs. update -- default POST)
        visible: null     // if the interface should start visible or not (default true)
      };

      //<!-- Mix in any configurations provided through Rails.application.config.mercury_config -->
      - if Rails.application.config.respond_to?(:mercury_config)
        jQuery.extend(Mercury.config,
        = Rails.application.config.mercury_config.to_json.html_safe
        );
      - end

      //<!-- Mix in any options for PageEditor provided through Rails.application.config.mercury_page_editor_config -->
      - if Rails.application.config.respond_to?(:mercury_page_editor_config)
        jQuery.extend(options,
        = Rails.application.config.mercury_page_editor_config.to_json.html_safe
        );
      - end

      //<!-- Instantiate the PageEditor -->
      new Mercury.PageEditor(saveUrl, options);
      ...

Could you please provide an example of how to do this properly?

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

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

发布评论

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

评论(1

半夏半凉 2025-01-09 06:16:14

您可以将逻辑提取到辅助方法中,然后简单地插入它们的结果。看看吧。

# page_helper.rb

def mercury_config
  if Rails.application.config.respond_to?(:mercury_config)
    "jQuery.extend(Mercury.config,
       #{Rails.application.config.mercury_config.to_json.html_safe}
    );"
  end
end

def mercury_page_editor_config
  if Rails.application.config.respond_to?(:mercury_page_editor_config)
    "jQuery.extend(options,
        #{Rails.application.config.mercury_page_editor_config.to_json.html_safe}
    );"
  end
end

# your_view.html.haml

:javascript
  var saveUrl = null; // Set to the url that you want to save any given page to.
  var options = {
    saveStyle: null,  // 'form', or 'json' (default json)
    saveMethod: null, // 'POST', or 'PUT', (create, vs. update -- default POST)
    visible: null     // if the interface should start visible or not (default true)
  };

  //<!-- Mix in any configurations provided through Rails.application.config.mercury_config -->
  #{mercury_config}

  //<!-- Mix in any options for PageEditor provided through Rails.application.config.mercury_page_editor_config -->
  #{mercury_page_editor_config}

  //<!-- Instantiate the PageEditor -->
  new Mercury.PageEditor(saveUrl, options);

You could extract your logic to helper methods and then simply interpolate their results. Take a look.

# page_helper.rb

def mercury_config
  if Rails.application.config.respond_to?(:mercury_config)
    "jQuery.extend(Mercury.config,
       #{Rails.application.config.mercury_config.to_json.html_safe}
    );"
  end
end

def mercury_page_editor_config
  if Rails.application.config.respond_to?(:mercury_page_editor_config)
    "jQuery.extend(options,
        #{Rails.application.config.mercury_page_editor_config.to_json.html_safe}
    );"
  end
end

# your_view.html.haml

:javascript
  var saveUrl = null; // Set to the url that you want to save any given page to.
  var options = {
    saveStyle: null,  // 'form', or 'json' (default json)
    saveMethod: null, // 'POST', or 'PUT', (create, vs. update -- default POST)
    visible: null     // if the interface should start visible or not (default true)
  };

  //<!-- Mix in any configurations provided through Rails.application.config.mercury_config -->
  #{mercury_config}

  //<!-- Mix in any options for PageEditor provided through Rails.application.config.mercury_page_editor_config -->
  #{mercury_page_editor_config}

  //<!-- Instantiate the PageEditor -->
  new Mercury.PageEditor(saveUrl, options);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文