Created
November 2, 2016 16:24
-
-
Save stemcc/4db085f6b66087cd6c7c428f684afe95 to your computer and use it in GitHub Desktop.
Vuejs -- working with server data
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 characters
| //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