1

Backand を使用してデータを保存しています。別のオブジェクト Locations を参照するオブジェクト Events があります。

  {
    "name": "events",
    "fields": {
      "eventCommentsId": {
        "collection": "comments",
        "via": "eventId"
      },
      "tags": {
        "collection": "events_tags",
        "via": "event"
      },
      "users": {
        "collection": "users_events",
        "via": "event"
      },
      "name": {
        "type": "string"
      },
      "date": {
        "type": "datetime"
      },
      "time": {
        "type": "datetime"
      },
      "info": {
        "type": "text"
      },
      "locationId": {
        "object": "locations"
      }
    },
  {
    "name": "locations",
    "fields": {
      "events": {
        "collection": "events",
        "via": "locationId"
      },
      "name": {
        "type": "text"
      },
      "geo": {
        "type": "point"
      }
    }
  }

イベントの場所を表示しようとすると、locationID の値しか取得できません。IDではなく、場所の実際の名前が必要です。それ、どうやったら出来るの?

<ion-list>
  <ion-item class="item item-thumbnail-left" ng-repeat="event in events" type="item-text-wrap" href="#/event-detail/{{event.id}}">
      <h2>{{event.name}}</h2>
      <p><i class="ion-location"></i> {{event.locationId.name}}</p>
    <ion-option-button class="button-assertive" ng-click="deleteEvent(event.id)">
      Delete
    </ion-option-button>
  </ion-item>
</ion-list>

角度コード

.service('EventService', function ($http, Backand) {
  var baseUrl = '/1/objects/';
  var objectName = 'events/';

  function getUrl() {
    return Backand.getApiUrl() + baseUrl + objectName;
  }

  function getUrlForId(id) {
    return getUrl() + id;
  }

  getEvents = function () {
    return $http.get(getUrl());
  };

  addEvent = function(event) {
    return $http.post(getUrl(), event);
  }

  deleteEvent = function (id) {
    return $http.delete(getUrlForId(id));
  };

  getEvent = function (id) {
    return $http.get(getUrlForId(id));
  };

  return {
    getEvents: getEvents,
    addEvent: addEvent,
    deleteEvent: deleteEvent,
    getEvent: getEvent
  }
})

.controller('FeedCtrl', ['$scope', '$ionicModal', '$ionicSideMenuDelegate', 'EventService',  function($scope, $ionicModal, $ionicSideMenuDelegate, EventService) {

  $scope.events = [];
  $scope.input = {};

  function getAllEvents() {
    EventService.getEvents()
    .then(function (result) {
      $scope.events = result.data.data;
    });
  }

  $scope.addEvent = function() {
    EventService.addEvent($scope.input)
    .then(function(result) {
      $scope.input = {};
      getAllEvents();
    });
  }

  $scope.deleteEvent = function(id) {
    EventService.deleteEvent(id)
    .then(function (result) {
      getAllEvents();
    });
  }

  getAllEvents();

}])
4

1 に答える 1