knockout.js - Clicked table row bound via knockoutjs gets not selected -


i want make simple table row selectable clicking on here:

http://jsfiddle.net/rniemeyer/apzk8/6/

i applied above logic still nothing selected, wrong?

the data displayed correctly, selecting not work.

define(['services/dataservice'], function (dataservice) {      var self = this;     this.selected = ko.observable();     var schoolyears = ko.observablearray();      this.selectschoolyear = function (config) {         self.selected(config);     };      this.selected(schoolyears()[0]);      var vm = {         activate: activate,         schoolyears: schoolyears,         title: 'schoolyears'         };     return vm;      function activate(){         var schoolyearmodels = dataservice.getschoolyears();         var schoolyearviewmodels = [];         (var = 0; < schoolyearmodels.length; i++){             var e = schoolyearmodels[i];             var schoolyearviewmodel = new schoolyearviewmodel(e.schoolyearid, e.schoolyearname, e.from, e.to, e.lastedited, self.selected);             schoolyearviewmodels.push(schoolyearviewmodel);         }         return schoolyears(schoolyearviewmodels);     }     function schoolyearviewmodel(id, schoolyearname, from, to, lastedited, selected){         var me = this;         this.schoolyearid = id;         this.schoolyearname = ko.observable(schoolyearname);         this.from = ko.observable(from);         this.to = ko.observable(to);         this.lastedited = ko.observable(lastedited);         this.amiselected = ko.computed(function (){             debugger;             return selected() === me;         });     }    });   <section id="schoolyears-view" class="view">     <a class="btn btn-info btn-force-refresh pull-right" data-bind="click: remove" href="#"><i class="icon-remove"></i>delete</a>      <table class="table table-striped table-bordered table-condensed">         <thead>             <tr>                 <th style="width: 25%">schoolyear name</th>                 <th style="width: 25%">from</th>                 <th style="width: 25%">to</th>                 <th style="width: 250%">last edited</th>             </tr>         </thead>         <tbody data-bind="foreach: schoolyears">             <tr data-bind="click: $parent.selectschoolyear, css: { selected: amiselected }, attr: { 'data-id': schoolyearid }" >                 <td data-bind="text: schoolyearname()"></td>                 <td data-bind="text: from()"></td>                 <td data-bind="text: to()"></td>                 <td data-bind="text: lastedited()"></td>             </tr>         </tbody>     </table> </section> 

the issue seems knockout looking remove , selectschoolyear methods on vm object, aren't present. they're on this object.

here's solution (note still need implementation remove):

var vm = {    activate: activate,    schoolyears: schoolyears,    title: 'schoolyears',    selectschoolyear: self.selectschoolyear,    remove: function () {} }; 

this assumes activate called somewhere.

vm.activate(); 

i've made working jsfiddle here.

note: view binding errors (like ones mentioned), use browser's developer console (knockout throw exceptions).


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -