Friday 15 April 2011

jquery - having trouble convert getJSON data to model -


I can not seem to be working knockout when I get the storage from getJSON. Here is an example of my code

  function MyTestDodel () {this.modelProperty = ko.observable (); This.myCompute = ko.computed (function () {return "test";}); } MyViewModel () {var} = this; Self.myTestModels = ko.observableArray (); } Var vm = new MyViewModel (); Ko.applyBindings (VM); $ (Document) .ready (function () {$ .getJSON ("http://MyUrl.com/api/MyController", function (data) {var viewable data = to.mapping.fr JS (data); Var array = observation data (); Vm.myTestModels (array);});}); & Lt; Tbody data-bind = "foreach: myTestModels" & gt; & Lt; TR & gt; & Lt; Td data-bind = "text: model property" & gt; & Lt; / Td> & Lt; Td data-bind = "text: myCompute" & gt; & Lt; / Td> & Lt; / TR & gt; & Lt; / Tbody & gt; [{"ModelProperty": "My Property 1"}, {"Model Property": "My Property 2"}]   

What is the data for model property show, but chrome javascript The console says: Unacceptable error: Unable to parse binding message: ReferenceAir: My computer is not defined; Binding Value: Text: Microdoot

It appears to me.mapping.fromJS is not converting it into MyTestModel. I'm thinking because Knockout does not know about my test model, but I'm not sure How is that to tell about my model?

Still, thanks for everyone!

You did not specify that array items should be converted to MyTestModel.

It converts each array item into MyTestModel:

  $ (document) .ready (function () {$ .getJSON ("http: // MyUrl. Com / api / MyController ", function (data) {var array = ko Utils.arrayMap (data, function (item) {new MyTestModel (item.modelProperty);}); vm.myTestModels (array);});   

I hope this helps.

No comments:

Post a Comment