Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
ko.toJSON Issues
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mr.brad.lawre...@gmail.com  
View profile  
 More options Sep 5 2012, 1:12 am
From: mr.brad.lawre...@gmail.com
Date: Tue, 4 Sep 2012 22:12:48 -0700 (PDT)
Local: Wed, Sep 5 2012 1:12 am
Subject: ko.toJSON Issues

I am attempting to use ko.toJSON to prep my data before I send it to the
server and I am unable to get the data.

in HTML <pre data-bind="text: ko.toJSON($data, null, 2)"></pre>  works
great!  

BUT in javascript ko.toJSON(viewModel) is returning undefined!  WHY?

Also if I use ko.toJS(viewModel) it works but I get all the function code.

Anyone have any ideas?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Gudehus  
View profile  
 More options Sep 5 2012, 4:57 am
From: Benjamin Gudehus <hasteb...@gmail.com>
Date: Wed, 5 Sep 2012 10:56:37 +0200
Local: Wed, Sep 5 2012 4:56 am
Subject: Re: ko.toJSON Issues

Maybe your viewModel is a function?

> console.log(typeof(viewModel))

function

2012/9/5 <mr.brad.lawre...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mr.brad.lawre...@gmail.com  
View profile  
 More options Sep 5 2012, 8:54 am
From: mr.brad.lawre...@gmail.com
Date: Wed, 5 Sep 2012 05:54:48 -0700 (PDT)
Local: Wed, Sep 5 2012 8:54 am
Subject: Re: ko.toJSON Issues

Yes it is a function.   What are my options?  Do I have to recode it to a
variable or is there a less painful way?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rpn  
View profile  
 More options Sep 5 2012, 9:13 am
From: rpn <rnieme...@gmail.com>
Date: Wed, 5 Sep 2012 06:13:06 -0700 (PDT)
Local: Wed, Sep 5 2012 9:13 am
Subject: Re: ko.toJSON Issues

Maybe you can share some of your code.  Otherwise, if you are using a
constructor function and your function that does ko.toJSON is on the view
model, then you can call* ko.toJSON(this)* or *ko.toJSON(self)* if you
saved off the value of this.

like:

var ViewModel = function() {
   this.name = ko.observable("Bob");

   this.save = function() {
       alert(ko.toJSON(this));
   };


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Gudehus  
View profile  
 More options Sep 5 2012, 12:23 pm
From: Benjamin Gudehus <hasteb...@gmail.com>
Date: Wed, 5 Sep 2012 18:22:48 +0200
Local: Wed, Sep 5 2012 12:22 pm
Subject: Re: ko.toJSON Issues

The two options to define a view model is to use object notation or a
constructor function.

// object notation.
var viewModel = {name: ko.observable("Bob")}
ko.applyBindings(viewModel)
console.log(ko.toJSON(viewModel))

// contructor function.
var ViewModel = function() {
    this.name = ko.observable("Bob")

}

var viewModel = new ViewModel()
ko.applyBindings(viewModel)
console.log(ko.toJSON(viewModel))

By convention variable names of constructor functions should be like class
names in e.g. Java and C#, so everyone can see, that one has to use the new
keyword.

I think you just mixed the constructor function with the instantiated
object.

--Benjamin

2012/9/5 <mr.brad.lawre...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mr.brad.lawre...@gmail.com  
View profile  
 More options Sep 5 2012, 9:14 pm
From: mr.brad.lawre...@gmail.com
Date: Wed, 5 Sep 2012 18:14:52 -0700 (PDT)
Local: Wed, Sep 5 2012 9:14 pm
Subject: Re: ko.toJSON Issues

Thanks, I had blended a couple of examples and lost track of what I was
doing, I will watch my notation use moving forward.

What fixed it was proper use of the constructor so I had a viewModel
variable.  

Working well now, much appreciated!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »