【问题标题】:How to add event sources如何添加事件源
【发布时间】:2013-08-13 19:49:53
【问题描述】:

在我的完整日历页面中,我有一个(默认)事件源区域,如下所示:

eventSources: [{
    url: '/events/index/',
    color: 'yellow',
    textColor: 'black',
    ignoreTimezone: false
}],

我有一个名为 "rentalcars" 的模型(和视图)文件夹。我将如何使事件自动从该模型中提取?此模型具有开始日期和结束日期。

我试过了:

eventSources: [{
    url: '/rentalcars/index/',
    color: 'yellow',
    textColor: 'black',
    ignoreTimezone: false
}],

这当然行不通。我查看了 arshaw 的源代码,我知道如何制作静态事件,但我正在寻找动态事件:将迭代现有模型的事件。

有什么建议吗?

【问题讨论】:

    标签: ruby ruby-on-rails-3 calendar fullcalendar


    【解决方案1】:

    您的 url 参数需要返回 FullCalendar 可以理解和解析的任何内容,因此根据 documentation 判断,您必须确保您的 rentelcars 索引操作返回此特定格式:

    {
            title  : 'event1',
            start  : '2010-01-01'
        },
        {
            title  : 'event2',
            start  : '2010-01-05',
            end    : '2010-01-07'
        },
        {
            title  : 'event3',
            start  : '2010-01-09 12:30:00',
            allDay : false // will make the time show
        }
    }
    

    因此,在index 定义中的rentalcars_controller.rb 中,您必须确保返回的格式在我看来很像JSON。因此,最简单的方法是将其作为您的 JavaScript:

    eventSources: [{
      url: '/rentalcars.json',
      color: 'yellow',
      textColor: 'black',
      ignoreTimezone: false
    }],
    

    然后在你的控制器中你会得到这样的东西:

    def index
        rentalcar_dates = RentelcarDates.all # assuming this is an object that holds start, end date and maybe the car name
    
        respond_to do |format|
            format.html
            format.json { render :json => rentalcar_dates }
        end
    end
    

    【讨论】:

    • 目前出租汽车有很多属性,开始和结束日期只是其中两个。我是否必须创建另一个名为 RentalCarDates 的表,其列名与“title,start,end..”完全匹配?或者我可以以某种方式取这两列吗?
    • 是的,你可以像Rentelcar.all.map { |r| {:title => r.title, :start => r.start, :end => r.end} }这样简单地取任何列
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多