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

用户名

我正在使用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();

}])
依赖

有两种选择。您可以在每个对象的__metadata中使用描述性值,如下所示:request: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
        ]
      }
    }
  }
}

搜索Madison Square Garden作为位置名称,以了解JSON结构。您可以在对象设置中设置描述性字段

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章