实现 vuejs 和 codemirror 的相互绑定
Vue.directive("codemirror", {
twoWay: true,
bind: function () {
// 请自行初始化CodeMirror, 可应用 $(this.el).data("codemirror")
this.editor = editor;
// model自动赋值标志
this.silent = false;
this.handler = function () {
if (!this.silent) {
this.set(this.editor.getValue(), true); // 加锁,避免相互赋值
}
}.bind(this);
this.editor.on("change", this.handler);
},
update: function (value, oldValue) {
this.silent = true;
this.editor.setValue(this.vm.$data[this.raw]);
this.silent = false;
}
});Vue.direct