angularjsionic-frameworkbackand

Is it possible to update multiple json object using just one function and one button using angularjs?


Her's the HTML code

<ion-view view-title="Reviewer Title">
<ion-content>
  <div class="content no-header">
    <ion-nav-bar class="bar-assertive-900" align-title="left">
    <ion-nav-buttons side="left">
        <button class="button button-icon button-clear ion-android-arrow-back" ui-sref="app.reviewers"> </button>
    </ion-nav-buttons>

    <ion-nav-buttons side="right">
       <button class="button button-icon button-clear ion-android-done-all" 
       ng-click="edtRv.edit(edtRv.e[0].id,qs_id,chs_id, rv_object,qs_object,chs_object)"> </button>
       <button class="button button-icon button-clear ion-trash-b"> </button> 
    </ion-nav-buttons>
  </ion-nav-bar>

    <br> <br> <br>

  <div class="blk"> 
  <input type="text" value="{{edtRv.e[0].rev_name}}" >  
  </div>

  <div class="button-bar">
    <a class="button button-outline button-assertive" ng-click="showCard()"> show all</a>
    <a class="button button-outline button-assertive" ng-click="hideCard()"> hide all</a>
  </div>

  <form ng-repeat="edit in edtRv.e">
    <ion-item ng-if="edit.question_type_id == 1">
     <ion-checkbox ng-if=""> </ion-checkbox> 
  <div class="list list-inset">
    <div>
        Rv id{{edit.id}}
        Qstn id{{edit.id1}}
        Chs id{{edit.id2}}
        <label class="item item-input">
        <b><span class="input-label item-num"> {{$index+1}}.) </span> </b>
          <input type="text" ng-value="edit.question">
        </label>
    </div>

    <div id="startCard" ng-show="showstartCard">
        <label class="item item-input">
        <b><span class="input-label item-num"> Answer: </span> </b>
          <input type="text" ng-value="edit.answer">
        </label>
    </div>
  </div>
  </ion-item>

  <ion-item  ng-if="edit.question_type_id == 2">
     <ion-checkbox ng-if=""> </ion-checkbox>
  <div class="list list-inset">
    <div>
    Rv id{{edit.id}}
        Qstn id{{edit.id1}}
        Chs id{{edit.id2}}
      <label class="item item-input">
      <b><span class="input-label item-num"> {{$index+1}}.) </span> </b>
        <input type="text" ng-value="edit.question">
      </label>
    </div>

    <div id="startCard" ng-show="showstartCard">
      <label class="item item-input">
      <b><span class="input-label item-num"> Answer: </span></b>
        <input type="text" ng-value="edit.answer">
      </label>
      <label class="item item-input">
      <b><span class="input-label item-num"> choice 1: </span></b>
        <input type="text" ng-value="edit.choice_1">
      </label>
      <label class="item item-input">
      <b><span class="input-label item-num"> choice 2: </span></b>
        <input type="text" ng-value="edit.choice_2">
      </label>
      <label class="item item-input">
      <b><span class="input-label item-num"> choice 3: </span></b>
        <input type="text" ng-value="edit.choice_3">
      </label>
    </div>
  </div>
  </ion-item>
</form>


</ion-content>

What Im trying to do is to update the reviewers title the questions and the choices that follow with just one function or one button in the view.I don't want button each item so is there's a way on how to do this?Thank you :D

Here's the Service file

service.update = function (rv_id,qs_id,chs_id, rv_object,qs_object,chs_object) {
        // return $http.put(getUrlForId(id), object);

        // for updating rev object
        return $http.put(getRevUrlForId(rv_id), rv_object)
            .then(function (result) {
                console.log(result.data);
            });
        // for updating qstn object
        return $http.put(getQsUrlForId(qs_id), qs_object)
            .then(function (result) {
                    console.log(result.data);
                });
        // for updating chs object
        return $http.put(getChoicesUrlForId(chs_id), chs_object)
            .then(function (result) {
                console.log(result.data);
            });
    };

Here's the controller

.controller('EditRevCtrl', function($scope, $timeout, $stateParams, $ionicPopup, ionicMaterialInk, ItemsModel, Backand) { var edtRv = this;

edtRv.e = ItemsModel.getcontForEdit(); // use for getting reviewers info

function edit (rv_id,qs_id,chs_id, rv_object,qs_object,chs_object) {
  ItemsModel.update(rv_id,qs_id,chs_id, rv_object,qs_object,chs_object);
}
})

Solution

  • Certainly!

    First you should inject $q into your service as we will use it below. Using Q you can pass an array of promises to be resolved.

    service.update = function (rv_id,qs_id,chs_id, rv_object,qs_object,chs_object) {
        return $q.all([
            $http.put(getUrlForId(id), object),
            $http.put(getRevUrlForId(rv_id), rv_object),
            $http.put(getQsUrlForId(qs_id), qs_object),
            $http.put(getChoicesUrlForId(chs_id), chs_object),
        ]).then(function(results) {
            var UrlForId = results[0];
            var RevUrlForId = results[1];
            var QsUrlForId= results[2];
            var ChoicesUrlForId= results[3];
    
            return results;
        })
    };