Baremetrics / calendar

Date range picker for Baremetrics
MIT License
679 stars 78 forks source link

Use with turbolinks #52

Closed jclusso closed 8 years ago

jclusso commented 8 years ago

I'm trying to use turbolinks in our application and I can't figure out how to make it so I don't get multiple calendar objects. Is there a way to destroy the calendar? I've tried doing like

if window.calendar == undefined
      window.calendar = new Calendar(
        element: $('.daterange--double')
        earliest_date: moment(createdAt)
        latest_date: loadEndDate
        start_date: loadStartDate
        end_date: loadEndDate
        callback: ->
          updateCharts(@start_date, @end_date)
        )

The issue is the Calendar no longer works after I do that...

jclusso commented 8 years ago

I ended up doing this... not sure if it's the best way to go about the problem though. Btw, ignore the tabbing. It's coffeescript and It's just pasting in a little weird

if window.calendar != undefined
      $('.daterange.daterange--double').children().remove()
    window.calendar = new Calendar(
      element: $('.daterange--double')
      earliest_date: moment(@createdAt)
      latest_date: @loadEndDate()
      start_date: @loadStartDate()
      end_date: @loadEndDate()
      callback: (@start_date, @end_date) =>
        @updateChart(@start_date, @end_date)
      )
kalepail commented 8 years ago

@jclusso What I typically do is test for the existence of the calendar object and if it doesn't exist I create it. So rather then destroy it every time you ensure you only create it once. Either way though sounds like you got it working.

jclusso commented 8 years ago

Well checking if it existed stopped it from making a new one, but I think because the events are not bound to the document it doesn't work anymore after a page change.