試圖解決一個控制器內部在以下情況推遲AngularJS:Q deferreds和requestFileSystem
services.factory('MyService', ['$q',
function($q) {
var Foo = function() {
var deferred = $q.defer();
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, deferred.resolve, this.errorHandler);
return deferred.promise;
}
return {
Foo: Foo
}
}
]);
然後,當我遇到一些問題:
var bar = new MyService.Foo().then(function(cb) {
console.log(cb)
});
如果我使用deferred.resolve外部requestFileSystem函數,然後它完美的工作,但在上面的情況沒有任何反應。 PS:requestFileSystem函數的第三個參數是一個回調函數,它在準備好文件系統對象後就會獲取它。
編輯: 這是條:
Object {then: function, catch: function, finally: function}
的第四個參數requestFileSystem是登錄到控制檯文件系統API初始化錯誤,但在這種情況下,它不叫功能,因爲requestFileSystem成功如此deferred.resolve應叫做。如果我更換一個正常功能延遲:
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(filesystem){
console.log(filesystem);
}, this.errorHandler);
然後文件系統對象返回正確:
DOMFileSystem {root: DirectoryEntry, name: "http_127.0.0.1_3000:Temporary"}
EDIT2:
我試圖用我自己的模擬requestFilesystem功能做一個簡單的測試功能和它的工作原理:
var Foo = function() {
var deferred = $q.defer();
asd(deferred.resolve)
return deferred.promise;
}
function asd(callback) {
callback('it works')
}
MyService.Foo().then(function(cb){
console.log(cb)
})
我猜承諾一個更好的解決方案被退回,但它從來沒有得到解決?添加一些調試語句以查看它掛起的位置。 – Halcyon
第四個參數是什麼?你在控制檯中是否有任何錯誤?不要使用'new','Foo'不是構造函數。 – Bergi
你能指點一下'requestFileSystem'方法嗎? – Chandermani