javascript - angular: deep scope traversal and updating -
i have angular scope looks this:
{ node_type: 100, node_instance: 001, value: 'pony', show: true, childnodes: { node_type: 100, node_instance: 011, value: 'cat', show: false, childnodes: { node_type: 101: node_instance: 001, value: 'apple'show: true, } node_type: 100: node_instance: 012, value: 'kitty', show: true, node_type: 100: node_instance: 013, value: 'meow', show: true, } node_type: 100, node_instance: 002, value: 'horse', show: true, childnodes: { node_type: 102: node_instance: 001, value: 'dog', show: false, node_type: 100: node_instance: 014, value: 'mutt', show: true, } }
it's (much) deeper , more complex this.
upon making various actions (say, changing "mutt" "fido"), server return newly updated, seemingly unrelated data in set like…
{ node_type:101, node_instance:001, show:false }
i've found various ways of traversing scope find 101,001 node, involve sending copies of scope nodes traversal function, updating properties in copy worthless. how can tell function update scope @ correct address, or update node properties reference?
(or solution dumb setting data="show" property in dom, updating dom outside of angular, , setting scope.apply() request? i've had bad luck scope.apply()…)
the solution twofold.
first, deep-traverse $scope.data (which stores json), per this example.
second, update via reference ajay describes.
using traverse() function above…
for( var = 0; < response.nodes.length; i++ ) { traverse( $scope.data, response.nodes[i], function(key, val, update) { if( val != null && typeof val.node_type != 'undefined' && update.node_type == val.node_type && update.node_instance == val.node_instance ) { val.show = update.show; } } ); }
thank ajax assistance, , stack overflow @ large. terrific world live in.
Comments
Post a Comment