Custom binding using knockoutmvc

113 views
Skip to first unread message

Lulamile Gaji

unread,
May 1, 2014, 6:22:25 AM5/1/14
to knocko...@googlegroups.com
How do I call a custom binding in knockout MVC. 

e.g. @ko.Html........

using this:-


ko.bindingHandlers.datetimepicker = {
            init: function (element, valueAccessor, allBindingsAccessor) {
                //initialize datepicker with some optional options
                var options = allBindingsAccessor().datepickerOptions || {};
                $(element).datetimepicker(options);

                //when a user changes the date, update the view model
                ko.utils.registerEventHandler(element, "changeDate", function (event) {
                    var value = valueAccessor();
                    if (ko.isObservable(value)) {
                        value(event.date);
                    }
                });
            },
            update: function (element, valueAccessor) {
                var widget = $(element).data("datetimepicker");
                //when the view model is updated, update the widget
                if (widget) {
                    widget.date = ko.utils.unwrapObservable(valueAccessor());
                    widget.setValue();
                }
            }
        };

Agah Özer

unread,
Apr 27, 2017, 5:29:58 AM4/27/17
to Knockout MVC
is there any way to custom bindings ?

i need to bind select2

Agah Özer

unread,
Apr 27, 2017, 5:39:39 AM4/27/17
to Knockout MVC
after i try some methods.....


                                @ko.Html.DropDownList(m => m.MyList, null, "MyListName").Custom("select2","")


<script type="text/javascript">
    ko.bindingHandlers.select2 = {

        init: function (element, valueAccessor, allBindingsAccessor) {
            var obj = valueAccessor(),
              allBindings = allBindingsAccessor(),
              lookupKey = allBindings.lookupKey;

            setTimeout(function () {
                $(element).select2(obj);
            }, 0);

            if (lookupKey) {
                var value = ko.utils.unwrapObservable(allBindings.value);
                $(element).select2('data', ko.utils.arrayFirst(obj.data.results, function (item) {
                    return item[lookupKey] === value;
                }));
            }

            ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                $(element).select2('destroy');
            });
        },
        update: function (element) {
            $(element).trigger('change');
        }
    };
</script>
@ko.Apply(Model)


this is work for me.....

1 Mayıs 2014 Perşembe 13:22:25 UTC+3 tarihinde Lulamile Gaji yazdı:
Reply all
Reply to author
Forward
0 new messages