Geek asks and answers

    How to get parent values ​​of template binding in nested binding?

    I have a knockout pattern:

    <script type="text/html" id="list">
      <ul data-bind="foreach: items">
        <li data-bind="{text: name}"></li>
      </ul>
    </script>
    

    What I use like this:

    <div data-bind="
      template: {name: 'list', data: itemList}, 
      myBinding: {itemType: 'foo'}
    "></div>
    

    I have a binding handler myBinding:

    ko.bindingHandlers.myBinding = {
      init: function(element, valueAccessor) {
        var bindingValue = valueAccessor();
    
        alert ( bindingValue.itemType );  // alerts "foo"
    
        // now set up a jQuery click handler
        $(element).on("click", "li", listItemClickHandler);
      }
    };
    

    And an event handler:

    function listItemClickHandler() {
      var bindingContext = ko.contextFor(this);
    
      alert( "bindingValue.itemType ???" );
    });
    

    Is there a way to get the itemTypeparent template, as provided in the user binding, in the click handler, although knockout bindingContext?

    • Without adding some fake CSS class, such as .type-foo, to <ul>(what am I doing now).
    • Without saving "foo"to an array element during myBinding.init().
    • Without binding the event handler, use the close variable ( bindingValue).
    • Without using jQuery event.dataobject. I could do this, but I would like to get it from the context of the knockout binding, if that is not possible.
    +3
    knockout.js
    Tomalak 24 '12 18:02
    1

    5- . , , context.$itemType = bindingValue.itemType;. : http://jsfiddle.net/rniemeyer/yeN8P/

    - , itemType $parent. , , :

    template: {name: 'list', data: { items: items, itemType: 'foo' } }

    , , items, items itemType, $parent.itemType "foo".

    : http://jsfiddle.net/rniemeyer/yeN8P/1/

    +7
    RP Niemeyer 25 '12 3:11

    More articles:

    • PSQLException misses - java
    • Get the column name that has the maximum value in the sql row - max
    • Гармонический текст при чтении из внешнего процесса в Java - java
    • Excel array with Quartile function from different sheets - excel
    • What is the reason for the classpath mismatch between compile time and runtime in a multi-threaded application? - java
    • How can I programmatically clear text selection in InDesign using ExtendScript? - textselection
    • Get attribute key from XML using PHP - php
    • LuaJava for Android? - java
    • Repetition in a builder pattern - java
    • Как использовать автозапуск в Windows 7 с флеш-накопителя для открытия веб-страницы? - windows-7

    All Articles

    Geek-Ask | 2020