如何从外键获取对象的值?

时间:2016-03-29 00:52:22

标签: html angularjs json backand

我使用Backand来存储我的数据。我有一个对象,事件,引用另一个对象,位置。

  {
    "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();

}])

1 个答案:

答案 0 :(得分:3)

有两种选择。您可以在每个对象的__metadata中使用描述性值,如下所示: 请求:https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1

响应:

{
  "totalRows": 2,
  "data": [
    {
      "__metadata": {
        "id": "1",
        "fields": {
          "id": {
            "type": "int",
            "unique": true
          },
          "name": {
            "type": "string"
          },
          "date": {
            "type": "datetime"
          },
          "time": {
            "type": "datetime"
          },
          "info": {
            "type": "text"
          },
          "locationId": {
            "object": "locations"
          }
        },
        "descriptives": {
          "locationId": {
            "label": "Madison Square Garden",
            "value": "1"
          }
        },
        "dates": {
          "date": "",
          "time": ""
        }
      },
      "id": 1,
      "name": "knicks vs warriors",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    },
    {
      "__metadata": {
        "id": "2",
        "fields": {
          "id": {
            "type": "int",
            "unique": true
          },
          "name": {
            "type": "string"
          },
          "date": {
            "type": "datetime"
          },
          "time": {
            "type": "datetime"
          },
          "info": {
            "type": "text"
          },
          "locationId": {
            "object": "locations"
          }
        },
        "descriptives": {
          "locationId": {
            "label": "Madison Square Garden",
            "value": "1"
          }
        },
        "dates": {
          "date": "",
          "time": ""
        }
      },
      "id": 2,
      "name": "knicks vs cavs",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    }
  ]
}

或者您可以执行深层请求并获取relatedObjects中的值 请求:https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1&deep=true

响应:

{
  "totalRows": 2,
  "data": [
    {
      "__metadata": {
        "id": "1",
        "fields": {
          "id": {
            "type": "int",
            "unique": true
          },
          "name": {
            "type": "string"
          },
          "date": {
            "type": "datetime"
          },
          "time": {
            "type": "datetime"
          },
          "info": {
            "type": "text"
          },
          "locationId": {
            "object": "locations"
          }
        },
        "descriptives": {
          "locationId": {
            "label": "Madison Square Garden",
            "value": "1"
          }
        },
        "dates": {
          "date": "",
          "time": ""
        }
      },
      "id": 1,
      "name": "knicks vs warriors",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    },
    {
      "__metadata": {
        "id": "2",
        "fields": {
          "id": {
            "type": "int",
            "unique": true
          },
          "name": {
            "type": "string"
          },
          "date": {
            "type": "datetime"
          },
          "time": {
            "type": "datetime"
          },
          "info": {
            "type": "text"
          },
          "locationId": {
            "object": "locations"
          }
        },
        "descriptives": {
          "locationId": {
            "label": "Madison Square Garden",
            "value": "1"
          }
        },
        "dates": {
          "date": "",
          "time": ""
        }
      },
      "id": 2,
      "name": "knicks vs cavs",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    }
  ],
  "relatedObjects": {
    "locations": {
      "1": {
        "__metadata": {
          "id": "1",
          "fields": {
            "id": {
              "type": "int",
              "unique": true
            },
            "events": {
              "collection": "events",
              "via": "locationId"
            },
            "name": {
              "type": "text"
            },
            "geo": {
              "type": "point"
            }
          },
          "descriptives": {},
          "dates": {}
        },
        "id": 1,
        "events": null,
        "name": "Madison Square Garden",
        "geo": [
          40.7505,
          73.9934
        ]
      }
    }
  }
}

搜索麦迪逊广场花园作为理解JSON结构的位置的名称。 您可以在对象设置

中设置描述性字段