Question

I am using the Revealing Module Pattern to get some structure in my knockout.js code. It is a very simple Example Goal: return the value of the Name-Property of the Object. Problem: The function parameter x is undefined.

http://jsfiddle.net/ThomasDeutsch/8hzhp/

What exactly is the problem here? Help me fiddle this one out please.

Was it helpful?

Solution 2

I have the solution. The Problem was that i have not defined a function. ko.computed will not do the job. So this is the solution: Knockout.js: Function parameter undefined

OTHER TIPS

Perhaps you should write

x.Name

instead of

x.getElementsByName('Name')

since I do not see where x should obtain this method from, as x is not an element of the document tree. But I am not an expert on this.

Ok, this works for me:

// My Model
function Customer(id, name, lastname) {
    this.Id = ko.observable(id);
    this.Name = ko.observable(name);
    this.LastName = ko.observable(lastname);
}

// My ViewModel
ViewModel = (function () {
    var customer = new Customer(1, "Thomas", "D")
    var getName = ko.computed(function () {
        return customer.Name ();
    })
    ;
    return {
        getName: getName
    };
})();

ko.applyBindings(ViewModel);

The getName in the return statement must be a function, not the result of a function. Probably the framework (which I do not know) calls the function (without arguments) in order to obtain the value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top