Trouble creating object from Json using Knockout

Fulmetal5

Ok, the scenario is that I have a list of objects that itself contains a list of objects. I am trying to put the outer objects in a list in my View-Model and I'm trying to turn the inner objects into an object I created as a model with an extra property, like so -

function Tag(data, isChecked) {
    var TagId = ko.observable(data.TagId);
    var Description = ko.observable(data.Description);
    var IsActive = ko.observable(data.IsActive);
    var checked = ko.observable(isChecked)
}

Here is how i'm doing it.

   $.each(parentTags, function (i, tag) {
    var tempTag = tag[i];
    for (Object in tag.ChildTags)
    {
        var checked = false;
        for(checkedItem in savedTags)
        {
            if(tag.ChildTags[Object].TagId === savedTags[checkedItem])
            {
                checked = true;
            }
        }
        //var tempChild = new Tag(tag.ChildTags[Object], checked);
        tempTag.ChildTags.push(new Tag(tag.ChildTags[Object], checked));
    }
    viewModel.ModelTags.push(tempTag);
});

parentTags = [[object Object],[object Object],[object Object],[object Object],[object Object],]

I'm definitely new to json and knockout. Any help would be appreciated.

This is what I have now and it seems to be working.

    $.each(parentTags, function (i, tag) {
    var tempTag = new Tags(JSON.parse(JSON.stringify(tag)));
    tempTag.ChildTags().length = 0;

    for (Object in tag.ChildTags) {
        var checked = false;
        for (checkedItem in savedTags) {
            if (tag.ChildTags[Object].TagId === savedTags[checkedItem]) {
                checked = true;
            }
        }

        tempTag.ChildTags().push(new Tag(tag.ChildTags[Object], checked));
    }
    viewModel.ModelTags().push(tempTag);
});
huocp

What are you doing with var tempTag = tag[i];? The variable tag is already the i-th member of parentTags.

If I understand you correctly, try this:

function Tag(data, isChecked) {
   // not "var TagId", please.
   this.TagId = ko.observable(data.TagId);
   this.Description = ko.observable(data.Description);
   this.IsActive = ko.observable(data.IsActive);
   this.checked = ko.observable(isChecked);
}

$.each(parentTags, function (i, tag) {
    // map each child tag into a Tag object
    var tags = $.map(tag.ChildTags, function(cTag) {
        return new Tag(cTag, $.inArray(cTag.TagId, savedTags));
    });

    // replace with Tag objects
    tag.ChildTags = tags;

    // push modified tag onto vm
    viewModel.ModelTags.push(tag);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating an observable knockout array from a JSON response

From Dev

Exception while creating Java object from JSON using fasterxml

From Dev

Creating an ArrayList from a JSON Object

From Dev

Creating Json object from array

From Dev

Trouble creating Object Data Source

From Dev

Knockout - Having trouble accessing nested JSON data

From Dev

knockout js add values to array using json object

From Dev

Bind complex JSON object using knockout.js

From Dev

Trouble creating array of JSON objects

From Dev

Trouble creating array of JSON objects

From Dev

Creating a jSON object using php loop

From Dev

Creating a jSON object using php loop

From Dev

Creating Complex JSON Object using JavaScript Code

From Dev

Creating a nested JSON object from SQL results

From Dev

Creating JS Object with methods from JSON

From Dev

Creating json object in mvc and returning from controller

From Dev

Creating php classes tree from a json object

From Dev

Creating a JSON object from Javascript array

From Dev

Elixir - Creating JSON object from 2 collections

From Dev

Error Creating Data object from JSON

From Dev

Creating a json object from nsmutable array

From Dev

Creating JSON object from language file

From Dev

Creating JSON object from Typescript class

From Dev

Trouble with creating Battleship using Java

From Dev

Knockout bindings from a JSON

From Dev

Having trouble getting access to the Object in JSON from url

From Dev

Having trouble getting access to the Object in JSON from url

From Dev

Using Knockout to update the view from JSON via click event

From Dev

Create table from Json response using Knockout.js

Related Related

HotTag

Archive