get data configured for list element

This commit is contained in:
Gaffen 2019-08-14 23:16:03 +01:00
parent 4b77d0b8ca
commit f4e6ab9d0c
4 changed files with 16 additions and 14 deletions

View File

@ -5,7 +5,7 @@
{% include "partials/sprite.svg" %} {% include "partials/sprite.svg" %}
</div> </div>
<header> <header>
<h1>Your brand-spanking new Kindling website!</h1> <h1>SSR Rendering in Nunjucks proof of concept</h1>
</header> </header>
{% block body %} {% block body %}
{{ content | safe }} {{ content | safe }}

View File

@ -4,6 +4,7 @@
{% vue { component:"HelloWorld" } %} {% vue { component:"HelloWorld" } %}
{% vue { component:"HelloWorld", props: { message: "This is a test" } } %} {% vue { component:"HelloWorld", props: { message: "This is a test" } } %}
{% vue { component:"HelloWorld", props: { message: "Of server rendered Vue components" } } %} {% vue { component:"HelloWorld", props: { message: "Of server rendered Vue components" } } %}
{% vue { component:"ListComp"} %}
{% vue { component:"ListComp", props: { startlist: [{message:"Render Vue in Nunjucks"}, {message:"???"}, {message:"Profit!"}] } } %} {% vue { component:"ListComp", props: { startlist: [{message:"Render Vue in Nunjucks"}, {message:"???"}, {message:"Profit!"}] } } %}
<section id="content" role="main" class="content-wrapper"> <section id="content" role="main" class="content-wrapper">
{{content|safe}} {{content|safe}}

View File

@ -1,12 +1,15 @@
<template lang="html"> <template lang="html">
<div class="ListComp" v-bind:data-startlist="startlist"> <div class="ListComp" v-bind:data-startlist="startlist">
<ol> <ol>
<li v-for="item in list"> <li v-for="(item, index) in list">
{{ item.message }} <p>{{ item.message }}</p>
<button v-on:click="removeItem(index)">&times;</button>
</li> </li>
</ol> </ol>
<input v-model="newitem.message" /> <form v-on:submit.prevent>
<button v-on:click="addItem"></button> <input v-model="newitem.message" />
<button v-on:click="addItem">Add</button>
</form>
</div> </div>
</template> </template>
@ -15,24 +18,22 @@ import Vue from "vue";
export default { export default {
props: { props: {
startlist: { startlist: {
type: Array, type: String,
default: function() { default: JSON.stringify([{ message: "Add items to me" }])
return [{ message: "Add items to me" }];
}
} }
}, },
data: function() { data: function() {
return { return {
newitem: { message: "Enter text here" }, newitem: { message: "Enter text here" },
list: [].concat([this.startlist]) list: [].concat(JSON.parse(this.startlist))
}; };
}, },
mounted: function() {
console.log(this.startlist);
},
methods: { methods: {
addItem: function(e) { addItem: function(e) {
this.list = this.list.concat([Object.assign({}, this.newitem)]); this.list = this.list.concat([Object.assign({}, this.newitem)]);
},
removeItem: function(e) {
this.list = this.list.slice(0, e).concat(this.list.slice(e + 1));
} }
} }
}; };

View File

@ -15,6 +15,6 @@ Array.prototype.forEach.call(document.querySelectorAll(".ListComp"), function(
elem elem
) { ) {
new Vue({ new Vue({
render: h => h(ListComp, { props: { message: elem.dataset.list } }) render: h => h(ListComp, { props: { startlist: elem.dataset.startlist } })
}).$mount(elem); }).$mount(elem);
}); });