Sha256: f00f9bf3785128dee327661bcca09d29648eb4b8a96df563691acb2091717871

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Elm
  module Rails
    module Helper
      def elm_embed(module_name, args = {})
        content_tag(:div) do
          component_div + script_tag(module_name, args)
        end
      end

      private

      def component_div
        content_tag(:div) {}
      end

      def script_tag(module_name, args)
        content_tag(:script, raw(<<-HTML), type: "text/javascript")
          (function() {
            var onLoad = function(func) {
              if (window.attachEvent) {
                console.log("attachEvent");
                console.log(yourFunctionName);
                window.attachEvent('onload', yourFunctionName);
              } else {
                if (window.onload) {
                  var curronload = window.onload;
                  var newonload = function(evt) {
                    curronload(evt);
                    func(evt);
                  };
                  window.onload = newonload;
                } else {
                  window.onload = func;
                }
              }
            };

            var currentScript = document.currentScript || (function() {
              var scripts = document.getElementsByTagName("script");
              return scripts[scripts.length - 1];
            })();

            var embedDiv = currentScript.previousSibling;

            onLoad(function() {
              window.#{raw module_name} = #{raw module_name}.embed(embedDiv, #{raw args.to_json});
            });
          })();
        HTML
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
elm-rails-0.4.0 lib/elm/rails/helper.rb
elm-rails-0.2.2 lib/elm/rails/helper.rb
elm-rails-0.2.1 lib/elm/rails/helper.rb
elm-rails-0.2.0 lib/elm/rails/helper.rb