javascript - Turbolinks - Showing datetimepicker only on Scheduled posts - hidden by default -
i'm having _form partial in oorder create/edit posts. form has dropdown setting post status, if user sets status scheduled
datetimepicker shows in order select date, otherwise should hidden default (ie when status "draft" or "published").
# app/views/posts/_form.html.haml = simple_form_for ["admin", @post] |f| = f.input :title = f.input :status, collection: ["draft", "published", "scheduled"], selected: status_for(@post) .published-field = f.input :published_at, as: :date_time_picker ... # app/helpers/posts_helper.rb module postshelper def status_for(post) if post.published_at? if post.published_at > time.zone.now "scheduled" else "published" end else "draft" end end end # app/assets/javascripts/posts.coffee changevisibility = (status) -> if status == "scheduled" $(".published-field").show() else $(".published-field").hide() jquery -> changevisibility $("#post_status :selected").text() $("#post_status").on "change", (e) -> changevisibility $(this).find(":selected").text()
the problem comes when user access page first time, status
set draft
default (as expected), datetimepicker visible isn't supposed (remeber? should visible if user selects schedule post). if refresh page @ point when vieweing fomr, datepicker disapears.
the original issue can found here https://gorails.com/episodes/scheduling-posts
this issue caused because of turbolinks. have tried use jquery-turbolinks
gem, didn't manage solve problem. ideas how fix issue?
if want use turbolinks, important remember works not having real page loads @ all, xhr request replaces html within <body>
, while saving old <body>
implement button.
this means no scripts executed, wont document load/ready events, etc. $(handler)
same $.ready(handler) , run on page load once "page's document object model (dom) becomes safe manipulate.".
turbolinks fires turbolinks:load
on document
, use $(document).on('turbolinks:load', handler)
such things.
you need aware of fact kept copy of old <body>
dom , scripts did, memory used that, , other javascript last "page" such timeouts , animation frames, etc. keeps running. , used implement show/hide based on css selector @ load time.
this rails guide has more information, turbolinks library itself.
Comments
Post a Comment