can't get knockout object property value

When I try to get the value of foo.Id in the click event (removeAFoo -> removeFoo) in the I get function text 'function b(){if(0...'. What needs to change to get the property value of foo.Id? Another weird thing is when I call indexOf() on viewModel.foos array it returns the index even though that array says it's zero length.

function getFoos() {
    viewModel.foos([]);
    var data = [{
        Id: 1,
        Name: 'Joe' },
    {
        Id: 2,
        Name: 'Jon' },
    {
        Id: 3,
        Name: 'Jim' }        
    ]

    for (var i = 0; i < data.length; i++) {
        var f = new foo();
        f.Id(data[i].Id);
        f.Name(data[i].Name);

        viewModel.foos.push(f);
    }
};

var viewModel = {
    foos: ko.observableArray([]),

    reloadFoos: function() {
        getFoos();
    },

    removeAFoo: function(foo) {
        removeFoo(foo);
    }
};

var foo = function() {
    this.Id = ko.observable();
    this.Name = ko.observable();
};

function removeFoo(foo) {
    alert(viewModel.foos.indexOf(foo));
    alert(foo.Id);
    viewModel.foos.splice(viewModel.foos.indexOf(foo), 1);
}

ko.applyBindings(viewModel);

getFoos();
​

<div data-bind="foreach: foos">
    <div style="float: left">
        <button data-bind="click : $parent.removeAFoo, attr: { id: Id }">
        </button>
        <label data-bind="text: Name">
        </label>
    </div>
</div>​

http://jsfiddle.net/suedeuno/gAUgV/12/

Answers


To get the value of foo.id you need to remember to use parentheses:

like so:

document.write(foo.Id());


Need Your Help

Sort Hashtable by (possibly non-unique) values

c# hashtable

I have a Hashtable that maps strings to ints. Strings are unique, but several may be mapped to the same integer.

How to handle Back button while using DrawerLayout in activity in android?

android drawerlayout

I am using DrawerLayout in my application. Now when I pressed on back button first of all it is closing DrawerLayout and need to click again on back button then it will redirect to previous screen....