I am currently working on implementing editor functionality for a page using Vue2. The page contains an 'editable' filter which receives a content_id. Based on this content_id, we need to retrieve data from the root Vue instance (e.g. pageContent.mainTitle). Depending on the value of the editModeOn variable in the root, we should either render a component or output the corresponding content (e.g.
<editable content="mainTitle" />
or the content in the mainTitle key).
This is the basic structure of the HTML:
<div id="root">
{{ 'mainContent' | editable }}
<label class="checkbox">
<input type="checkbox" v-model="editModeOn">
Switch edit mode
</label>
</div>
Here is an example of the Vue instance:
new Vue({
el: '#root',
data: {
editModeOn: true,
pageContent: {
mainTitle: "Test title",
mainContent: "Lorem ipsum dolor sit amet, consectetur adipisicing elit. At, et!"
}
},
filters: {
editable(contentId) {
if (!this.editModeOn) {
return `<editable content="{{ this.pageContent[contentId] }}" />`;
} else {
return this.pageContent[contentId];
}
}
}
});
I decided to utilize filters for this functionality because when edit mode is disabled, I want to avoid adding unnecessary wrappers like span or div.
If there is a better way to achieve the desired functionality, I would appreciate any suggestions. Thank you!