1

Problem is that $window.print() is always called and not just when promise is success.How can i create promise when data is populated ? Any suggestion?

'use strict';
angular.module("printModule").controller('printController', ['$scope', '$window', '$q', function ($scope, $window, $q) {


    $scope.ticketPin = localStorage.getItem("pin");
    $scope.payoutTime = localStorage.getItem("payoutTime");
    $scope.payoutAmount = localStorage.getItem("payoutAmount");

    var defer = $q.defer();
    defer.resolve($scope.ticketPin);
    defer.resolve($scope.payoutTime);
    defer.resolve($scope.payoutAmount);

    defer.promise.then(function () {
        $window.print();
    })
}]);
None
  • 8,817
  • 26
  • 96
  • 171
  • The code *immediately resolves* (or 'completes') the promise (and as such it will immediately invoke the following 'then' with a success). That is probably not what is desired... – user2864740 Jul 30 '15 at 07:35
  • @user2864740 how can i fix that then? :) – None Jul 30 '15 at 07:37
  • There is really nothing to complete / no-promise to wait on because `LocalStorage.getItem` is synchronous. – user2864740 Jul 30 '15 at 07:38
  • Have you considered using a getFromLocalStorage function and from inside it returning the data in a callback? – tuckerjt07 Jul 30 '15 at 07:38
  • @tuckerjt07 how u mean ? can u post example of that? – None Jul 30 '15 at 07:39
  • @user2864740 so i cant do anything to change that? – None Jul 30 '15 at 07:40
  • All your actions seems to be synchronous. It means that the actions (the getItems methods) are done before the code is even get into the defer. (For reference, look here: http://stackoverflow.com/questions/20231163/is-html5-localstorage-asynchronous) Have you tried to check if there is a problem while reading the data? Maybe the data is not populated because you had a problem with that. – matan7890 Jul 30 '15 at 07:42
  • @matan7890 i see that but can i something do about it ? to say if promise is success call print function if not do something else? – None Jul 30 '15 at 07:43
  • Be aware that I edited my comment. – matan7890 Jul 30 '15 at 07:49
  • As mentioned here, try to use to use the "in" operator to check if the items are in localStorage before you get them out, or check if they are null after the get: http://www.bennadel.com/blog/2105-exploring-html5-s-localstorage-persistent-client-side-key-value-pairs.htm – matan7890 Jul 30 '15 at 07:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84654/discussion-between-none-and-matan7890). – None Jul 30 '15 at 08:22

1 Answers1

1
function getFromLocalStorage (item, callback)  {
    //To prevent errors
    if (callback) {
        return callback(localStorage.getItem(item)) ;
    }
} 

var callback;
callback = function (data) {
    //Set variable to data
} 

//I would make getFromLocalStorage a factory
myFactory.getFromLocalStorage("itemName", callback);

Caveat, the above should be able to be made to work but I have not used LocalStorage enough to know the ins, outs, and gotchas, so there may need to be some rearrangement of that nested return.

tuckerjt07
  • 902
  • 1
  • 12
  • 31
  • If the above doesn't work, I HATE having to do this, but sometimes a $timeout set on just the high side of average return time works. Two blatant problems with that approach though. It artificially slows your app if it's performance is better than normal and will sometimes not be long enough if slower than normal. – tuckerjt07 Jul 30 '15 at 07:48
  • @tuckerjt07 how can i "inject" this in my project ? :) – None Jul 30 '15 at 08:34
  • var callback and everything below goes into the controller and the above would go into a factory, you could put it all in the controller but that's not a good practice in my opinion. – tuckerjt07 Jul 30 '15 at 08:35