ngResoruce or ionic problem?


#1

Hi there!

I’ve an ionic project where i’m getting and posting data from / to my backend server. I’ve a problem and i’m not sure, if it’s an ionic problem or angular (ngResource).

I’ve created factories where i post / get events with ngResource and i love it. I put these factories into a scope so i can get the events and show in my view easy. The problem starts, after i did 30 get calls. When i’ve 31 events, my view doesn’t show the 31th (and 31+) event. I checked the backend and the event is"created. If i delete 1 event, then the view shows me the 31th event, because after deleting one event i’ve 30 in total. I’m not able to make the view to show 31 or 31+ event.

I hope i could put my problem into words.

Ionic ver: 1.1.1
angular 1.4.3

Some code:
Controller:

.controller('EventCtrl', ['$scope', '$ionicModal', 'Event', '$timeout', '$ionicPopup', '$filter', 'User','$cordovaNetwork', '$cordovaCamera', 'ionicDatePicker', 'PtrService' , function ($scope, $ionicModal, Event, $timeout, $ionicPopup, $filter, User, $cordovaNetwork, $cordovaCamera, ionicDatePicker, PtrService) {

//email of logged user
var useremail = localStorage.getItem('useremail');

 //save event data
$scope.eventData = {};        

$scope.$on('$ionicView.enter', function () {
    PtrService.triggerPtr('ptr-content');
    $scope.doRefresh();
});

//---------------------------- upload event start ------------------

// Our form data for creating a new event with ng-model
$scope.newEvent = function (form) {
    //      console.log("inside new Event");
    
    //spinner start
    $scope.showing = true;
    
    if (form.$valid) {

        if ($scope.eventData.kardes == "" || $scope.eventData.kategorie == "" || $scope.eventData.photo == undefined || $scope.eventData.photo == null || $scope.eventData.date == undefined || $scope.eventData.date == null || $scope.eventData.date == "") {

                 ...

                //console.log("event erzeugt");
                var event = new Event($scope.eventData);

                event.$save(function (result) {
                    //onsucces
                                            
                    //email of logged user
                    var useremail = localStorage.getItem('useremail');
                    
                    User.get({
                        'email': useremail
                    }, function(result) {
                        // on success
                        
                        //get points in total
                        $scope.pointsSum = result.points;

                        localStorage.setItem('lastEvent', result.events[result.events.length-1].createdAt);
                                                    
                        if(result != undefined) {
                            if (result.id == undefined) {
                                var alertPopup = $ionicPopup.alert({
                                    title: 'Hata oluştu!',
                                    template: 'Abi hesabınız bulunamadı. Tekrar deneyiniz.',
                                    okText: 'Tamam'
                                });
                                alertPopup.then(function (res) {
                                    //spinner stop
                                    $scope.showing = false;
                                });
                            } else {

                                var userID = result.id;

                                $scope.events = Event.query({
                                    'owner': userID
                                })                                    
                                
                                //spinner stop
                                $scope.showing = false;
        
                            }
                        } else {
                            var alertPopup = $ionicPopup.alert({
                                title: 'Hata oluştu!',
                                template: 'Abi hesabınız yok veya silinmiş. Tekrar deneyiniz.',
                                okText: 'Tamam'
                            });
                            alertPopup.then(function (res) {
                                    //spinner stop
                                    $scope.showing = false;
                                });

                        }

                    }, function(response) {
                        // on fail e.g. server down
                        if(response.data == null) {
                            var alertPopup = $ionicPopup.alert({
                                title: 'Hata oluştu!',
                                template: 'Servere ulaşılamıyor. Sonra tekrar deneyiniz.'
                            });
                            alertPopup.then(function (res) {
                                //spinner stop
                                $scope.showing = false;
                            });
                        }
                    });

                }, function(response) {
                    // on fail e.g. server down
                    if(response.data == null) {
                        var alertPopup = $ionicPopup.alert({
                            title: 'Hata oluştu!',
                            template: 'Servere ulaşılamıyor. Sonra tekrar deneyiniz.',
                            okText: 'Tamam'
                        });
                        alertPopup.then(function (res) {
                            //spinner stop
                            $scope.showing = false;
                        });
                    }
                });
            }
        }

    }

};

//---------------------------- upload event end ----------------------

//delete event
$scope.deleteEvent = function (event) {
    //        console.log("inside delete");

    // Delete an event. Issues a DELETE
    var confirmPopup = $ionicPopup.confirm({
        title: 'Faaliyet siliniyor!',
        template: 'Faaliyeti silmek istediğinizden emin misiniz? Geri dönüşü yok!',
                okText: 'Sil',
                okType: 'button-assertive'
    });

    confirmPopup.then(function (res) {
        if (res) {
            //console.log('You are sure');
            if($cordovaNetwork.isOffline()) {
                var alertPopup = $ionicPopup.alert({
                    title: 'Hata oluştu!',
                    template: 'İnternet bağlantınızı kontrol ediniz. Tekrar deneyiniz.',
                    okText: 'Tamam'
                });
                alertPopup.then(function (res) {});
            } else {
            
                event.$delete(function () {
                    
                    //email of logged user
                    var useremail = localStorage.getItem('useremail');
                    
                    User.get({
                        'email': useremail
                    }, function(result) {
                        // on success
                        
                        //get points in total
                        $scope.pointsSum = result.points;
                        
                        if(result != undefined) {

                            if (result.id == undefined) {
                                var alertPopup = $ionicPopup.alert({
                                    title: 'Hata oluştu!',
                                    template: 'Abi hesabınız silinmiş olabilir veya bulunamadı. Tekrar deneyiniz.',
                                    okText: 'Tamam'
                                });
                                alertPopup.then(function (res) {});
                            } else {
                                var userID = result.id;

                                $scope.events = Event.query({
                                    'owner': userID
                                })
                            }
                        } else {
                            var alertPopup = $ionicPopup.alert({
                                title: 'Hata oluştu!',
                                template: 'Abi hesabınız bulunamadı. Sonra tekrar deneyiniz.',
                                okText: 'Tamam'
                            });
                            alertPopup.then(function (res) {});
                        }

                    }, function(response) {
                        // on fail e.g. server down
                        if(response.data == null) {
                            var alertPopup = $ionicPopup.alert({
                                title: 'Hata oluştu!',
                                template: 'Servere ulaşılamıyor. Sonra tekrar deneyiniz.',
                                okText: 'Tamam'
                            });
                            alertPopup.then(function (res) {});
                        }
                    });

                }, function(response) {
                    // on fail e.g. server down
                    if(response.data == null) {
                        var alertPopup = $ionicPopup.alert({
                            title: 'Hata oluştu!',
                            template: 'Servere ulaşılamıyor. Sonra tekrar deneyiniz.',
                            okText: 'Tamam'
                        });
                        alertPopup.then(function (res) {});
                    }
                });
            }
            
        } else {
            //console.log('You are not sure');
        }
    });

};

Factory:

.factory('Event', function($resource, $http, $injector) {
var token;
if (localStorage.getItem('auth_token')) {
    token = localStorage.getItem('auth_token');
    $http.defaults.headers.common['Authorization'] = token;
} else {
    localStorage.removeItem('auth_token');
    localStorage.removeItem('usermail');
    $injector.get('$state').go('login');
}
return $resource('http://xxxxxx:8080/event/:id', { id: '@id' }, {
    update: {
        method: 'PUT'
    }
});

Event.html

<ion-view view-title="Faliyetler <i class='badge badge-balanced' style='float: right; margin-top: 3.5%'>Toplam puan {{pointsSum.toFixed(2)}}</i>">

<ion-content delegate-handle="ptr-content">

    <ion-refresher on-refresh="doRefresh()" pulling-icon="ion-chevron-down" spinner="android">
    </ion-refresher>

    <button class="button button-block button-balanced" ng-click="showModal('templates/event-upload.html')">
        <div class="center-vertical-horizontal">                    
            <ion-spinner ng-show="showing" class="button-spinner light"></ion-spinner> 
            Faaliyet yükle
        </div>
    </button>

    <div class="card" ng-repeat="event in events | reverse">
        <div class="item item-divider itemDivCol">
            {{ events.length - $index }} | {{ event.createdAt | date:'dd.MM.yyyy HH:mm' }}
            <i class="button button-small button-assertive buttonSize" style="float: right" ng-click="deleteEvent(event)">X</i>
        </div>
        <div class="item">
            Kategori:
            <div class="item-text-wrap" style="float: right">{{ event.kategorie }}</div>
            <p/>
        </div>
        <div class="item">
            Kardeşler:
            <div class="item-text-wrap" style="float: right">{{ event.kardes }}</div>
            <p/>
        </div>
        <div class="item">
            Faaliyet hakkında:
            <div class="item-text-wrap" style="float: right">{{ event.info }}</div>
            <p/>
        </div>
        <div class="item">
            Süre:
            <div class="item-text-wrap" style="float: right">{{ event.dauer }}</div>
            <p/>
        </div>
        <div ng-if="event.photo" class="item noPadding" ng-click="showImages($index)">
            <div class="col">
                <div style="text-align: center">
                    <img class="eventPhoto" data-ng-src={{event.photo}}>
                </div>
            </div>
        </div>
    </div>

</ion-content>

Any help appreciated.

P.s. The showModal button in my view opens a modal where i do give event infos and then create it per ng-click. Shouldn’t be relevant.