hmclarrk/draft

Alerts

Opened this issue · 1 comments

@aaronksaunders

I added a service to save objects to the Parse but am having difficulty accessing the success and error responses for the function. The item gets saved but no alert pops up and nothing is logged to the console.

https://github.com/HClark/draft/blob/master/www/js/app/services.js#L25

@RMScott

need to allow forcing the updating of the list since the views are cached, we set a parameter to indicate if we should update UI or not

        // Each tab has its own nav history stack:
        // optional params - 
        // - https://github.com/angular-ui/ui-router/issues/108#issuecomment-72005173
        .state('tab.list', {
            url: '/list/:forceUpdate',
            params: {
              forceUpdate: {value: null},
              forceUpdate: {value: null, squash: true} 
            },
            views: {
                'tab-list': {
                    templateUrl: 'templates/tab-list.html',
                    controller: 'ListCtrl'
                }
            }
        })

In the List Controller, we listen for the $ionicView.enter event and if forceUpdate is set, the get the data and redraw the list

.controller('ListCtrl', [
    '$state', '$scope', 'UserService','AppService','$timeout', '$stateParams',   
    function ($state, $scope, UserService, AppService, $timeout, $stateParams) {

        function updateUI() {
          AppService.findStuff().then(function(_photos){
              $timeout(function(){
                  $scope.photoList = _photos;
              },0);

          }, function(_error){
              JSON.stringify(alert(_error));
          });
        }

        $scope.$on( "$ionicView.enter", function( scopes, states ) {
            console.log("$ionicView.enter");
            console.log('ListCtrl:$stateParams '+JSON.stringify($stateParams));

            if ($stateParams.forceUpdate) {
              updateUI();
            }
        });
        updateUI()
    }])

When adding the item, we set the forceUpdate on the $stateParams

        $scope.createNew = function() {
            if ($scope.particulars.colour == "" || $scope.particulars.detail == "") {
                alert("Sorry, you didn't input a full entry.")
                $state.go('tab.list', {});
            } else {
                AppService.addOneItem($scope.particulars.colour,$scope.particulars.detail)
                  .then(function(_newObject) {
                    console.log(JSON.stringify(_newObject, null, 2));

                    // force an update since I added an item
                    $state.go('tab.list', {forceUpdate:true});
                }, function(_error) {
                    alert(JSON.stringify(_error));
                    $state.go('tab.list', {});
                });
            }
        };

Finally, in the service, we just return the promise, no need for success & error callbacks

        addOneItem : function(_colors, _detail) {
            var Photos = Parse.Object.extend('photo');
            var addition = new Photos();

            addition.set("colors", _colors);
            addition.set("detail", _detail);

            return addition.save(null, {});
        }