How to add event sources

情到浓时终转凉″ 提交于 2019-12-11 14:56:41

问题


In my fullcalendar page, I have a (default) event sources area that looks like such:

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

I have a model (and view) folder titled "rentalcars". How would I make the events automatically be pulled from that model? This model has start and end dates.

I tried:

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

which certainly does not work. I looked at arshaw's source, and I can tell how to make static events, but I am looking for dynamic ones: ones that will iterate through existing models.

Any suggestions?


回答1:


Your url parameter needs to return whatever the FullCalendar understands and can parse, so judging by the documentation you will have to make sure that your rentelcars index action returns this particular format:

{
        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
    }
}

So in your rentalcars_controller.rb in the index definition you will have to make sure that you return this format which looks a lot like JSON to me. So, easiest way would be to have this as your JavaScript:

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

And then in your controller you will have something like this:

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


来源:https://stackoverflow.com/questions/18217957/how-to-add-event-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!