I've got a simple CRUD app running on a Linux server with Apache and MySQL. The database operations are done by PHP scripts. The crud operations are working fine, the server is on a different machine than the developing machine.
Everything is almost fine, but when I try to list the items in the database after an create, update or delete operation, the list sometimes updates and sometimes it doesn't.
Here's the code.
index.html:
<body ng-controller="GetScenesCtrl">
<ul>
<li ng-repeat="scene in scenes">
<em><a href="#/getscene/{{scene.scene_id}}">{{scene.scene_name}}</a></em>
</li>
</ul>
service:
angular.module('writServices', ['ngResource'])
.factory('Scenes', function($resource){
return $resource('/:id/:nimi/:sisalto/:pics', {}, {
query: {
url:'php/getscenes.php',
method:'GET',
isArray:true
}
});
App.service('ScenesService', function ($rootScope, Scenes) {
var scenes = [];
this.setScenes = function() {
scenes = Scenes.query();
$rootScope.$broadcast('updscenes', scenes);
};
this.getScenes = function() {
scenes = Scenes.query();
return scenes;
};
});
controller:
App.controller('GetScenesCtrl', function($scope, Scenes, ScenesService) {
$scope.scenes = Scenes.query();
//$scope.scenes = ScenesService.setScenes();
//$scope.scenes = ScenesService.getScenes();
//$scope.$on('updscenes', function(event, scenes) {
// $scope.scenes = scenes;
//});
$scope.updateScenes = function() {
$scope.scenes = Scenes.query();
};
});
Ok, so when index.html first time loads I use the Scenes.query() method to initialize the list. The updateScenes() -method is called after an CRUD -operation to update the scope. This call is from another Controller. Like this:
App.controller('AddSceneCtrl', function($scope, Scenes, ScenesService) {
$scope.addScene = function(scene) {
Scenes.add({
nimi : scene.name,
sisalto : scene.content
});
$scope.updateScenes();
//$scope.scenes = ScenesService.getScenes();
//ScenesService.setScenes();
}
});
Another way of updating the scope I've tried was the broadcasting way, and both ways work similarly leaving the list sometimes unupdated. Why is this? There must be a better way, the Angular way, of doing this. I'm rather newbie with Angular, so there must be something I've missed here.
Stone
$save()method takes a callback function which you can use to tell when the save has completed.The $save() method takes a callback function which you can use to. So if you could elaborate how I would do that?