1

I have a application where we have a survey that we want to add rules and conditions to. As a result I am trying to add values to a JSON object that is returned to the client upon loading. I am using ng-repeat to loop through an array that represents "rules" within this object which also have arrays representing "conditions" that will be displayed as elements of the first array. I am trying to figure out how to represent the new elements that won't have ids until it is submitted to the server and added to the database and still have them represented by the forms generated in angular:

{
  "rules": [
    {
      "ruleId": 1235,
      "questionId": 5,
      "effect": "show",
      "conditions": [
        {
          "conditionId": 1,
          "questionId": 1,
          "operator": "==",
          "value": 2
        },
        {
          "conditionId": 2,
          "questionId": 2,
          "operator": "~=",
          "value": "Hide me"
        }
      ]
    },
    {
    }
  ]
}

The following data is rendered in the html like this:

<div ng-repeat="rule in survey.rules" class="rule">
    <!-- Display the form for modifying data of rule -->
    <div ng-repeat="condition in rule.condition">
        <!-- The condition details -->
    </div>
    <a ng-click="survey.addCondition(rule)>(+) Add Condition</a>
</div>
<a ng-click="survey.addRule()>(+) Add Rule</a>

I created a way to add to the array for rules and their array of conditions but when I try to add more than one I get an id conflict since the app is using ruleId and conditionId for indexing. I don't want to assign random ids to the application's data without having an actual id but there may be a better way I can do this so I can pass them to the server and have them save in the database.

2
  • 1
    Do you need to store the ids client side? You could assume that any condition that gets sent back to the server is a new one. Or try using negative numbers or something else that you'd never return from the server? Commented Oct 27, 2014 at 17:16
  • I am a bit new to angular so I am still getting used to the whole idea behind it. The back end is returning the ids because once we are done adding rules/conditions, we use the ids to update the details in the database. Since Angular is throwing an issue with the assigning of multiple values with a null id, I figured I would need to find a way to add to the array without having duplicate keys that has been the root of the issue. Commented Oct 27, 2014 at 17:38

0

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.