Knockout - is it possible to combine standard select bindings with a custom binding?

前端 未结 4 1555
离开以前
离开以前 2021-01-14 19:53

This does NOT work (custom binding is called but dropdown is empty)


                        
    
提交评论

  • 2021-01-14 20:44

    Yup, just reorder your bindings (fiddle: http://jsfiddle.net/gBhbx/4/):

    <select id="parentArea" class="chosen-select" data-bind="   
       options: parentAreas,
       optionsCaption: 'Choose...',
       optionsText: 'Label',
       chosen:{},
       value: selectedParentArea">
    </select>
    
    0 讨论(0)
  • 2021-01-14 20:45

    Its a antipattern that you depend on the order of bindings.

    If you have a custom binding that needs other bindings to run before itself you should call those bindings from the custom binding like

    ko.applyBindingsToNode(element, { options: arr, value: val });
    

    and after that do $(element).chosen

    0 讨论(0)
  • 2021-01-14 20:52

    I think that your problem is in the chosen plugin itself. When you apply .chosen on your select tag it changes its markup(it's not longer a normal html select).
    So in your bindning your apply chosen custom binding first which then change the html markup and so your binding are not working properly..

    To solve that problem you need to apply your custom binding last not at first .. so that ko binding are applied normally then your custom binding is applied and change your select(but now you have built your select properly)

    Update

    to run function after the option elements are generated you can use optionsAfterRender callback .. Check out the documentaion here

    Another dirty solution is to use settimeout

    0 讨论(0)
  • 提交回复
    热议问题