【发布时间】:2013-12-17 13:01:56
【问题描述】:
我使用 TinyMCE Wysiwyg 编辑器进行了自定义数据绑定,但由于某种原因,当我选择新的文本输入时它不会更新。有些东西告诉我,也许更新应该破坏绑定只是为了重新初始化它,但我无法让它工作。
ko.bindingHandlers.tinymce = {
init: function (element, valueAccessor, allBindingsAccessor, context) {
var options = allBindingsAccessor().tinymceOptions || {};
var modelValue = valueAccessor();
var value = ko.utils.unwrapObservable(valueAccessor());
var el = $(element)
//handle edits made in the editor. Updates after an undo point is reached.
options.setup = function (ed) {
console.log(ed)
ed.onChange.add(function (ed, l) {
if (ko.isWriteableObservable(modelValue)) {
modelValue(l.content);
}
});
};
//handle destroying an editor
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
setTimeout(function () { $(element).tinymce().remove() }, 0)
});
//$(element).tinymce(options);
setTimeout(function () { $(element).tinymce(options); }, 0);
el.html(value);
},
update: function (element, valueAccessor, allBindingsAccessor, context) {
var el = $(element)
var value = ko.utils.unwrapObservable(valueAccessor());
var id = el.attr('id')
alert("Value=" + value);
//handle programmatic updates to the observable
// also makes sure it doesn't update it if it's the same.
// otherwise, it will reload the instance, causing the cursor to jump.
if (id !== undefined) { // Does not work
var content = tinyMCE.getInstanceById(id).getContent({ format: 'raw' });
if (content !== value) {
//el.html(value);
el.val(value);
}
}
}
};
更新部分只在第一次选择时调用一次。选择新的文本批次不会触发警报。为什么?
这里的提琴手:http://jsfiddle.net/Wf7xk/1/
【问题讨论】:
标签: javascript jquery data-binding knockout.js tinymce