I have a vuejs 1.x v-for loop over some elements that display some components that use a custom directive (for tinymce). The directive works when the expression can be parsed based on the root element, but since it is inside a loop, I need it to reference the index somehow.
// tinymce directive
Vue.directive('tinymce-editor',{
twoWay: true,
bind: function() {
var self = this;
// required cause textarea not in DOM yet
$(document).on('click', '#'+self.el.id, function(){
tinymce.init({
menubar: false,
browser_spellcheck: true,
plugins: "link image preview wordcount table",
selector: "#" + self.el.id,
setup: function(editor) {
// init tinymce
editor.on('init', function() {
tinymce.get(self.el.id).setContent(self.value);
});
// when typing keyup event
editor.on('keyup', function() {
// ************
// self.expression here needs to be questions[idx].value
// not question.value
// ************
self.vm.$set(self.expression, tinymce.get(self.el.id).getContent());
});
}
})});
},
update: function(newVal, oldVal) {
// set val and trigger event
$(this.el).val(newVal).trigger('keyup');
}
}
})
<div class="form-group" v-show="questions.length" v-for="question in questions">
<textarea
id="textarea-{{question.id}}"
v-tinymce-editor="question.value"
>{{question.value}}</textarea>
</div>
</div
Inside the tinymce init, the keyup event gets self.expression but it needs to be dynamic? from the questions array ..
You should be considering following from documentation:
el, binding, vnode in the bind functionbinding.value will give you value passed which is question.valueSo you need to make following changes:
Vue.directive('tinymce-editor',{
twoWay: true,
bind: function(el, binding, vnode) {
var self = this;
// required cause textarea not in DOM yet
$(document).on('click', '#'+self.el.id, function(){
tinymce.init({
menubar: false,
browser_spellcheck: true,
plugins: "link image preview wordcount table",
selector: "#" + self.el.id,
setup: function(editor) {
// init tinymce
editor.on('init', function() {
tinymce.get(self.el.id).setContent(self.value);
});
// when typing keyup event
editor.on('keyup', function() {
// ************
// binding.value will be questions[idx].value
// ************
self.vm.$set(binding.value, tinymce.get(self.el.id).getContent());
});
}
})});
},
...
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With