简体   繁体   English

Rails AJAX和Webpacker

[英]Rails AJAX and webpacker

I am using 5.2 with webpacker. 我将5.2与webpacker一起使用。 It's a simple restaurant app with reviews. 这是一个带有评论的简单餐厅应用。

The reviews have a column content and rating. 评论有专栏内容和评分。 In my app/javascript/plugins I run a little script that creates a star rating on the form, which makes the input for rating look like stars,( https://github.com/antennaio/jquery-bar-rating ) which works fine. 在我的app/javascript/plugins我运行了一个小脚本,该脚本在表单上创建了星级评分,这使得评分输入看起来像是星级,( https://github.com/antennaio/jquery-bar-rating )可以正常工作精细。

However, I display the reviews and the review form on the restaurants show page and when submitting do an AJAX request which renders the following js - basically, it appends the new review to the list of reviews for this restaurant and then I refresh the whole form to get rid of the input. 但是,我在餐厅显示页面上显示评论和评论表单,并在提交时执行AJAX请求,该请求呈现以下js-基本上,它将新评论附加到该餐厅的评论列表中,然后刷新整个表单摆脱输入。 If the review does not get saved, I just refresh the form. 如果评论没有保存,我只是刷新表格。

function refreshForm(innerHTML) {
  const newReviewForm = document.getElementById('new_review');
  newReviewForm.innerHTML = innerHTML;
}

function addReview(reviewHTML) {
  const reviews = document.getElementById('reviews');
  reviews.insertAdjacentHTML('beforeend', reviewHTML);
}

<% if @review.errors.any? %>
  refreshForm('<%# j render "reviews/form", restaurant: @restaurant, review: @review %>');
<% else %>
  addReview('<%= j render "reviews/show", review: @review %>');
  refreshForm('<%= j render "reviews/form", restaurant: @restaurant, review: Review.new %>');

<% end %>

This AJAX request also works as intended, but when the simple form gets refreshed, the input for rating is just a normal dropdown and not the stars from the plugin anymore. 这个AJAX请求也可以按预期工作,但是当刷新简单表单时,评分输入只是正常的下拉列表,不再是插件中的星星。 How can I call the script from app/javascript/plugin/init_star.js inside my AJAX js, which is inside the view folder? 如何在view文件夹内的AJAX js中从app/javascript/plugin/init_star.js调用脚本?

I also tried to have the star script in a pack and call the <%= javascript_pack_tag> % only on the restaurants/show or even in the partial rendering the form (and refreshing it). 我还尝试将star脚本放入一个包中,并仅在餐厅/表演上,甚至在部分呈现表单(并刷新表单)时调用<%= javascript_pack_tag> % I need to call the star function again and I don't know how to access it. 我需要再次调用star函数,但我不知道如何访问它。

You could do a few different things. 您可以做一些不同的事情。 The simplest option is likely to duplicate your initialize code a bit and simply reinitialize the stars plugin in refreshForm taking care to destroy the existing one first. 最简单的选项可能会稍微复制您的初始化代码,然后在refreshForm中简单地重新初始化stars插件,请小心先破坏现有的插件。 Something like this (taken from readme, may not be your initialization) 诸如此类(取自自述文件,可能不是您的初始化)

function refreshForm(innerHTML) {
  const newReviewForm = document.getElementById('new_review');
  newReviewForm.innerHTML = innerHTML;
  // destroy the existing one
  $('#example').barrating('destroy');
  // insert your custom initialization here
  $('#example').barrating({
    theme: 'fontawesome-stars'
  });
}

If you want to DRY this up and are looking for architecture advice, I would generally say that I trend towards making rails a pure JSON api backend and handle the frontend code entirely through your pack via webpacker. 如果您想干这个,并在寻找架构建议,我通常会说我倾向于将rails变成纯JSON api后端,并通过webpacker完全通过您的包处理前端代码。

No rendering of JS or html via ajax. 无法通过Ajax呈现JS或html。 This depends a bit on your own preferences and how the app is currently built though. 不过,这取决于您自己的喜好以及应用程序的当前构建方式。 Are you planning mobile apps for example? 例如,您正在计划移动应用程序吗? Going webpacker with a strong separation of an API and a frontend codebase will be helpful. 将Web Packer与API和前端代码库高度隔离会很有帮助。 Is this just a webapp? 这只是一个webapp吗? Maybe drop webpacker and lean into the more traditional Rails-y stuff like Turbolinks and what your are doing here with rendering html via ajax. 也许放下webpacker并倾向于使用诸如Turbolinks之类的更传统的Rails-y东西,以及通过ajax渲染html在这里做什么。

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

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