Appcelerator REST Complex Custom Object Update

58 views Asked by At

All,

TL:DR

  • Appcelerator Cloud Services
  • REST
  • Custom Object with arrays
  • Update property in an array using $inc atomic increment operator

Details:

I am using AngularJS to access Appcelerator Cloud Services using REST. I have a complex custom object (see Gist links below). My custom collection will contain one of these objects per event. Each event has teams, and each team has contestants. Each team and contestant competes in event segments, and the audience votes on team and contestant performance. I want to update team and contestant scores, but I can't figure out how I need to set the fields value in order to update scores.

I've created a gist with all my files that are involved in this issue. If anyone could please provide some assistance I'd appreciate it.

2

There are 2 answers

0
jaxmeier On BEST ANSWER

I gave up and changed the score object to a flat document. I now have a score document per event/team/segment or event/contestant/segment. This should allow the score property to be updated easily since there are now no hashes or arrays in the score document.

2
Taku On

Demo

Let the whole array you have be called 'arr';

var arr= {....};

Let's deal with the segments, located 3rd key from the top. Then in your HTML, it would look something like this:

    <table>
        <tr ng-repeat="segment in arr[0].segments">
            <td ng-bind="segment.name"></td>
            <td>
                <input type="text" ng-model="segment.name">
            </td>
            <td ng-bind="segment.score"></td>
            <td style='cursor:pointer;' ng-click="segment.score = segment.score+1">Add Score</td>
        </tr>
    </table>

And your controller would be something like:

var TodoCtrl = function ($scope) {
    $scope.arr = arr;
}

It's just an array of objects so think of it like an array of string or a number but with properties. For example, if you want to output all the contestants in the second array, you'd be doing something like

arr[1].contestants.forEach(function(element, index, object){
    console.log(element.id, element.name); // name would start with Gene Collier
    element.segments.forEach(function(_el){
            console.log('name', _el.name, 'score', _el.score);
    });
}

I hope this helps.