Pass object from array when clicked in knockout.js

I am new to knockout.js and I have missed something simple here, I am sure. I have a list of objects in the list, and I just want to select one of them and get the property to load some more data from it.

JS:

 function category(data) {
                this.categoryName = ko.observable(data.CategoryName);
                this.categoryID = ko.observable(data.CategoryID);
            }

            function CatalogViewModel() {

                //Data
                var self = this;
                self.categories = ko.observableArray([]);
                self.chosenCategoryID = ko.observable();

                //Behaviours
                self.gotoCategory= function (category) {
                    self.chosenCategoryID(category.categoryID);
                    alert(category.categoryID);
                };

                $.getJSON("/api/catalog", function (allData) {
                    var mapped = $.map(allData, function (item) { return new category(item) });
                    self.categories(mapped);
                });
            }

            ko.applyBindings(new CatalogViewModel());

Here is my HTML:

 <ul data-bind="foreach: categories">
            <li>
                <a  data-bind="click: $parent.gotoCategory"><label data-bind="text: $data.categoryName"></label></a>
            </li>
        </ul>

The list of elements is fine, but then when I click on the element, I hope to get my category identifier, but instead I get a bunch of javascript that looks like a function trying to evaluate and argue.

What am I missing here?

EDIT - HERE THAT I GOT IN MY OTHER:

function c(){if(0<arguments.length){if(!c.equalityComparer||!c.equalityComparer(d,arguments[0]))c.I(),d=arguments[0],c.H();return this}a.U.La(c);return d}function c(){if(0<arguments.length){if(!c.equalityComparer||!c.equalityComparer(d,arguments[0]))c.I(),d=arguments[0],c.H();return this}a.U.La(c);return d}
+3
source share
1 answer

It looks like yours categoryIDis observable. To access the value of the observable, you need to call it as a function with no arguments.

, gotoCategory :

            self.gotoCategory= function (category) {
                self.chosenCategoryID(category.categoryID());
                alert(category.categoryID());
            };
+4

All Articles