Trouble with updating object properties in AngularJs -


i building first app in angularjs.

here plunkr i've done far. user should able add new websites , group them in groups. groups made user. time new group created available new websites. app should update group objects newly assigned websites... , fail.

here how json should like:

{ "sites": [   {     "url": "http://sfdg",     "id": 0,     "groups": [       {         "name": "adsf",         "id": 0       }     ]   } ], "groups": [   {     "name": "adsf",     "id": 0,     "sites": [//sites assigned      ]   } ] 

}

in plunkr code used push adds new group...

could please direct me right way of achieving this.

thanks!

to prevent circular references (a website object refers group object refers website object, etc...), store id's relevant objects instead.

first, when creating new group, add empty sites array it:

function creategroup(newgroup) {   newgroup.sites = [];          // <-- add empty array of sites   $scope.groups.push(newgroup);   newgroup.id = groupidseq;   groupmap[newgroup.id] = newgroup;   groupidseq++;   return newgroup; } 

then, when create new site, update each group site added:

  function createsite(newsite, groups) {     $scope.sites.push(newsite);     newsite.id = siteidseq;     sitesmap[newsite.id] = newsite;      // instead of storing groups array, store id:     newsite.groups = groups.map(function(group) { return group.id });      // , add new sites id groups' sites array.     groups.foreach(function(group) {       group.sites.push(newsite.id);     });      siteidseq++;     return newsite;   } 

(updated plunker here)


Comments