kendo-labs / knockout-kendo

A project to create a robust set of Knockout.js bindings for the Kendo UI widgets.
http://kendo-labs.github.com/knockout-kendo/
273 stars 144 forks source link

descendant binding for kendoDatePickerFor #214

Open hidegh opened 9 years ago

hidegh commented 9 years ago

There's possible a problem while evaluating binding: data_bind = "kendoDatePicker: { readOnly: isMadeReadOnly }"

Got the error below. But somehow if i write: data_bind = "kendoDatePicker: { value: '1990-01-01', readOnly: isMadeReadOnly }" even setting the value as setting the disabled flag works!

Uncaught TypeError: Unable to process binding "kendoDatePicker: function (){return { readOnly:isMadeReadOnly} }"
Message: e.indexOf is not a function
abdurrahman commented 8 years ago

Any solution ? I have same trouble with timepicker to

hidegh commented 8 years ago

i simply dropped the usage of this solution. instead tried to use custom kendo bindings...this is what i use - see i only verwrite 4 main bindings.

$(function () {
    // on document ready...

    if (typeof ko != "undefined" && typeof kendo != "undefined") {
        // Extending original KO handlers to support kendo widgets...since knockout-kendo.js does not worked well in my tests...
        var originalHandlerForValueUpdate = ko.bindingHandlers.value.update;
        var originalHandlerForDisableUpdate = ko.bindingHandlers.disable.update;
        var originalHandlerForEnableUpdate = ko.bindingHandlers.enable.update;
        var originalHandlerForReadonlyUpdate = ko.bindingHandlers.readonly.update;

        ko.bindingHandlers.value.update = function (element, valueAccessor, allBindingsAccessor, viewModel, context) {

            var $element = $(element);
            var value = ko.utils.unwrapObservable(valueAccessor());

            var widget = kendo.widgetInstance($element);
            if (widget != "undefined") {
                // Check if widget has given method, execute it...
                if (widget && widget["value"] !== undefined) {
                    widget["value"](value);
                    return;
                }
            }

            // If binding wasn't processed via kendo widget, use original knockout handler
            originalHandlerForValueUpdate(element, valueAccessor, allBindingsAccessor, viewModel, context);

        };

        ko.bindingHandlers.disable.update = function (element, valueAccessor, allBindingsAccessor, viewModel, context) {

            var $element = $(element);
            var value = ko.utils.unwrapObservable(valueAccessor());

            var widget = kendo.widgetInstance($element);
            if (widget != "undefined") {
                // Check if widget has given method, execute it...
                if (widget && widget["enable"] !== undefined) {
                    widget["enable"](!value);
                    return;
                }
            }

            /*
            if ($element.hasClass('k-textbox')) {
                if (value) $element.addClass("k-state-disabled");
                else $element.removeClass("k-state-disabled");
            }
            */

            // If binding wasn't processed via kendo widget, use original knockout handler
            originalHandlerForDisableUpdate(element, valueAccessor, allBindingsAccessor, viewModel, context);

        };

        ko.bindingHandlers.enable.update = function (element, valueAccessor, allBindingsAccessor, viewModel, context) {

            var $element = $(element);
            var value = ko.utils.unwrapObservable(valueAccessor());

            var widget = kendo.widgetInstance($element);
            if (widget != "undefined") {
                // Check if widget has given method, execute it...
                if (widget && widget["enable"] !== undefined) {
                    widget["enable"](value);
                    return;
                }
            }

            /*
            if ($element.hasClass('k-textbox')) {
                if (value) $element.removeClass("k-state-disabled");
                else $element.addClass("k-state-disabled");
            }
            */

            // If binding wasn't processed via kendo widget, use original knockout handler
            originalHandlerForEnableUpdate(element, valueAccessor, allBindingsAccessor, viewModel, context);

        };

        // We override it only if it was defined before - since readonly binding is a custom handler, we need to do this check
        if (ko.bindingHandlers.readonly !== undefined) {

            ko.bindingHandlers.readonly.update = function (element, valueAccessor, allBindingsAccessor, viewModel, context) {

                var $element = $(element);
                var value = ko.utils.unwrapObservable(valueAccessor());

                var widget = kendo.widgetInstance($element);
                if (widget != "undefined") {
                    // Check if widget has given method, execute it...
                    if (widget && widget["readonly"] !== undefined) {
                        widget["readonly"](value);
                        return;
                    }
                }

                // If binding wasn't processed via kendo widget, use original knockout handler
                originalHandlerForReadonlyUpdate(element, valueAccessor, allBindingsAccessor, viewModel, context);

            };

        };

    } else {
        console.log("ko or kendo is undefined, cannot apply kendo.knockout.bindings.js");
    }

});