How get knockout template outer component?












0















I changed previous example code and my logic. I need click the button and collapse the from div by element id. Collapse text gets from KO
arrays, and I want button visible if Array exist. data-bind"visible: exist Array1..2"



<div class=«header»>
<!— ko if: typeof Array1 !== 'undefined' —>
<button data-toggle="collapse" href="#containerLoan<?= $key + 1 ?>» aria-expanded=" false" aria-controls="containerLoan<?= $key ? >">button1</button>
<!-- /ko -->
<!— ko if: typeof Array2 !== 'undefined' —>
<button data-toggle="collapse" href="#containerLoan<?= $key + 2 ?>»
aria-expanded=" false
" aria-controls="containerLoan<?= $key ?>">button2</button>
<!-- /ko -->
</div>

<div class=«loandata» data-bind='component: { name: «test-component", params: { Id: <?= $model->Id ?>} }'></div>

<script type="text/html" id=«test-template">

<div class="collapse multi-collapse" id="containerLoan<?= $key + 1 ?>">
<div class="card card-body">
<!-- ko if: Array1.length > 0 -->
<div>…</div>
<!-- /ko -->
</div>
</div>

<div class="collapse multi-collapse" id="containerLoan<?= $key + 2 ?>">
<div class="card card-body">
<!-- ko if: Array2.length > 0 -->
<div>…</div>
<!-- /ko -->
</div>
</div>

</script>


In *.ts
namespace Test.One {



export class TestViewModel {

Array1: KnockoutObservable<Product> = ko.computed(() => { … return …; }
Array2: KnockoutObservable<Product> = ko.computed(() => { … return …; }

private fetch(id: number): void {
$.getJSON(…)
}

constructor(params: { Id: number }) {
this.Id(params.Id);
this.fetch(params.Id);
}
export class ViewModel {
constructor() {
ko.components.register(‘test-component', {
viewModel: params => {
return new TestViewModel(params);
},
template:{
element :
'hint-loan-product-template'
},
});
}
}

export let viewModelObject = new ViewModel();
ko.cleanNode(document.getElementById(‘test-area’));
ko.applyBindings(viewModelObject);//, document.getElementById(‘test-area'));


}










share|improve this question





























    0















    I changed previous example code and my logic. I need click the button and collapse the from div by element id. Collapse text gets from KO
    arrays, and I want button visible if Array exist. data-bind"visible: exist Array1..2"



    <div class=«header»>
    <!— ko if: typeof Array1 !== 'undefined' —>
    <button data-toggle="collapse" href="#containerLoan<?= $key + 1 ?>» aria-expanded=" false" aria-controls="containerLoan<?= $key ? >">button1</button>
    <!-- /ko -->
    <!— ko if: typeof Array2 !== 'undefined' —>
    <button data-toggle="collapse" href="#containerLoan<?= $key + 2 ?>»
    aria-expanded=" false
    " aria-controls="containerLoan<?= $key ?>">button2</button>
    <!-- /ko -->
    </div>

    <div class=«loandata» data-bind='component: { name: «test-component", params: { Id: <?= $model->Id ?>} }'></div>

    <script type="text/html" id=«test-template">

    <div class="collapse multi-collapse" id="containerLoan<?= $key + 1 ?>">
    <div class="card card-body">
    <!-- ko if: Array1.length > 0 -->
    <div>…</div>
    <!-- /ko -->
    </div>
    </div>

    <div class="collapse multi-collapse" id="containerLoan<?= $key + 2 ?>">
    <div class="card card-body">
    <!-- ko if: Array2.length > 0 -->
    <div>…</div>
    <!-- /ko -->
    </div>
    </div>

    </script>


    In *.ts
    namespace Test.One {



    export class TestViewModel {

    Array1: KnockoutObservable<Product> = ko.computed(() => { … return …; }
    Array2: KnockoutObservable<Product> = ko.computed(() => { … return …; }

    private fetch(id: number): void {
    $.getJSON(…)
    }

    constructor(params: { Id: number }) {
    this.Id(params.Id);
    this.fetch(params.Id);
    }
    export class ViewModel {
    constructor() {
    ko.components.register(‘test-component', {
    viewModel: params => {
    return new TestViewModel(params);
    },
    template:{
    element :
    'hint-loan-product-template'
    },
    });
    }
    }

    export let viewModelObject = new ViewModel();
    ko.cleanNode(document.getElementById(‘test-area’));
    ko.applyBindings(viewModelObject);//, document.getElementById(‘test-area'));


    }










    share|improve this question



























      0












      0








      0








      I changed previous example code and my logic. I need click the button and collapse the from div by element id. Collapse text gets from KO
      arrays, and I want button visible if Array exist. data-bind"visible: exist Array1..2"



      <div class=«header»>
      <!— ko if: typeof Array1 !== 'undefined' —>
      <button data-toggle="collapse" href="#containerLoan<?= $key + 1 ?>» aria-expanded=" false" aria-controls="containerLoan<?= $key ? >">button1</button>
      <!-- /ko -->
      <!— ko if: typeof Array2 !== 'undefined' —>
      <button data-toggle="collapse" href="#containerLoan<?= $key + 2 ?>»
      aria-expanded=" false
      " aria-controls="containerLoan<?= $key ?>">button2</button>
      <!-- /ko -->
      </div>

      <div class=«loandata» data-bind='component: { name: «test-component", params: { Id: <?= $model->Id ?>} }'></div>

      <script type="text/html" id=«test-template">

      <div class="collapse multi-collapse" id="containerLoan<?= $key + 1 ?>">
      <div class="card card-body">
      <!-- ko if: Array1.length > 0 -->
      <div>…</div>
      <!-- /ko -->
      </div>
      </div>

      <div class="collapse multi-collapse" id="containerLoan<?= $key + 2 ?>">
      <div class="card card-body">
      <!-- ko if: Array2.length > 0 -->
      <div>…</div>
      <!-- /ko -->
      </div>
      </div>

      </script>


      In *.ts
      namespace Test.One {



      export class TestViewModel {

      Array1: KnockoutObservable<Product> = ko.computed(() => { … return …; }
      Array2: KnockoutObservable<Product> = ko.computed(() => { … return …; }

      private fetch(id: number): void {
      $.getJSON(…)
      }

      constructor(params: { Id: number }) {
      this.Id(params.Id);
      this.fetch(params.Id);
      }
      export class ViewModel {
      constructor() {
      ko.components.register(‘test-component', {
      viewModel: params => {
      return new TestViewModel(params);
      },
      template:{
      element :
      'hint-loan-product-template'
      },
      });
      }
      }

      export let viewModelObject = new ViewModel();
      ko.cleanNode(document.getElementById(‘test-area’));
      ko.applyBindings(viewModelObject);//, document.getElementById(‘test-area'));


      }










      share|improve this question
















      I changed previous example code and my logic. I need click the button and collapse the from div by element id. Collapse text gets from KO
      arrays, and I want button visible if Array exist. data-bind"visible: exist Array1..2"



      <div class=«header»>
      <!— ko if: typeof Array1 !== 'undefined' —>
      <button data-toggle="collapse" href="#containerLoan<?= $key + 1 ?>» aria-expanded=" false" aria-controls="containerLoan<?= $key ? >">button1</button>
      <!-- /ko -->
      <!— ko if: typeof Array2 !== 'undefined' —>
      <button data-toggle="collapse" href="#containerLoan<?= $key + 2 ?>»
      aria-expanded=" false
      " aria-controls="containerLoan<?= $key ?>">button2</button>
      <!-- /ko -->
      </div>

      <div class=«loandata» data-bind='component: { name: «test-component", params: { Id: <?= $model->Id ?>} }'></div>

      <script type="text/html" id=«test-template">

      <div class="collapse multi-collapse" id="containerLoan<?= $key + 1 ?>">
      <div class="card card-body">
      <!-- ko if: Array1.length > 0 -->
      <div>…</div>
      <!-- /ko -->
      </div>
      </div>

      <div class="collapse multi-collapse" id="containerLoan<?= $key + 2 ?>">
      <div class="card card-body">
      <!-- ko if: Array2.length > 0 -->
      <div>…</div>
      <!-- /ko -->
      </div>
      </div>

      </script>


      In *.ts
      namespace Test.One {



      export class TestViewModel {

      Array1: KnockoutObservable<Product> = ko.computed(() => { … return …; }
      Array2: KnockoutObservable<Product> = ko.computed(() => { … return …; }

      private fetch(id: number): void {
      $.getJSON(…)
      }

      constructor(params: { Id: number }) {
      this.Id(params.Id);
      this.fetch(params.Id);
      }
      export class ViewModel {
      constructor() {
      ko.components.register(‘test-component', {
      viewModel: params => {
      return new TestViewModel(params);
      },
      template:{
      element :
      'hint-loan-product-template'
      },
      });
      }
      }

      export let viewModelObject = new ViewModel();
      ko.cleanNode(document.getElementById(‘test-area’));
      ko.applyBindings(viewModelObject);//, document.getElementById(‘test-area'));


      }







      knockout.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 15:41







      Tom

















      asked Nov 14 '18 at 9:26









      TomTom

      43




      43
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The best solution is to explicitly pass a reference to the isVisible observable via the component's params. This way, you don't create a hard dependency that forces the component to only be used within binding contexts that contain a certain property.



          Here's an example, using some component from the knockout docs




          • The outer viewmodel contains a visibility control property: outerIsVisible

          • When creating the component, we tell it which observable controls the visibility

          • The component's viewmodel has its own isVisible which references the one in params

          • The component's view references its own viewmodel's property, of which we can be sure it's always defined.





          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>








          share|improve this answer
























          • thanks, I changed the my example in post and question

            – Tom
            Nov 14 '18 at 11:39











          • Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

            – user3297291
            Nov 14 '18 at 11:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296816%2fhow-get-knockout-template-outer-component%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The best solution is to explicitly pass a reference to the isVisible observable via the component's params. This way, you don't create a hard dependency that forces the component to only be used within binding contexts that contain a certain property.



          Here's an example, using some component from the knockout docs




          • The outer viewmodel contains a visibility control property: outerIsVisible

          • When creating the component, we tell it which observable controls the visibility

          • The component's viewmodel has its own isVisible which references the one in params

          • The component's view references its own viewmodel's property, of which we can be sure it's always defined.





          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>








          share|improve this answer
























          • thanks, I changed the my example in post and question

            – Tom
            Nov 14 '18 at 11:39











          • Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

            – user3297291
            Nov 14 '18 at 11:42
















          1














          The best solution is to explicitly pass a reference to the isVisible observable via the component's params. This way, you don't create a hard dependency that forces the component to only be used within binding contexts that contain a certain property.



          Here's an example, using some component from the knockout docs




          • The outer viewmodel contains a visibility control property: outerIsVisible

          • When creating the component, we tell it which observable controls the visibility

          • The component's viewmodel has its own isVisible which references the one in params

          • The component's view references its own viewmodel's property, of which we can be sure it's always defined.





          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>








          share|improve this answer
























          • thanks, I changed the my example in post and question

            – Tom
            Nov 14 '18 at 11:39











          • Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

            – user3297291
            Nov 14 '18 at 11:42














          1












          1








          1







          The best solution is to explicitly pass a reference to the isVisible observable via the component's params. This way, you don't create a hard dependency that forces the component to only be used within binding contexts that contain a certain property.



          Here's an example, using some component from the knockout docs




          • The outer viewmodel contains a visibility control property: outerIsVisible

          • When creating the component, we tell it which observable controls the visibility

          • The component's viewmodel has its own isVisible which references the one in params

          • The component's view references its own viewmodel's property, of which we can be sure it's always defined.





          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>








          share|improve this answer













          The best solution is to explicitly pass a reference to the isVisible observable via the component's params. This way, you don't create a hard dependency that forces the component to only be used within binding contexts that contain a certain property.



          Here's an example, using some component from the knockout docs




          • The outer viewmodel contains a visibility control property: outerIsVisible

          • When creating the component, we tell it which observable controls the visibility

          • The component's viewmodel has its own isVisible which references the one in params

          • The component's view references its own viewmodel's property, of which we can be sure it's always defined.





          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>








          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>





          ko.components.register('message-editor', {
          viewModel: function(params) {
          this.isVisible = params.isVisible;
          this.text = ko.observable(params && params.initialText || '');
          },
          template: `<div data-bind="visible: isVisible">
          Message: <input data-bind="textInput: text" />
          (length: <span data-bind="text: text().length"></span>)
          </div>`
          });

          ko.applyBindings({
          outerIsVisible: ko.observable(true)
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

          <label>
          <input type="checkbox" data-bind="checked: outerIsVisible">
          Show message editor
          </label>

          <div data-bind='component: {
          name: "message-editor",
          params: {
          initialText: "Hello, world!",
          isVisible: outerIsVisible
          }
          }'></div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 10:34









          user3297291user3297291

          15.8k11634




          15.8k11634













          • thanks, I changed the my example in post and question

            – Tom
            Nov 14 '18 at 11:39











          • Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

            – user3297291
            Nov 14 '18 at 11:42



















          • thanks, I changed the my example in post and question

            – Tom
            Nov 14 '18 at 11:39











          • Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

            – user3297291
            Nov 14 '18 at 11:42

















          thanks, I changed the my example in post and question

          – Tom
          Nov 14 '18 at 11:39





          thanks, I changed the my example in post and question

          – Tom
          Nov 14 '18 at 11:39













          Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

          – user3297291
          Nov 14 '18 at 11:42





          Your new example can be solved by using the same approach: pass references to Array1 and Array2 to the component. Of course, you can also use $parent.Array1 in your component template, but this breaks the whole idea of using components.

          – user3297291
          Nov 14 '18 at 11:42


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296816%2fhow-get-knockout-template-outer-component%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python