0

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.

Thomas Deutsch
  • 2,344
  • 2
  • 27
  • 36

2 Answers2

1

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.

JohnB
  • 13,315
  • 4
  • 38
  • 65
  • Thank you, but this is not helping. The problem is that x is undefined - and i do not know why. Btw. x.Name would return the observable function. x.Name() is maybe the right call. – Thomas Deutsch Jun 03 '12 at 17:45
  • This was my solution. Than i wanted to have more reusable code - so i moved the customer out of the function and replaced it with the x property. so i can use every object with a Name-Property to use this function. This is a best practice i need to have. – Thomas Deutsch Jun 03 '12 at 18:14
0

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

Community
  • 1
  • 1
Thomas Deutsch
  • 2,344
  • 2
  • 27
  • 36