-
-
Save scottcorgan/b35cb59f21acd392d07020a700d017b7 to your computer and use it in GitHub Desktop.
Revisions
-
benoitongit revised this gist
Dec 19, 2016 . No changes.There are no files selected for viewing
-
benoitongit revised this gist
Dec 19, 2016 . No changes.There are no files selected for viewing
-
benoitongit revised this gist
Dec 19, 2016 . No changes.There are no files selected for viewing
-
benoitongit revised this gist
Dec 19, 2016 . No changes.There are no files selected for viewing
-
benoitongit revised this gist
Dec 19, 2016 . No changes.There are no files selected for viewing
-
benoitongit revised this gist
Dec 19, 2016 . No changes.There are no files selected for viewing
-
benoitongit revised this gist
Dec 19, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # Vue adapter to mount/destroy components VueAdapter = init: -> @vueModels = [] -
benoitongit renamed this gist
Dec 19, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
benoitongit renamed this gist
Dec 19, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
benoitongit created this gist
Dec 19, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ <%= vue_component 'MyComponent', my_prop: 'awesome prop!' %> <!-- Final Outpout: <div data-vue-component="MyComponent" data-vue-props="{"my-prop":"awesome prop!"}"> <div>A custom component with awesome prop!</div> </div> --> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ @MyComponent = props: ['myProp'] template: '<div>A custom component with {{myProp}}</div>' This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ VueAdapter = init: -> @vueModels = [] vueComponents = document.querySelectorAll('[data-vue-component]') return if vueComponents.length <= 0 self = this for i in [0...vueComponents.length] self.mountComponent(vueComponents[i]) mountComponent: (component) -> name = component.getAttribute('data-vue-component') props = JSON.parse(component.getAttribute('data-vue-props')) if typeof window[name] == 'object' vm = @newVueInstance(name, props) component.innerHTML = "" component.appendChild(vm.$el) @vueModels.push(vm) newVueInstance: (name, props) -> element = document.createElement(name) for key, value of props element.setAttribute(key, value) new Vue({ template: element.outerHTML components: { "#{name.toLowerCase()}": window[name] } }).$mount() destroy: -> for vm in @vueModels for child in vm.$children child.$destroy() vm.$destroy() @vueModels = [] document.addEventListener 'turbolinks:load', -> VueAdapter.init() document.addEventListener 'turbolinks:before-cache', -> VueAdapter.destroy() This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ module VueHelper def vue_component(component_name, props = {}, html_options = {}) html_options = html_options.reverse_merge(data: {}) props = VueHelper.dasherize_props(props) html_options[:data].tap do |data| data[:vue_component] = component_name data[:vue_props] = props.to_json end html_tag = html_options[:tag] || :div html_options.except!(:tag) content_tag(html_tag, '', html_options) end def self.dasherize_props(props) case props when Hash props.each_with_object({}) do |(key, value), new_props| new_key = key.to_s.dasherize new_value = VueHelper.dasherize_props(value) new_props[new_key] = new_value end when Array props.map { |item| VueHelper.dasherize_props(item) } else props end end end