Rails Webpack JS并在Ajax上刷新

pyf*_*l88 6 javascript ruby-on-rails webpack

我有一个在webpack js中呈现的日历模块- app/javascript/packs/application.js

document.addEventListener('DOMContentLoaded', function() {
  var calendarEl = document.getElementById('calendar');

  var calendar = new Calendar(calendarEl, {
    plugins: [ interactionPlugin, dayGridPlugin, timeGridPlugin, listPlugin, momentPlugin ],
    header: {
      left: 'prev,next today',
      center: 'title',
      right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
    },
    defaultDate: '2018-01-12',
    navLinks: true, // can click day/week names to navigate views
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    selectable: true,
    events: '/events.json',
    select: function(info) {
      $.getScript('/events/new', function(){
        $('#event_date_range').val(moment(info.start).format('YYYY-MM-DD HH:mm') + ' - ' + moment(info.end).format('YYYY-MM-DD HH:mm'));
        $('#start_date').val(moment(info.start).format('YYYY-MM-DD HH:mm'));
        $('#end_date').val(moment(info.end).format('YYYY-MM-DD HH:mm'));
      });
    }
  });

  calendar.render();
});
Run Code Online (Sandbox Code Playgroud)

我有一个create动作,想在成功的回调-上重新呈现日历create.js.erb。我怎样才能做到这一点?

jpe*_*erl 1

注意:我假设您使用的是 Rails 6。我还假设您添加format.js到了创建操作中。

忘记吧create.js.erb,你在这里不需要它。

另外,您不应该将代码放入app/javascript/packs/application.js.

该文件中的评论如下:

该文件与该目录中存在的任何其他文件一起由 Webpack 自动编译。我们鼓励您将实际的应用程序逻辑放置在app/javascript 中的相关结构中,并且仅使用这些包文件来引用该代码,以便对其进行编译。

这就是你构建它的方式:

  1. 创建文件夹并在该文件夹内创建一个包含代码的app/javascript/calendar文件:index.js

// import your calendar object (put it in a file calendar.js in the same folder)
import Calendar from './calendar';

document.addEventListener('DOMContentLoaded', function() {
  var calendarEl = document.getElementById('calendar');

  var calendar = new Calendar(calendarEl, {
    plugins: [ interactionPlugin, dayGridPlugin, timeGridPlugin, listPlugin, momentPlugin ],
    header: {
      left: 'prev,next today',
      center: 'title',
      right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
    },
    defaultDate: '2018-01-12',
    navLinks: true, // can click day/week names to navigate views
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    selectable: true,
    events: '/events.json',
    select: function(info) {
      $.getScript('/events/new', function(){
        $('#event_date_range').val(moment(info.start).format('YYYY-MM-DD HH:mm') + ' - ' + moment(info.end).format('YYYY-MM-DD HH:mm'));
        $('#start_date').val(moment(info.start).format('YYYY-MM-DD HH:mm'));
        $('#end_date').val(moment(info.end).format('YYYY-MM-DD HH:mm'));
      });
    }
  });

  // actually, you want to put that addEventListener on your form
  // more on 'ajax:success': https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers
  document.body.addEventListener('ajax:success', function(event) {
    var detail = event.detail;
    var data = detail[0], status = detail[1], xhr = detail[2];

    if (status === 'OK') { // upon success
      // do something
      // re-render the calendar
      calendar.render();
    }
  })

  calendar.render();
});
Run Code Online (Sandbox Code Playgroud)

请注意我添加的部分:

  // actually, you want to put that addEventListener on your form
  // more on 'ajax:success': https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers
  document.body.addEventListener('ajax:success', function(event) {
    var detail = event.detail;
    var data = detail[0], status = detail[1], xhr = detail[2];
    if (status === 'OK') { // upon success
      // do something
      // re-render the calendar
      calendar.render();
    }
  })
Run Code Online (Sandbox Code Playgroud)

接下来,您创建包文件app/javascript/packs/calendar.js,并在其中引用您的模块,如下所示:

// importing calendar module
import '../calendar';
Run Code Online (Sandbox Code Playgroud)

现在 Webpack 将自动编译您的文件。

剩下的就是使用javascript_pack_tag添加脚本标签的帮助器,该脚本标签引用由 webpack 编译的命名包文件:<%= javascript_pack_tag 'calendar' %>。将其添加到视图文件的底部(例如index.html.erb)。

希望这可以帮助。