简体   繁体   English

在 js.erb 中渲染 js 文件

[英]Render js file in js.erb

Is it possible to render a javascript file inside a js.erb file?是否可以在 js.erb 文件中呈现 javascript 文件? I have a modal that's being render in my new.js.haml file.我有一个正在我的 new.js.haml 文件中呈现的模式。 However I have some javascript code that needs to run only when that modal gets called.但是,我有一些 javascript 代码需要仅在调用该模态时运行。

new.js.haml新的.js.haml

$('.modal-content').html("#{ j(render partial: 'new') }");

I want to be able to call this file or render this file when new.js.haml is called upload.js当 new.js.haml 被称为 upload.js 时,我希望能够调用此文件或呈现此文件

$ ->
    files = [];

    $("#upload_button").click ->
        $("#file_input").trigger "click";

    $("#file_input").change (event) -> 
        file = event.target.files[0];
        files.push(file);
        uploadfile files.length - 1, file;

    uploadfile = (index, file) ->
        formData = new FormData();
        formData.append "file", file;
        fileitem = "
            <li class='file-item success'>
                <div class='active'>
                    <span class='remove'>×</span>
                    <span>#{file.name}</span>
                </div>
                <div class='lds-ellipsis'><div></div><div></div><div></div><div></div></div>
            </li>
        ";
        $('#filelist').append fileitem;
        setRemove();

    setRemove = ->
        $(".remove").unbind();
        $(".remove").click ->
           index = $(@).parent().parent().index();
           files.splice index, 1;
           $('.file-item').eq(index).remove();

Here is my form file that gets rendered but the upload.js file doesn't get called because it's already loaded on the main page.这是我呈现的表单文件,但没有调用 upload.js 文件,因为它已经加载到主页上。 When the the new form button gets clicked it renders this form in a modal.当新的表单按钮被点击时,它会在一个模态中呈现这个表单。

- @ticket.errors.full_messages.each do |message|
  .error= message
= render 'layouts/standard_flash'
%ZenTicketForm{ title: 'Submit Help Request', :multipart => true, data: { remote: true } }
  .col-xs-12.col-sm-12.col-md-12.col-lg-12{style: 'padding:0;'}
    .col-xs-12.col-sm-12.col-md-12.col-lg-6
      %dl.dl-horizontal
        %dt Type
        %dd.show-more-container
          %ZenSelect#ticket_type{options: ['Questions', 'Incidents', 'Problems', 'Tasks'],
            class: 'select2',
            placeholder: t('activerecord.placeholders.zen_support/ticket.ticket_type') }
    .col-xs-12.col-sm-12.col-md-12.col-lg-12
      %dl.dl-horizontal
        %dt Subject
        %dd.show-more-container
          %input.form-control{ type: 'text',
            name: 'zen_support_ticket[subject]',
            placeholder: t('activerecord.placeholders.zen_support/ticket.subject') }
    .col-xs-12.col-sm-12.col-md-12.col-lg-6
      %dl.dl-horizontal
        %dt Priority
        %dd.show-more-container
          %ZenSelect#priority{ options: ['Low', 'Normal', 'High', 'Urgent'],
            data: { placeholder: "activerecord.placeholders.zen_support/ticket.priority" },
            class: 'select2' }
    .col-xs-12.col-sm-12.col-md-12.col-lg-12
      %dl.dl-horizontal
        %dt Comment
        %dd.show-more-container
          %ZenTextArea#comment{ placeholder: t('activerecord.placeholders.zen_support/ticket.comment'),
            error: @zendesk_ticket.errors&.dig(:comment) }
      %dl.dl-horizontal
        .new-show-less
          %button.fileUpload.btn.btn-default#upload_button{type: "button"}
            Attach File
            %input.upload#file_input{:name => "attachments[]",  multiple: true, :type => "file"}
          %ul#filelist

You can put the JS code directly inside the js.erb file and it'll only be executed when that partial is rendered.您可以将 JS 代码直接放在 js.erb 文件中,它只会在呈现部分时执行。

See more: https://guides.rubyonrails.org/working_with_javascript_in_rails.html查看更多: https : //guides.rubyonrails.org/working_with_javascript_in_rails.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM