0

I am trying to combine custom knockoutjs binding with a standard binding. Although I have been able to find a related solution

ko.bindingHandlers.parentAreaComboBox = {

initialised: false,
init: function (element, valueAccessor, allBindingsAccessor, viewModel, context) {

    viewModel.parentAreas.subscribe(function (newParentAreas) {

        if (newParentAreas && newParentAreas.length > 0) {

            if (ko.bindingHandlers.parentAreaComboBox.initialised) {
                return;
            }
            ko.applyBindingsToNode(element, {
                options: viewModel.parentAreas,
                optionsCaption: 'Choose...',
                optionsText: 'Label',
                value: viewModel.selectedParentArea
            });
            $(element).chosen({});
            ko.bindingHandlers.parentAreaComboBox.initialised = true;
        }
    });
  }
};

but I am not able to make it work on mine. What am I doing wrong here?

Community
  • 1
  • 1
Harsh Vardhan
  • 111
  • 1
  • 14
  • For one, I get a complaint that bootstrap requires jquery, meaning your external resources are loaded in the wrong order. You also don't close your brace for the combobox spec. – Roy J Jul 14 '15 at 12:29
  • thanks for pointing, corrected [that](http://jsfiddle.net/harsh611/kwwq60bd/9/) still not working – Harsh Vardhan Jul 14 '15 at 12:35
  • Unrelated, but if you are using Knockout 3+ you probably want to use `context.$data` rather than `viewModel` in your `init` function as the Knockout documentation states that the `viewModel` parameter is [deprecated](http://knockoutjs.com/documentation/custom-bindings.html) – 300 baud Jul 14 '15 at 12:51

1 Answers1

0

Use the debugging console in your browser (press F12) and also the JSHint button in JSFiddle. Your VM constructor function is invalid. Once you fix that, you'll notice that you're trying to use subscribe on something that is not an observable.

Roy J
  • 42,522
  • 10
  • 78
  • 102
  • thanks for that and apologies for the poor quality of question from me, I tried it from this [version](http://jsfiddle.net/harsh611/kwwq60bd/4/) but I am not able to go further – Harsh Vardhan Jul 14 '15 at 12:47
  • This article on custom bindings is helpful. Read the whole thing, but in particular, look at the "Simple Wrapper Binding" section http://www.knockmeout.net/2011/07/another-look-at-custom-bindings-for.html – Roy J Jul 14 '15 at 13:55
  • after some effort I have been able to achieve [this](http://jsfiddle.net/harsh611/4zsu9pv2/).. thanks for reminding me about subscribe. – Harsh Vardhan Jul 14 '15 at 14:05
  • I am still confused Regarding how to create update feature in my custom binder. – Harsh Vardhan Jul 14 '15 at 15:39
  • Maybe you should ask a new question, saying what you want your binding to do, and post the latest code you have. – Roy J Jul 14 '15 at 16:31