可编辑的字符串knockoutJS自定义绑定

时间:2012-08-28 21:44:00

标签: knockout.js ko-custom-binding

我经常在网页上显示我需要更改的文本数据。

目前,我编写了一个自定义的mouseEvent处理程序,以显示div上弹出的“编辑”按钮,其中有一个可观察绑定的span被覆盖。如果用户点击它,我会通过span knockoutJS绑定隐藏visible并显示文本input以允许编辑。在标签上,我保存了修改更改并再次显示更新的span

是否存在包含所有此功能的自定义开源KOJS绑定。我并不是要求我为它编写,只是指出它,因为任务非常普遍,KO似乎有一个很好的基础来优雅地实现它。

2 个答案:

答案 0 :(得分:0)

您的问题是如何使用字符串作为模板引擎的源代码?如果是这样,我已经为我的一个绑定

做了那个

https://github.com/AndersMalmgren/Knockout.Bindings/blob/master/src/knockout.bindings.js

我的回购中的相关代码

var stringTemplateSource = function (template) {
    this.template = template;
};

stringTemplateSource.prototype.text = function () {
    return this.template;
};

var stringTemplateEngine = new ko.nativeTemplateEngine();
stringTemplateEngine.makeTemplateSource = function (template) {
    return new stringTemplateSource(template);
};

并使用它

ko.bindingHandlers.myCustomBinding = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            ko.renderTemplate('<span data-bind="text: $data"></span>', bindingContext.createChildContext(valueAccessor()), { templateEngine: stringTemplateEngine }, element, "replaceChildren");

            return { controlsDescendantBindings: true };
        }

答案 1 :(得分:0)

实际上,有这样的东西,它甚至看起来很棒。

https://github.com/brianchance/knockout-x-editable

在此处查看此行动:http://vitalets.github.io/x-editable/demo-bs3.html

希望能提供帮助:)

相关问题