Skip to content

Instantly share code, notes, and snippets.

@stemcc
Created November 2, 2016 16:24
Show Gist options
  • Select an option

  • Save stemcc/4db085f6b66087cd6c7c428f684afe95 to your computer and use it in GitHub Desktop.

Select an option

Save stemcc/4db085f6b66087cd6c7c428f684afe95 to your computer and use it in GitHub Desktop.
Vuejs -- working with server data
//app.js
function mountComponents() {
const components = document.querySelectorAll('[data-behaviour="component"]')
for (let i = 0; i < components.length; i++) {
const node = components[i]
const props = JSON.parse(node.getAttribute('data-props'));
const $Vue = new Vue({
el: node,
data: props,
})
}
}
mountComponents();
/* index.html */
<h2>Listing posts with Vue integration</h2>
<hr />
<div v-cloak data-behaviour="component" data-props="<%= Poison.encode!(%{posts: Enum.map(@posts, fn post -> %{
title: post.title,
body: post.body,
url: post_path(@conn, :show, post),
id: post.id
} end)}) %>">
<ul>
<li v-for="post in posts" track-by="id">
<h3>{{ post.title }}</h3>
<p>{{ post.body }}</p>
<a :href="post.url">Go to post</a>
</li>
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment