Custom Vue.js Components
In order to equip a Ruby component with some JavaScript, we associate the Ruby component with a Vue.js JavaScript component. The Ruby component therefore needs to inherit from Matestack::Ui::VueJsComponent. Matestack will then render a HTML component tag with some special attributes and props around the response defined in the Ruby component. The Vue.js JavaScript component (defined in a separate JavaScript file and managed via Webpacker) will treat the response of the Ruby component as its template.

Structure, files and registry

A Vue.js component is defined by two files. A Ruby file and a JavaScript file:

Vue.js Ruby component

Within the Ruby file, the Ruby class inherits from Matestack::Ui::VueJsComponent:
app/matestack/components/some_component.rb
1
class SomeComponent < Matestack::Ui::VueJsComponent
2
3
vue_name "some-component"
4
5
def response
6
div class: "some-root-element" do
7
plain "hello {{ foo }}!"
8
end
9
end
10
11
end
Copied!
Following the rule of Vue.js, the response of the component has to consist of exactly one root element! Disregarding this rule will lead to Vue.js errors in the browser.

Vue.js JavaScript component

The Vue.js JavaScript component is defined in a separate JavaScript file:
app/matestack/components/some_component.js
1
import Vue from "vue/dist/vue.esm";
2
import MatestackUiCore from "matestack-ui-core";
3
4
Vue.component('some-component', {
5
mixins: [MatestackUiCore.componentMixin],
6
data() {
7
return {
8
foo: "bar"
9
};
10
},
11
mounted(){
12
console.log(this.props['foo'])
13
}
14
});
Copied!
It can be placed anywhere in your apps folder structure, but we recommend to put it right next to the Ruby component file.
The Vue.js JavaScript file needs to be imported by some kind of JavaScript package manager. We recommendWebpacker
For Webpacker it would look like this:
1
// app/javascript/packs/application.js
2
import MatestackUiCore from 'matestack-ui-core'
3
import '../../../app/matestack/components/some_component'
Copied!
If setup correctly, Matestack will render the component to:
1
<component is='some-component' :props='{}' :params='{}' inline-template>
2
<div class="some-root-element">
3
hello {{ foo }}!
4
</div>
5
</component>
Copied!
As you can see, the component tag is referencing the Vue.js JavaScript component via is and tells the JavaScript component that it should use the inner html (coming from the response method) as the inline-template of the component.
{{ foo }} will be evaluated to "bar" as soon as Vue.js has booted and mounted the component in the browser.
Matestack will inject JSON objects into the Vue.js JavaScript component through the props and params tags if either props or params are available. This data is injected once on initial server side rendering of the component's markup. See below, how you can pass in data to the Vue.js JavaScript component.

Vue.js Ruby component API

Same as component

The basic Vue.js Ruby component API is the same as described within the component API documenation. The options below extend this API.

Referencing the Vue.js JavaScript component

As seen above, the Vue.js JavaScript component name has to be referenced in the Vue.js Ruby component using the vue_js_component_name class method
app/matestack/components/some_component.rb
1
class SomeComponent < Matestack::Ui::VueJsComponent
2
3
vue_name "some-component"
4
5
#...
6
end
Copied!

Passing data to the Vue.js JavaScript component

Like seen above, matestack renders a component-config prop as an attribute of the component tag. In order to fill in some date there, you should use the setup method like this:
app/matestack/components/some_component.rb
1
class SomeComponent < Matestack::Ui::VueJsComponent
2
3
vue_name "some-component"
4
5
def vue_props
6
{
7
some_serverside_data: "bar"
8
}
9
end
10
11
end
Copied!
This data is then available as:
1
this.props["some_serverside_data"]
Copied!
within the Vue.js JavaScript component.

Vue.js JavaScript component API

Component mixin

app/matestack/components/some_component.js
1
import Vue from "vue/dist/vue.esm";
2
import MatestackUiCore from "matestack-ui-core";
3
4
Vue.component('some-component', {
5
mixins: [MatestackUiCore.componentMixin],
6
//...
7
});
Copied!
Please make sure to integrate the componentMixin which gives the JavaScript component some essential functionalities in order to work properly within matestack

Component config

The JavaScript component can access the serverside injected data like:
1
this.props["some_serverside_data"]
Copied!
if implemented like
app/matestack/components/some_component.rb
1
class SomeComponent < Matestack::Ui::VueJsComponent
2
3
vue_name "some-component"
4
5
def vue_props
6
{
7
some_serverside_data: "bar"
8
}
9
end
10
11
end
Copied!

Params

If any query params are given in the URL, the JavaScript component can access them via:
1
this.params
Copied!
within the JavaScript component.

Vue.js API

As we're pretty much implementing pure Vue.js components, you can refer to the Vue.js guides in order to learn more about Vue.js component usage.
Last modified 29d ago