javascript - knockout computed object not working when working with JSON -


I'm new to knockout, the problem is that I try to load the object from Jason and computed Please use the value also please see that jsfiddle example and let me know what I am doing, it should display "Hello World" + "Start Date" (in the third line)

  self Starttextt = ko.computed (function () {return "Hello world" + this.start ();}, this); It is noteworthy that this calculation remains in the object "events" object (which is full of JSN) while "event" objects live in the ViewModel object.  

Appreciate your help, thank you

There are problems:

You need to fix your event function to take some parameters to fill with the data:

  function event (data) ) {Var self = this; Self.title = ko.observable (data.title); Self.start = ko.observable (data.start); Self.starttextt = ko.computed (function () {return "hello world" + this.start ();}, this); }  

then writing var data = [event]; Create an array with the event function and it does not in any way mapping you objs on the array map Need to call:

  var objs = JSON.parse (js); Var data = objs.map (function (e) {new event (e)});  

and you should use data instead of obj

  var viewModel = in your code Requires = function () {var itself = this; Self.events = ko.observableArray (data); };  

Demo

However, you do not need to write these mapping code because there is already a plugin that is designed to control such situations.

Your sample has been re-written with the mapping plugin:

  var mapping = {create: function (options) {var event = ko.mapping.fromJS ( Options.data); Event.starttextt = ko.computed (function () {returns "hello world" + event.start ();}, event); Event of return; }} Var viewModel = function () {var self = this; Self.events = ko.mapping.fromJSON (JS, Mapping); };  

Demo


Comments