app/assets/javascripts/seed_tray.js.coffee.erb in seed_tray-0.5.0 vs app/assets/javascripts/seed_tray.js.coffee.erb in seed_tray-0.6.0

- old
+ new

@@ -1,16 +1,18 @@ <% app_name = Rails.application.class.to_s.split('::').first %> class SeedTray constructor: () -> - if window.Turbolinks != undefined - if Turbolinks.EVENTS != undefined - $(window).on "page:change", => @render() - else - $(window).on "turbolinks:load", => @render() + loadEvent = undefined + if window.Turbolinks + loadEvent = if Turbolinks.EVENTS then 'page:change' else 'turbolinks:load'; else - $(@render) + loadEvent = 'DOMContentLoaded' + + document.addEventListener loadEvent, => + @render() + @root = <%= app_name %> # Dynamically delegate render to controller#action specific render methods render: => # Run the code that should run on every page. @@ -19,38 +21,41 @@ # Run the page specific render code @delegate_render() delegate_render: => suppress_console = <%= SeedTray.configuration.suppress_console %> - controller = $("[data-controller]").data("controller") - action = $("[data-action]").data("action") + controller = document.body.getAttribute("data-controller") + action = document.body.getAttribute("data-action") controllerLc = controller.toLowerCase(); actionLc = action.toLowerCase(); if @root[controller] && @render_defined(@root[controller]) @root[controller].render() - $(document).trigger("seedtray:#{controllerLc}:render") + @trigger_event "seedtray:#{controllerLc}:render" else unless suppress_console console.info "Skipped #{@root.name}.#{controller}.render()." if @root[controller] && @root[controller][action] && @render_defined(@root[controller][action]) @root[controller][action].render() - $(document).trigger("seedtray:#{controllerLc}:#{actionLc}:render") + @trigger_event "seedtray:#{controllerLc}:#{actionLc}:render" unless suppress_console console.info "Executed #{@root.name}.#{controller}.#{action}.render()." else unless suppress_console console.info "Skipped #{@root.name}.#{controller}.#{action}.render()." delegate_sitewide_render: => @site_wide_render() - $(document).trigger('seedtray:render') + @trigger_event 'seedtray:render' render_defined: (object) -> object.render != undefined + + trigger_event: (evt) -> + document.dispatchEvent new Event(evt); site_wide_render: -> <%= app_name %>.delegator = new SeedTray()