Reputation: 6032
With VueJS, I am trying to create a generic component that would work with different types of records.
For instance, let's say I have user records:
var users = [
{ UserID: 1, username: "pan",email:"[email protected]" },
{ UserID: 2, username: "john",email:"[email protected]" }
];
And group records
var groups = [
{ GroupId: 1, groupName: "Users", description: "Lorem ipsum ..." },
{ GroupId: 2, groupName: "Admins", description: "Some people with super powers" }
];
I want to create a Vue component to edit those records, so it can be defined as such:
<record-editor v-bind:record="user[0]" title="Edit user">
<text-editor label="User name" property="username"></text-editor>
<text-editor label="Email" property="email"></text-editor>
</record-editor>
<!-- For the binding syntax, I am not sure what should
I use to bind to a record in the lists shown before -->
<record-editor v-bind:record="groups[0]" title="Edit group">
<text-editor label="groupName" property="groupName"></text-editor>
<text-editor label="Description" property="description"></text-editor>
</record-editor>
Right now, what I have is:
(function() {
var textEditor = Vue.component('text-editor', {
template: "#text-editor",
props: ['label', 'property']
});
var recordEditor= Vue.component('record-editor', {
template: '#model-editor',
props: ['title', 'record']
});
var vue = new Vue({
el:"#someContainer",
data: {
users : users,
groups: groups
}
})
}())
<template id="text-editor">
<div>
<label v-bind:for="property">{{label}}</label>
<!-- need help figuring what to put in v-bind:value -->
<input type="text" v-bind:name="property"
v-bind:id="property"
v-bind:value="">
</div>
</template>
<template id="record-editor">
<div>
<h2>{{title}}</h2>
<form>
<slot></slot>
</form>
</div>
</template>
So basically, what I am missing is how to bin to the elements in the list to edit them.
And how can I dynamically define properties for the sub components (text-editor).
Upvotes: 3
Views: 1531
Reputation: 82479
You can do what you want with scoped slots and v-model
. Here is a working example.
console.clear()
var users = [
{ UserID: 1, username: "pan",email:"[email protected]" },
{ UserID: 2, username: "john",email:"[email protected]" }
];
var groups = [
{ GroupId: 1, groupName: "Users", description: "Lorem ipsum ..." },
{ GroupId: 2, groupName: "Admins", description: "Some people with super powers" }
];
var textEditor = Vue.component('text-editor', {
template: "#text-editor",
props: ['label', 'value'],
computed:{
property:{
get(){ return this.value},
set(v){this.$emit("input", v)}
}
}
});
var recordEditor= Vue.component('record-editor', {
template: '#record-editor',
props: ['title', 'record']
});
var vue = new Vue({
el:"#app",
data: {
users : users,
groups: groups
}
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="app">
<record-editor v-bind:record="users[0]" title="Edit user">
<template scope="{record}">
<text-editor label="User name" v-model="record.username"></text-editor>
<text-editor label="Email" v-model="record.email"></text-editor>
</template>
</record-editor>
{{users}}
</div>
<template id="text-editor">
<div>
<label>{{label}}</label>
<input type="text" v-model="property">
</div>
</template>
<template id="record-editor">
<div>
<h2>{{title}}</h2>
<form>
<slot :record="record"></slot>
</form>
</div>
</template>
I removed the label and id binding you were doing in the text editor primarily because an email address is an invalid id for an input element. Essentially I updated your text-editor
to work with v-model
which will substitute for your property
binding. A scoped slot is required because you are defining the model you want to edit on the record-editor
. A scoped slot allows you to pass data from the enclosing scope to the contained components.
Upvotes: 3