Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Knockout vincula 2 enlaces a diferentes vistas y cambia la vista del modelo

Soy nuevo en knockout y quiero tener 2 enlaces en la página con 2 modelos. Me gustaría que el evento de clic en los enlaces cambie el modelo de vista que se muestra. Lo intenté de varias maneras y tengo un jfiddle https://jsfiddle.net/edgrttj3/7/ pero no puedo hacer que funcione. ¿Alguna idea de lo que podría estar haciendo mal?

 <div id="content"> <a id="button1" href="#" >View 1</a>&nbsp; <a id="button2" href="#" >View 2</a>&nbsp; <hr /> <div data-bind="with: selectedView"> <div data-bind="template: { name: templateName, data: data }"></div> </div> <script id="oneTmpl" type="text/html"> <ul data-bind="foreach: items"> <li> <span data-bind="text: id"></span> <input data-bind="value: name" /> </li> </ul> </script> <script id="twoTmpl" type="text/html"> First: <input data-bind="value: firstName" /> Last: <input data-bind="value: lastName" /> </script> </div>

Aquí está el javascript:

 var View = function (title, templateName, data) { this.title = title; this.templateName = templateName; this.data = data; }; var subModelA = { items: ko.observableArray([ { id: 1, name: "one" }, { id: 2, name: "two" }, { id: 3, name: "three" } ]) }; var subModelB = { firstName: ko.observable("Bob"), lastName: ko.observable("Smith") }; var viewModel = { views: ko.observableArray([ new View("one", "oneTmpl", subModelA), new View("two", "twoTmpl", subModelB) ]), selectedView: ko.observable() }; function setSelectedView(newview) { alert(newview); if (newview == "oneTmpl") { viewModel.selectedView = viewModel.views[0]; } else { viewModel.selectedView = viewModel.views[1]; } }; ko.applyBindings(viewModel); document.getElementById ("button1").addEventListener("click", setSelectedView('oneTmpl'), false); document.getElementById ("button1").addEventListener("click", setSelectedView('twoTmpl'), false);
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En realidad, su implementación está casi lista. Solo hay un par de ajustes que debes hacer.

  • selectedView del ViewModel es un objeto observable. Un objeto observable es básicamente una función. Para modificar el valor de ese objeto, debe pasar el nuevo valor como un argumento selectedView(valueIsHere) .
  • vistas desde ViewModel es un objeto de matriz observable. Lo mismo que arriba, como un objeto observable es una función para obtener su valor para un índice específico, primero debe llamarlo como una función seguida de sus views()[0]

Así es como se ve después de la actualización:

 var View = function (title, templateName, data) { this.title = title; this.templateName = templateName; this.data = data; }; var subModelA = { items: ko.observableArray([ { id: 1, name: "one" }, { id: 2, name: "two" }, { id: 3, name: "three" } ]) }; var subModelB = { firstName: ko.observable("Bob"), lastName: ko.observable("Smith") }; var ViewModel = function (){ this.views = ko.observableArray([ new View("one", "oneTmpl", subModelA), new View("two", "twoTmpl", subModelB) ]); this.selectedView = ko.observable(); this.select = function(id){ if (id === 1) { this.selectedView(this.views()[0]); } else { this.selectedView(this.views()[1]); } }; }; ko.applyBindings(new ViewModel());

Y aquí hay algunos cambios en el botón:

 <a id="button1" href="#" data-bind="click: select(1)">View 1</a>&nbsp; <a id="button2" href="#" data-bind="click: select(2)">View 2</a>&nbsp;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!