module Rails::Vue::ViewHelper

Public Instance Methods

vue_component(component_name, data = {}, options = {}) click to toggle source
# File lib/rails-vue/view_helper.rb, line 4
def vue_component(component_name, data = {}, options = {})
  init_vuejs(component_name, data, options)
  init_component(component_name, options)
end

Private Instance Methods

init_component(component_name, options) click to toggle source
# File lib/rails-vue/view_helper.rb, line 10
def init_component(component_name, options)
  options[:id] = component_name if options[:id].nil?
  options.except!(:tag)
  content_tag(:div, "", options)
end
init_vuejs(component_name, data, opt) click to toggle source
# File lib/rails-vue/view_helper.rb, line 16
      def init_vuejs(component_name, data, opt)
        props = data.each_with_object({}) do |(key, _), objs|
          objs[":#{key}"] = key
        end
        html_options = opt.reverse_merge(props)
        html_options.except!(:tag)
        vue_script = <<~VUE_SCRIPT
          $(function () {
            new Vue({
              el: "##{opt.fetch(:id, component_name)}",
              data: function(){
                return #{data.to_json};
              },
              template: '#{content_tag(component_name, "", html_options)}'
            });
          });
        VUE_SCRIPT
        content_for :vue_assets do
          javascript_tag(vue_script)
        end
      end