GRowing
GRowing

Reputation: 4717

Angularjs - JSON object editing

I am pretty good at using existing data but I am not an expert on editing JSON objects quite yet so a little help here would do a lot for me. For all I know this could be a rudimentary question but I am asking because I don't know.

How would I update a value in JSON string that looks like below? It is a kineticjs object that I need to disable draggable elements in before I re-generate the stage.

I created a fiddle based on some input below but I am not successful at it: http://jsfiddle.net/8Y6zZ/5/

For example, I would like to set "draggable": false, wherever I have "draggable": true, ... How could I do that with javascript (I am working in angularjs).

{
    "attrs": {
        "width": 1276,
        "height": 660
    },
    "className": "Stage",
    "children": [
        {
            "attrs": {},
            "className": "Layer",
            "children": [
                {
                    "attrs": {
                        "x": 0,
                        "y": 0,
                        "draggable": false,
                        "name": "brochure-1.png"
                    },
                    "className": "Image"
                },
                {
                    "attrs": {
                        "x": 999,
                        "y": 288,
                        "draggable": true,
                        "name": "sample1.png",
                        "rotation": 0,
                        "scaleX": 1,
                        "scaleY": 1,
                        "offsetX": 0,
                        "offsetY": 0,
                        "skewX": 0,
                        "skewY": 0
                    },
                    "className": "Image"
                },
                {
                    "attrs": {
                        "x": 301,
                        "y": 115,
                        "draggable": true,
                        "name": "sample2.png",
                        "rotation": 0,
                        "scaleX": 1,
                        "scaleY": 1,
                        "offsetX": 0,
                        "offsetY": 0,
                        "skewX": 0,
                        "skewY": 0
                    },
                    "className": "Image"
                }
            ]
        }
    ]
}

Related to this question,, are there good ways to re-building JSON objects from "basic" JSON... I guess what I am asking is this... if a developer on the back end has no good way to translate data and they pass a badly organized JSON object, is there a way to manipulate the object to restructure it so it can be used well in angularjs..

Upvotes: 1

Views: 1588

Answers (2)

Austin Greco
Austin Greco

Reputation: 33554

Not sure on the json structure, but if you possibly have a bunch of child objects nested, you could use simple recursion to descend through all the children.. something like this:

function setDraggable(obj) {
  if(obj.attrs) {
    obj.attrs.draggable = false;
  }
  if(!obj.children) {
    return;
  }
  angular.forEach(obj.children, function(o) {
    setDraggable(o);
  });
}

Upvotes: 1

Zack Argyle
Zack Argyle

Reputation: 8427

Use angular's foreach. Then you just have to direct it to the list of children in your object. Something like this should work with replace YOUR_OBJECT with the name of your JSON object.

angular.forEach(YOUR_OBJECT.children.children, function(obj){
  obj.attrs.draggable = false;
});

Upvotes: 1

Related Questions