1

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..

2 Answers 2

1

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);
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually,, yes, since it is a kineticjs stage with possible unlimited layers and groups of objects recursion makes sense. I created a fiddle to test jsfiddle.net/BpmTU/4 and your suggestion seems to work good. Thank you.
1

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;
});

1 Comment

I am not sure that I follow. I created a fiddle to try it out but i am not getting anything jsfiddle.net/8Y6zZ/5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.