gpt4 book ai didi

javascript - Rails webpack js和ajax刷新

转载 作者:行者123 更新时间:2023-12-03 16:40:45 25 4
gpt4 key购买 nike

我有一个用 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();
});

我有一个创建操作,想在成功回调时重新渲染日历 - create.js.erb .我怎样才能做到这一点?

最佳答案

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

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

另外,你不应该把你的代码放在 app/javascript/packs/application.js 中。 .

该文件中的注释如下:

This file is automatically compiled by Webpack, along with any other files present in this directory. You're encouraged to place your actual application logic in a relevant structure within app/javascript and only use these pack files to reference that code so it'll be compiled.



这就是你将如何构建它:
  • 创建文件夹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();
    });

    注意我添加的部分:
      // 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();
    }
    })

    接下来,创建您的包文件 app/javascript/packs/calendar.js在其中,您只需像这样引用您的模块:
    // importing calendar module
    import '../calendar';

    现在 Webpack 会自动编译你的文件。

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

    希望这可以帮助。

    关于javascript - Rails webpack js和ajax刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58075213/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com