javascript - Mvvm 支持自定义剑道 ui 小部件

标签 javascript jquery mvvm kendo-ui custom-controls

前几天我问this问题并得到了一个非常酷的答案。此后我想知道我是否可以使用 my custom widget因为我按照 mvvm 约定使用所有标准的剑道组件。我必须编辑自定义小部件的哪些部分? 例如:

<div id="dropdowns" data-role="linkeddropdowns" data-period="YEAR" 
    data-bind="year: selectedYear"></div>

谢谢,

最佳答案

我认为您可能会遇到内部 View 模型方法的问题,因为 Kendo UI 往往不擅长嵌套 View 模型绑定(bind)。 据我所知,所有 Kendo UI 小部件都因此在代码中创建了它们的内部小部件。

旁注:在您的小部件中,您使用 DOM id 作为下拉元素 - 如果您要在同一页面上创建多个小部件,这将创建重复项。在这种情况下最好使用类。

要启用year 绑定(bind),您需要custom binder ,它可能看起来像这样:

kendo.data.binders.widget.year = kendo.data.Binder.extend({
    init: function (element, bindings, options) {
        kendo.data.Binder.fn.init.call(this, element, bindings, options);

        var that = this;
        that.element.bind("change", function () {
            that.change(); //call the change function
        });
    },
    refresh: function () {
        var that = this,
            value = that.bindings.year.get();

        this.element.setYear(value);
    },
    change: function () {
        var value = this.element.getYear();
        this.bindings.year.set(value);
    }
});

这将与提供这些访问器的小部件一起工作:

getYear: function () {
    return this._getWidget("year").value();
},

setYear: function (value) {
    this._getWidget("year").value(value);
    console.log(this._getWidget("year"));
}

完整的小部件示例(注意它没有完全实现——它只考虑了年份绑定(bind)和时间段设置):

(function ($) {
    var kendo = window.kendo,
        ui = kendo.ui,
        Widget = ui.Widget,
        periods = [
            { text: "PERIOD: YEAR", value: "YEAR" },
            { text: "PERIOD: QUARTER", value: "QUARTER" }
        ],
        quarters = [
            { text: "1. Quarter", value: 1 },
            { text: "2. Quarter", value: 2 },
            { text: "3. Quarter", value: 3 },
            { text: "4. Quarter", value: 4 }
        ],
        years = [2011, 2012, 2013, 2014];

    var LinkedDropDowns = Widget.extend({
        init: function (element, options) {
            var that = this,
                periodOption = $(element).data("period");
            if (periodOption) {
                this.options.period = periodOption;
            }

            Widget.fn.init.call(that, element, options);
            that._create();

          // make sure view state is correct depending on selected period
          this._getWidget("period").trigger("change");
        },

        options: {
            name: "LinkedDropDowns",
            period: "YEAR",
            periods: periods,
            year: 2011,
            years: years,
            quarter: 1,
            quarters: quarters,
            selectedYear: 2011
        },

        onPeriodChange: function (e) {
            switch (e.sender.value()) {
                case "YEAR":
                    $(this._getWidget("year").wrapper).show();
                    $(this._getWidget("quarter").wrapper).hide();
                    break;
                case "QUARTER":
                    $(this._getWidget("year").wrapper).show();
                    $(this._getWidget("quarter").wrapper).show();
                    break;
                default:
                    break;
            }
        },

        onChange: function () {
            // trigger change so the binder knows about it
            this.trigger("change", { sender: this }); 
        },

        _getWidget: function (name) {
            var el = $(this.element).find("input." + name).first();
            return el.data("kendoDropDownList");
        },

        _create: function () {
            var that = this;

            // create dropdowns from templates and append them to DOM
            that.periodDropDown = $(that._templates.periodDropDown);
            that.yearDropDown = $(that._templates.yearDropDown);
            that.quarterDropDown = $(that._templates.quarterDropDown);

            // append all elements to the DOM
            that.element.append(that.periodDropDown)
                .append(that.yearDropDown)
                .append(that.quarterDropDown);

            $(this.element).find(".period").kendoDropDownList({
                dataTextField: "text",
                dataValueField: "value",
                value: this.options.period,
                change: this.onPeriodChange.bind(that),
                dataSource: this.options.periods
            });

            $(this.element).find(".year").kendoDropDownList({
                dataSource: this.options.years,
                change: this.onChange.bind(this)
            });

            $(this.element).find(".quarter").kendoDropDownList({
                dataTextField: "text",
                dataValueField: "value",
                dataSource: this.options.quarters

            });
        },

        _templates: {
            periodDropDown: "<input class='period' />",
            yearDropDown: "<input class='year' style='width: 90px' />",
            quarterDropDown: "<input class='quarter' style='width: 110px' />"
        },

        getYear: function () {
            return this._getWidget("year").value();
        },

        setYear: function (value) {
            this._getWidget("year").value(value);
            console.log(this._getWidget("year"));
        }
    });

    ui.plugin(LinkedDropDowns);
})(jQuery);

( demo )

关于javascript - Mvvm 支持自定义剑道 ui 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24933598/

相关文章:

javascript - $("div")。引用所有div?

javascript - 如何去掉动态十六进制值LESS的引号

javascript - PHP网页上的进度条

javascript - Meteor Facebook 登录错误 Accounts.LoginCancelledError : No matching login attempt found

javascript - 使用 Restful 服务验证网站中数据库表中是否存在元素

Android 架构问题

silverlight - 如何从 ViewModel 绑定(bind) StackPanel 子级?

c# - MVVM 关闭文档的方式可以取消

javascript - 我怎样才能轻松提取数据扩展网址?

javascript - 搜索字段在 Chrome 中不起作用