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>
> 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?
> On Wednesday, September 5, 2012 4:57:00 AM UTC-4, Benjamin Gudehus wrote:
>> Maybe your viewModel is a function?
>> > console.log(typeof(viewModel))
>> function
>> 2012/9/5 <mr.brad....@gmail.com>
>> 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?