-1

I have problem in parsing my model to JSON:

    var UserViewModel = function (UserName, Password) {
        this.UserName = ko.observable(UserName);
        this.Password = ko.observable(Password);


    this.loginClick = function () {

        var strJSON = ko.toJSON(this);

alert(strJSON); }; };

    ko.applyBindings(UserViewModel);

Alert gives empty dialog box,

if i pass arguments to ko.toJSON

var strJSON = ko.toJSON({
    'UserName': this.UserName,
    'Password': this.Password
});
alert(strJSON);

then it is giving right json string.

Hitesh Modha
  • 2,554
  • 5
  • 26
  • 47
  • Also highly related: [Difference between knockout View Models declared as object literals vs functions](http://stackoverflow.com/questions/9589419/difference-between-knockout-view-models-declared-as-object-literals-vs-functions?rq=1) – nemesv May 13 '14 at 07:27
  • If you have some other specif problem with your JSON parsing then edit your question with the concrete problem, error messages, etc. and I will vote to reopen your question. – nemesv May 13 '14 at 07:30
  • 1
    The question is still rather unclear to me tbh. The last edit only changed the title. You speak of "self" which is not in your code, and of a "problem" but you don't state what the problem is. Please be specific about your issue and make sure the code in the question helps us repro the issue. – Jeroen May 13 '14 at 07:42

1 Answers1

1

http://plnkr.co/edit/yJqmQE9WXVLODTrYbtQ0?p=preview

The this in the loginClick function doesn't point to UserViewModel.

And your ko.applyBindings is incorrect.

Jason Li
  • 1,358
  • 10
  • 19