2016-04-26 86 views
0

我需要像這樣調用IndexedDB庫之後返回方法對象。AngularJS IndexedDB調用後的方法返回對象

.factory('presentationData', ['$indexedDB', 'uuid4','$q', function ($indexedDB, uuid4, $q) { 

    var objectStore = $indexedDB.openStore('presentations', function(store) { 
    return store; 
    }); 

    return objectStore.then(function(objectStore) { 
    var functions = getDefaultDataServiceFunctions(objectStore); 

    functions.getAll = function (includeFlaggedForDelete) { 
     if (includeFlaggedForDelete) { 
     return objectStore.getAll(); 
     } 
     return objectStore.getAll().then(function (presentations) { 
     return presentations.filter(function (p) { 
      return !(p.meta && p.meta.localStatus === 'deleted'); 
     }); 
     }); 
    }; 

    functions.getWithLocalStatus = function (status) { 
     var query = $indexedDB.queryBuilder().$index('localStatus').$eq(status).compile(); 
     return objectStore.each(query); 
    }; 

    return functions; 
    }) 
}]) 

爲什麼這不返回方法的對象?我不明白!

使用這個:https://github.com/bramski/angular-indexedDB

謝謝!

+0

如果你可以在jsfiddle中重現它,它將有助於解決你的問題。 – 6220119

回答

1

這不會起作用,因爲返回商店對象是無效的。該店將被關閉,其上的操作將失敗。

我真的不清楚你實際上想做什麼。只需獲取數據?您需要返回將返回數據的承諾。

.service('dataFetcher', ['$indexedDB', 'uuid4','$q', function ($indexedDB, uuid4, $q) { 
    this.getData = function() { 
    promise = $q.defer(); 
    $indexedDB.openStore('presentations', function (store) { 
     ... do what is needed to get the answer here... 
     promise.resolve(data); 
    } 
    return promise.promise; 
    } 
} 

使用該服務,然後去像...

.controller('MyControl', ['dataFetcher', function ($scope, dataFetcher) { 
    dataFetcher.getData().then(function (data) { 
    $scope.data = data; 
    }); 
}]; 

這會給你你實際上所追求的。索引DB在回調機制上工作。您需要使用它以合理的方式設計您的數據服務。