对于使用我一直在创建的API的数据的自定义AngularJS应用程序,我遇到了使用Angular双簧管的问题.Oboe是一个bower包,可帮助将大型JSON文件流式传输到视图中.经过一些试验和错误后,我设法建立了一个体面的双簧管GET
方法,在大约2秒内获得大约4000个JSON项目.但是在向GET
同一视图添加更多方法时我的问题就出现了.
起初没有任何问题,但最终,加载时间越来越大.所以我尝试过使用Oboe Cached: true
配置.可悲的是,它根本不起作用.每次加载页面时,所有数据都会再次加载而不是从中获取browser Cache
在下面的例子中,你可以看到我一直试图缓存的一个双簧管函数的结构.下面还添加了一个JSfiddle链接.
功能和视图
function createProduct(id, name) {
this.id = id;
this.name = name;
}
$scope.products = [];
oboe({
url: 'config/get/getProducts.php',
method: 'GET',
cached: true
}).path('products.product.*', function () {
// we don't have the person's details yet but we know we
// found someone in the json stream. We can eagerly put
// their div to the page and then fill it with whatever
// other data we find: …
Run Code Online (Sandbox Code Playgroud)我正在使用Oboe.js来解析一个非常大的JSON文件
const promises = [];
oboe('http://domain/my-file.js')
.node('items.*', item => {
// parseItem() returns a rejected Promise because of invalid JSON items
promises.push(parseItem(item));
})
.done(() => {
Promise.all(promises).then(() => {
doSomething();
});
})
Run Code Online (Sandbox Code Playgroud)
但我的浏览器控制台充斥着Uncaught (in promise)
.如果你写一个setTimeout()
类似的承诺,也会发生同样的情况
const promises = [];
setTimeout(() => {
promises.push(Promise.reject());
}, 500);
// some time in the future
Promise.all(promises);
Run Code Online (Sandbox Code Playgroud)
真正奇怪的是:现代浏览器的行为有所不同.在Firefox Developer Edition中,一切都在没有错误消息的情况下运行,而在Chrome中,我充斥着它Uncaught (in promise)
.在Chrome中,如果您Promise.reject();
没有捕获,您可以立即收到消息.在Firefox和Safari中没有任何反应.
那么这个解决方案是什么?忽略这条消息?我的意思是如果这种行为真的在官方承诺规范中,那么异步代码中的承诺对我来说并没有真正意义.