2016-03-29 3 views
1

Ich benutze Backand um meine Daten zu speichern. Ich habe ein Objekt, Ereignisse, das auf ein anderes Objekt verweist, Standorte.Wie erhält man einen Wert eines Objekts von seinem Fremdschlüssel?

Wenn ich versuche, den Ort des Ereignisses anzuzeigen, kann ich nur den Wert von locationID abrufen. Ich möchte den tatsächlichen Namen des Ortes, nicht die ID. Wie mache ich das?

<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> 

Winkel Code

.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(); 

}]) 

Antwort

3

Es gibt zwei Möglichkeiten. Sie können entweder den beschreibenden Wert im __metadata jeder wie dieses Objekt verwenden: Anfrage: https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1

Antwort:

{ 
    "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" 
    } 
    ] 
} 

oder Sie können einen tiefen Wunsch tun und den Wert in den relatedObjects Anfrage erhalten: https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1&deep=true

Antwort:

{ 
    "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 
     ] 
     } 
    } 
    } 
} 

Suche nach Madison Square Garden als der Name des Ortes, um die JSON-Struktur zu verstehen. Sie können das beschreibende Feld in den Objekteinstellungen festlegen

+0

Wie bekomme ich die beschreibenden Werte? –

+0

und die zugehörigen Objektwerte? –