Reputation: 339
I have a PhoneGap mobile web app which uses storage (SQLite) on iOS, Android and BlackBerry. The same code works on all platforms. However, I would also like to create a Chrome app using the same web app. I will need to decouple the storage portion of the code as the storage API of Chrome is quite different.
My thought is to create a single storage API interface which the web app will use to access PhoneGap storage and Chrome storage.
Are their known patterns that can be used or javscript APIs that do this already? Is my design thoughts correct or is there a better approach?
Thanks for your time!
Upvotes: 0
Views: 157
Reputation: 1892
Sriram has talked about a different approach which might invalidate your need for an abstract interface, which is what you should consider first. I'll answer your direct question.
Your design thoughts seem sound and valid. This is very possible and easy in Javascript - easier than other languages - but it always feels unsafe to me.
Javascript is not statically typed. This means you can use an abstract interface but you don't need to declare / define it anywhere. As long as the concrete implementations implement all the right functions, you're fine.
As a concrete example, you could do:
main file:
var gStorage = null
initStorage();
gStorage.load();
....
gStorage.save();
app storage implementation:
var gAppStorage = {};
gAppStorage.load = function() {
...
}
gAppStorage.save = function() {
...
}
function initStorage() {
gStorage = gAppStorage;
}
And similar for any other storage providers you want. You would then only include the js of the provider you want to use in your app and it should just work.
Javascript is very flexible, and there countless permutations of how to do this. This is just one way you could go.
Upvotes: 1
Reputation: 1174
You could use the Chrome Cordova plugin for chrome.storage.local API which would work on your Cordova app in IOS and Android as well as the Chrome app on desktop. You can find more info here: https://github.com/MobileChromeApps/mobile-chrome-apps/blob/master/docs/APIStatus.md https://github.com/MobileChromeApps/mobile-chrome-apps
Upvotes: 1