Matestack Ui Core
AboutMatestack Ui CoreMatestack Ui VueJsMatestack Ui Bootstrap
2.1
2.1
  • Welcome
  • Migrating from 1.x to 2.0
  • Getting started
    • Installation & Update
    • Concepts & Rails Integration
    • Tutorial
    • Support & Feedback [WIP]
    • Common Issues [WIP]
  • HTML implemented in pure Ruby
    • Overview
    • HTML Rendering
    • Components
      • Component API
      • Component Registry
      • Components on Rails views
    • Pages
      • Page API
      • Rails Controller Integration
    • Apps
      • App API
    • Reusing Rails Views or Partials
  • Built-in Reactivity composed in pure Ruby
    • Overview
    • Emit Client Side Events
      • Onclick Component API
    • Toggle UI States
      • Toggle Component API
    • Call Server Side Actions
      • Overview
      • Action Component API
    • Reactive Forms
      • Overview
      • Form Component API
      • Input Component API
      • Textarea Component API
      • Checkbox Component API
      • Select Component API
      • Radio Component API
      • Nested Forms
    • Partial UI Updates
      • Overview
      • Async Component API
      • Cable Component API
      • Isolated Component API
    • Page Transitions
      • Overview
      • Transition Component API
    • Reactive Collections
      • Overview
      • Collection Component API [WIP]
  • Custom Reactivity implemented in Vue.js
    • Custom Vue.js Components
    • Third party Vue.js components [WIP]
    • Matestack's Vue.js APIs [WIP]
    • Matestack's Vuex API [WIP]
  • Integrations
    • Action Cable
    • Devise
    • Authorization [WIP]
    • CSS Frameworks [WIP]
    • Third Party JavaScript [WIP]
    • Third Party Ruby Gems [WIP]
  • Matestack Addons
    • Create your own Addon [WIP]
  • Testing
    • Capybara & Rspec
  • Community
    • Discord
    • Contribute [WIP]
  • About
    • Core Team
    • Sponsoring [WIP]
    • Legal Details [WIP]
Powered by GitBook
On this page
  • Structure, files and registry
  • Vue.js Ruby component
  • Vue.js JavaScript component
  • Vue.js Ruby component API
  • Same as component
  • Referencing the Vue.js JavaScript component
  • Passing data to the Vue.js JavaScript component
  • Vue.js JavaScript component API
  • Component mixin
  • Component config
  • Params
  • Vue.js API

Was this helpful?

Edit on GitHub
  1. Custom Reactivity implemented in Vue.js

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

class SomeComponent < Matestack::Ui::VueJsComponent

  vue_name "some-component"

  def response
    div class: "some-root-element" do
      plain "hello {{ foo }}!"
    end
  end

end

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

import Vue from "vue/dist/vue.esm";
import MatestackUiCore from "matestack-ui-core";

Vue.component('some-component', {
  mixins: [MatestackUiCore.componentMixin],
  data() {
    return {
      foo: "bar"
    };
  },
  mounted(){
    console.log(this.props['foo'])
  }
});

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:

// app/javascript/packs/application.js
import MatestackUiCore from 'matestack-ui-core'
import '../../../app/matestack/components/some_component'

If setup correctly, Matestack will render the component to:

<component is='some-component' :props='{}' :params='{}' inline-template>
  <div class="some-root-element">
    hello {{ foo }}!
  </div>
</component>

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

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

class SomeComponent < Matestack::Ui::VueJsComponent

  vue_name "some-component"

  #...
end

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

class SomeComponent < Matestack::Ui::VueJsComponent

  vue_name "some-component"

  def vue_props
    {
      some_serverside_data: "bar"
    }
  end

end

This data is then available as:

this.props["some_serverside_data"]

within the Vue.js JavaScript component.

Vue.js JavaScript component API

Component mixin

app/matestack/components/some_component.js

import Vue from "vue/dist/vue.esm";
import MatestackUiCore from "matestack-ui-core";

Vue.component('some-component', {
  mixins: [MatestackUiCore.componentMixin],
  //...
});

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:

this.props["some_serverside_data"]

if implemented like

app/matestack/components/some_component.rb

class SomeComponent < Matestack::Ui::VueJsComponent

  vue_name "some-component"

   def vue_props
    {
      some_serverside_data: "bar"
    }
  end

end

Params

If any query params are given in the URL, the JavaScript component can access them via:

this.params

within the JavaScript component.

Vue.js API

PreviousCollection Component API [WIP]NextThird party Vue.js components [WIP]

Last updated 3 years ago

Was this helpful?

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

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

component API documenation
Vue.js guides