I am trying to implement service workers into my application, which I have managed to do in Chrome, Firefox and Safari but not completely in IE. So far I am able to create an object store and add data to it but when I call getAll() I get the following error:
SCRIPT438: Object doesn't support property or method 'getAll'
This is the code I am trying to run:
var docDB = indexedDB.open("docDB", 1);
docDB.onsuccess = function(event) {
var db = docDB.result;
var tx = db.transaction("documents", "readwrite");
var docStore = tx.objectStore("documents");
var docStoreRequest = docStore.getAll();
docStoreRequest.onsuccess = function(event) {
var rowHTML = '';
$.each( docStoreRequest.result, function( index, value ){
var id = $(this)[0].id;
});
};
Neither IE nor Edge support all of the IndexedDB spec. One of the missing things is getAll. Fortunately, there is a polyfill you can use.
Related
I have very big list of geo points which I want to translate to coordinates with the geocoder and the best way I think is to move the task to a web worker, otherwise the Firefox times out and never loads the page.
// the main html file:
var myWorker = new Worker('datapointscollection.js');
myWorker.onmessage = function(e) {
document.getElementById('loadingStatus').innerHTML = count + " elements from " + all + "are ready.";
if (count == all) {
myWorker.terminate();
myWorker = undefined;
}
};
myWorker.postMessage([geocodingParams]);
// the worker js file:
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-core.js");
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-service.js");
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-ui.js");
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-mapevents.js");
self.addEventListener(
'message',
function(e) {
var count = 0;
var all = 0;
// Initialize the platform object:
var platform = new H.service.Platform({
'app_id': 'myappID',
'app_code': 'myappCODE'
});
var geocoder = platform.getGeocodingService();
var onResult = function(result) {};
var findLocations = function(geocodingParams) {
var i=0;
all = geocodingParams.length;
for (i=0; i<geocodingParams.length; i++) {
geocoder.geocode(
geocodingParams[i],
onResult,
function(e){
alert(e);
} );
count = i;
self.postMessage(count, all);
}
};
findLocations(e.data[0]);
},
false);
I tried different approaches, but executing the worker script fails with different errors. The last problem is ReferenceError: document is not defined in mapsjs-core.js:158:623. and after a while another error:
NetworkError: A network error occurred. from datapointscollection.js:1
For huge number of geocodes, you should consider batch geocoding.
Check developer’s guide at developer.here.com
It looks like the files you are importing into your worker depend on a DOM existing. Your web worker doesn't have a DOM, so you will have to use dependencies that don't need a DOM (if it will work in node, it will work without a DOM). Check the documentation for your dependencies to see if there a version that works in node or doesn't need a DOM, and use that version in your web worker. (It might be just the mapsjs-service.js file. see if you can get away with just that).
See also Web Workers API
The first time using the index query takes too long.
usage scenarios: mobile use webview.
After the data is saved into indexedDb, the first time you open the page query is extremely slow.
query code:
var startTime = new Date().getTime();
var request = indexedDB.open("yfg");
request.onerror = function(event) {
alert("Why didn't you allow my web app to use IndexedDB?!");
};
request.onsuccess = function(event) {
var table = [];
var db = request.result;
var objectStore = db.transaction("table").objectStore("table");
//objectStore.openCursor().onsuccess = function(event) {
objectStore.openCursor(null,IDBCursor.NEXT).onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
table.push(cursor.value);
cursor.continue();
}else {
//alert("No more entries!");
console.log(table);
var endTime = new Date().getTime();
console.log("总耗时:",(endTime-startTime)/1000);
}
};
};
Execute on console:
The first result:
The second result:
Browser version:
The specific content of each piece of data:
This table sheet has these data:
Try using IDBObjectStore.prototype.getAll. getAll generates an array, similar to what you are doing, but involves fewer function calls than openCursor and is therefore more performant.
However, unlike openCursor, getAll may not be supported because it is a newer feature of indexedDB. The linked page shows a compatibility table but that may also be out of date.
I am using Event Source Pollyfill : https://github.com/Yaffle/EventSource/blob/master/eventsource.js
I noticed that this pollyfill send a unique code like r=5743466970371525. I also want to send a parameter for every request/retry.
I did this:
user_time = 1407805878;
var sse = new EventSource('server.php?user_time='+user_time);
sse.addEventListener('message',function(e){
var data = e.data;
var obj = JSON.parse(data);
// here I have updated user_time from responce
user_time = obj['time'];
console.log(obj);
},false);
But after changing value of user_time from response, still old time is sent.
I hope I need need to override EventSource url from prototype ?
I tried this to update EventSource url:
user_time = 1407805878;
var sse = new EventSource('server.php?user_time='+user_time);
sse.addEventListener('message',function(e){
var data = e.data;
var obj = JSON.parse(data);
// here I tried to update URLe
sse.url = 'server.php?user_time='+obj['time']
user_time = obj['time'];
console.log(obj);
},false);
So, please help me to send a custom parameter. The pollyfill I am using does this, but I cant figure out how to ?
Thanks
I'm trying to use VersionOne JS SDK in Node.js (https://github.com/versionone/VersionOne.SDK.JavaScript). I'm simply downloading whole library, placing it alongside with my js file:
var v1 = require('./v1sdk/v1sdk.js');
var V1Server = v1.V1Server;
console.log(v1);
console.log(V1Server);
Unfortunately something seems wrong, the output I get after calling
node app.js
is:
{}
undefined
Can somebody point me what I'm doing wrong or check whether the sdk is valid.
Thanks!
You can see in the source where V1Server is defined, that it's a class with a constructor. So you need to use the new keyword and pass the arguments for your environment.
https://github.com/versionone/VersionOne.SDK.JavaScript/blob/master/client.coffee#L37
var server = new V1Server('cloud'); //and more if you need
Can you try the sample.js script that I just updated from here:
https://github.com/versionone/VersionOne.SDK.JavaScript/blob/master/sample.js
It pulls in the two modules like this:
var V1Meta = require('./v1meta').V1Meta;
var V1Server = require('./client').V1Server;
var hostname = "www14.v1host.com";
var instance = "v1sdktesting";
var username = "api";
var password = "api";
var port = "443";
var protocol = "https";
var server = new V1Server(hostname, instance, username, password, port, protocol);
var v1 = new V1Meta(server);
v1.query({
from: "Member",
where: {
IsSelf: 'true'
},
select: ['Email', 'Username', 'ID'],
success: function(result) {
console.log(result.Email);
console.log(result.Username);
console.log(result.ID);
},
error: function(err) { // NOTE: this is not working correctly yet, not called...
console.log(err);
}
});
You might have to get the latest and build the JS from CoffeeScript.
I think I was trying out "browserify" last year and that's how the "v1sdk.js" file got generated. But I'm not sure if that's the best approach if you're using node. It's probably better just to do it the way the sample.js file is doing it.
However, I did also check in a change to v1sdk.coffee which property exports the two other modules, just as a convenience. With that, you can look at sample2.js. The only different part there is this, which is more like you were trying to do with your example:
var v1sdk = require('./v1sdk');
var hostname = "www14.v1host.com";
var instance = "v1sdktesting";
var username = "api";
var password = "api";
var port = "443";
var protocol = "https";
var server = new v1sdk.V1Server(hostname, instance, username, password, port, protocol);
var v1 = new v1sdk.V1Meta(server);
I have got below cocpy clipboard function which works perfectly on latest Internet Explorer as well as Latest Firefox.
function copyToClipboardCrossbrowser(s)
{
//s = document.getElementById(s).value;
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else
{
// You have to sign the code to enable this or allow the action in about:config by changing
//user_pref("signed.applets.codebase_principal_support", true);
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var clip = Components.classes["#mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;
// create a transferable
var trans = Components.classes["#mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return;
// specify the data we wish to handle. Plaintext in this case.
trans.addDataFlavor('text/unicode');
// To get the data from the transferable we need two new objects
var str = new Object();
var len = new Object();
var str = Components.classes["#mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data= s;
trans.setTransferData("text/unicode",str, str.data.length * 2);
var clipid=Components.interfaces.nsIClipboard;
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
Can you please suggest what changes I need to do in above function so that works perfectly on safari as well as opera.
I know there is way if we install shockwave on these browsers using flash to get copied data. I just don't want to go for flash solution.
Thanks.
Best Regards,
Manoj