Correct way to do cordova factory (files)

I was trying to mod this here … from mike360 to cope with ionic

here is my factory

.factory('FileService', function($q, $rootScope, $window, $http, $ionicPlatform) {
return {
    requestFileSystem: function(onSuccess, onError) {
        console.log("FileService Factory");
        $ionicPlatform.ready(function(onSuccess, onError) {
            $window.requestFileSystem(
                LocalFileSystem.PERSISTENT, 0,
                function() {
                    var that = this,
                        args = arguments;
                    console.log("requestFileSystem onSuccess");

                    if (onSuccess) {
                        $rootScope.$apply(function() {
                            onSuccess.apply(that, args);
                        });
                    }

                },
                function() {
                    var that = this,
                        args = arguments;
                    console.log("requestFileSystem onError");
                    if (onError) {
                        $rootScope.$apply(function() {
                            onError.apply(that, args);
                        });
                    }
                }
            );
        })
    }
};

});

and controller

FileService.requestFileSystem(
    function(fs) {
        console.log(fs.root.name);
    },
    function() {
        console.log('fail');
    }
    );

but is fails … to run the console.log in the controller portion?

any better way?

anyone got a nice file factory already?

related post from brian ford here for ref

I dont have it in a factory but I do it like this and it works. Do you get any errors?

function downloadImagesZip () {
		console.log("Trying to download imagesZIP from server");

		var fileURL = localImagesZipUrl;
                var uri = encodeURI(serverImagesZip);

		var fileTransfer = new FileTransfer();   		
		fileTransfer.download(
		    uri,
		    fileURL,
		    function(entry) {
		        console.log("download complete: " + entry.fullPath);
		    },
		    function(error) {
		        console.log("download error source " + error.source);
		        console.log("download error target " + error.target);
		        console.log("download error code" + error.code);
		    },
		    true
		);

	};

My guess is that the $ionicPlatform ready is firing BEFORE your request to the FileService factory. So, your call back never gets called.

1 Like

Cheers,

mmmmm doesnt look like it’s that …

I wrapped it in $ionic.platform now …

$ionicPlatform.ready(function() {
FileService.requestFileSystem(
    function(fs) {
        console.log('filerootname',fs.root.name);
    },
    function() {
        console.log('fail');
    }
    );
    }
);

and still get nothing back …?

Cheers,

yeah I have something like that … but wanted to be a bit more re-useable … using services … seems the way to go.

i have the mistake also ,could you tell me that how you sloved it ?