My json object, "flowComponents" contains a string (name) and an array of strings (edition). For example:
{
"_id": "553e87f3205465e83b46999b",
"name": "FLOWCOMPONENT_CONTACTCOMBINATION_EDITION",
"__v": 0,
"edition": [
"billing",
"billingDelivery",
"default",
"deliveryAddressOnly",
"deliveryBillingLicensee",
"deliveryBillingLicenseeWithWrapper",
"deliveryLicensee",
"deliveryOnlyCopyToAll",
"licenseeDelivery",
"sassDefault",
"sassDeliveryOnlyCopyToAll"
]
}
I need to add/concat another edition to this existing flowComponents object. My form has a dropdown with the names of the existing flowComponents, and a text area that makes an array of each line of text:
<form ng-submit="addToExistingFlowComponent()">
<div class="interact">
<select ng-model="existingName" chosen options="flowComponents" ng-options="item as item.name for item in flowComponents" data-placeholder="Select a flow component...">
</select>
</div>
<div class="interact">
<label class="interact-label">Enter each edition on a new line.</label>
<textarea id="text_area" placeholder="Edition" ng-model="existingEditionList" ng-list=" " ng-trim="false"></textarea>
</div>
<button type="submit">Submit</button>
</form>
This is the add edition method in my controller:
$scope.addToExistingFlowComponent = function(){
if(!$scope.existingName || $scope.existingName === '') { return; }
var existingFC = $scope.existingName._id;
sendAppData.postEdition( existingFC, {
edition: $scope.existingEditionList
});
$scope.existingName = '';
$scope.existingEditionList = '';
};
And this is the method that posts the data to the server:
this.postEdition = function(existingFC, newEdition) {
return $http.post('/new-flow-component', newEdition).success(function(data){
flowComponents.push(data);
});
};
The problem is, this is pushing the data to a new object rather than adding to the existing object. I'm able to pass the _id of the existing object to the existingFC parameter, but I can't figure out how to get that inside of function(data) in order to push it into the correct edition array.
.push()
doesn't work on an object, so you can't push ontoflowComponents
. IfflowComponents
is a single object that gets updated (not duplicated) then you want to do as @shivas suggested. Another question is how does the postedition method know whatflowComponents
is? It is not being passed in and if it belongs to the controller you are going to have to refer to it like$scope.flowComponents
or something like that.