Ich habe zwei Objekte, Ereignisse & Kommentare:Wie mit AngularJS in einem anderen Objekt-ID eines Objekts hinzufügen
{
"name": "events",
"fields": {
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"users": {
"collection": "users_events",
"via": "event"
},
"eventCommentsId": {
"collection": "comments",
"via": "eventId"
},
}
},
{
"name": "comments",
"fields": {
"content": {
"type": "text"
},
"owner": {
"object": "users"
},
"eventId": {
"object": "events"
},
"date": {
"type": "datetime"
}
}
}
Jede Veranstaltung sollte seine eigene einzigartige Sammlung von Kommentaren haben. Also, es ist eine One to Many Beziehung.
Gerade jetzt, ich kann nur alle Kommentare statt nur diejenigen, die zu jedem Ereignis entsprechen. Mein Gedanke ist, dass ich die ID des Ereignisses in jeden Kommentar aufnehmen muss. Aber ich bin mir nicht ganz sicher, wie ich das machen soll.
Wenn mir jemand damit helfen könnte, wäre das erstaunlich!
Ich baue die App mit Ionic/AngularJS und ich speichere meine Daten mit Backand.
Vielen Dank im Voraus!
.controller('EventDetailCtrl', ['$scope', '$stateParams', '$ionicSideMenuDelegate', 'EventService', 'CommentService',function($scope, $stateParams, $ionicSideMenuDelegate, EventService, CommentService) {
$scope.openMenu = function() {
$ionicSideMenuDelegate.toggleLeft();
};
var id = $stateParams.id;
EventService.getEvent(id).then(function(response){
$scope.event = response.data;
});
$scope.comments = [];
$scope.input = {};
function getAllComments() {
CommentService.getComments()
.then(function (result) {
$scope.comments = result.data.data;
});
}
$scope.addComment = function() {
CommentService.addComment($scope.input)
.then(function(result) {
$scope.input = {};
getAllComments();
});
}
$scope.deleteComment = function(id) {
CommentService.deleteComment(id)
.then(function (result) {
getAllComments();
});
}
getAllComments();
}])
.service('CommentService', function ($http, Backand) {
var baseUrl = '/1/objects/';
var objectName = 'comments/';
function getUrl() {
return Backand.getApiUrl() + baseUrl + objectName;
}
function getUrlForId(id) {
return getUrl() + id;
}
getComments = function() {
return $http.get(getUrl());
};
addComment = function(event) {
return $http.post(getUrl(), event);
}
deleteComment = function (id) {
return $http.delete(getUrlForId(id));
};
getComment = function (id) {
return $http.get(getUrlForId(id));
};
return {
getComments: getComments,
addComment: addComment,
deleteComment: deleteComment,
getComment: getComment
}
})
Sie tun es besser von Back-End-Seite als in JS (angularjs)! – Bettimms