0

I've read many questions and answers, noone has helped me. I've this function:

var model = {};

var mediaReproductionApp = angular.module("mediaReproductionApp",["ui.event",'ngAnimate']);

mediaReproductionApp.run(function ($http) {
    $http.get("movimenti_per_totem.json").success(function (data) {
        model.items = data;
    });
});

mediaReproductionApp.controller("MediaReproductionCtrl", function($scope, $http, $timeout) {
    $scope.item = model;

    $scope.playVideo = function(media) {
        return media ? "../gallery/video/" + media : null;
    }

    $scope.reproductionCodeIsEmpty = function() {
        return Object.keys($scope.item).length == 0;
    }

    $scope.endVideo = function() {
        $timeout(function() {
            $http.get("php/delete_record.php").success(function () {
                $http.get("movimenti_per_totem.json").success(function (data) {
                    $scope.item.items = data;
                });
            });
            if($scope.reproductionCodeIsEmpty()) {
                prelevaDati('../json/52.json', 'spot_creator', 'sc1', modello_SC, {});
                $scope.checkMediaData();
            }
        },1800);
    }

    $scope.checkMediaData = function() {
        $http.get("movimenti_per_totem.json").success(function (data) {
            $scope.item.items = data;
        });
        if($scope.reproductionCodeIsEmpty()) {
            $timeout(function(){$scope.checkMediaData();},2000);
        }
    }

    $scope.checkMediaData();
});

This is my JSON file when it is not empty:

[ {"media":"zafferano_VP8.webm"}, {"media":"amaretti_VP8.webm"}, {"media":"passata_VP8.webm"}]

It never return true when it is empty. I've tried also:

$scope.reproductionCodeIsEmpty = function() {
    return $scope.item.length == 0;
}

$scope.reproductionCodeIsEmpty = function() {
    return $scope.item == {};
}

$scope.reproductionCodeIsEmpty = function() {
    return angular.isUndefined($scope.item) || $scope.item === null;
}

$scope.reproductionCodeIsEmpty = function() {
    return angular.isUndefined($scope.item.items) || $scope.item.items === null;
}

Nothing works... can u tell me why? Thank you!

4
  • We don't know what model is or where you use this code. Provide a minimal reproducible example. Note that return $scope.item == {}; would never work due to different object references Commented Sep 17, 2016 at 16:22
  • I'v edited my question! With more infos Commented Sep 17, 2016 at 16:32
  • Added to my answer. Commented Sep 17, 2016 at 16:35
  • console.log(data) and check what is in there when the json is empty Commented Sep 17, 2016 at 16:56

2 Answers 2

1

After you added to your question:

You define model as: model.items = data;

So, you empty model is: model = { items: [] }.

That's why it isn't empty. You need to test for model.items being empty.


If you need a tested way to tell that the object is empty, I'd recommend lodash.isEmpty(). You can use it for "any Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections".

https://lodash.com/docs/4.15.0#isEmpty

Since I don't know what your model is, this would cover the most possible data types.

_.isEmpty(null);
// => true

_.isEmpty(true);
// => true

_.isEmpty(1);
// => true

_.isEmpty([1, 2, 3]);
// => false

_.isEmpty({ 'a': 1 });
// => false
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to check if object/array is empty, I use:

angular.equals({}, yourObject);
angular.equals([], yourArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.