knockout.js - custom bindinghandler in knockoutjs -


i have div in html below..

<div data-helper="my helper text1">help</div> 

also have array in javascript

self.helpers = [        { name: ' abc ', helper: ' helper text1 ' },        { name: ' def ', helper: ' helper text2 ' },        { name: ' xyz ', helper: ' helper text3 ' }            ]; 

using knockoutjs, bind helper data-helper text.

basically need convert code below necessary html code ..

<div databind="helper:helpers.helper">help</div> 

i tried create custom bindinghandler not..

any sincerely appreciated.

thanks

update: accepted answer without looking @ code , have reopen question. need lines below..

<div data-helper="my helper text1">help</div> 

so, div data-bind:text not work

i tried below did not succeed..

<!-- ko foreach: helpers -->             <div data-bind="attr: {data-helper:helper}">help             </div>             <!-- /ko --> 

there 2 ways implement functionality: custom component , custom binding. can start short example below:

ko.components.register('my-component', {          viewmodel: {              createviewmodel: function(params, componentinfo) {                return {                  helpers: params.helpers                };              }          },          template: { element: 'my-component-template' }      });    ko.bindinghandlers.helper = {          init: function(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext) {            var values = valueaccessor(),                $templatehtml = $('#my-component-template').text(),                $element = $(element).append($templatehtml);              var childcontext = bindingcontext.createchildcontext(values);              ko.applybindings(childcontext, $element.children()[0]);              return { controlsdescendantbindings: true };          }      };    ko.applybindings([         { name: ' abc ', helper: 'my helper text1' },         { name: ' def ', helper: 'my helper text2' },         { name: ' xyz ', helper: 'my helper text3' }             ]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>    <script type="text/html" id="my-component-template">      <div>          <!-- ko foreach: helpers -->              <div data-bind="text: helper, attr: { 'data-helper': helper }">              </div>          <!-- /ko -->      </div>  </script>    <div>component:</div>  <my-component params="helpers: $data"></my-component>    <div>custom binding:</div>  <div data-bind="helper: { helpers: $data }"></div>

the custom component consists of registration code ("ko.components.register('my-component'...") , template ("...").

update 1.

i've added custom binding example. can check documentation more details.

update 2

you should wrap attr name quotes:

<div data-bind="text: helper, attr: { 'data-helper': helper }"> </div> 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -