Embedding resource file in Cordova app
My latest Cordova application neeeded to read app files such as XML or CSV. During research due to embedding them as resource files I have found that there isn’t needed any platform-specified behaviour.
Embed file
My app is being made in AngularJS so the file structure like this:
app
- images
- scripts
- styles
- views
index.html
What you need to embed files is just add them into this structure. I like to have folders so there appeared one:
app/data
where all my resource files come in.
Read it
$.get 'data/Recipes.csv', (data) ->
alert data
This is it.
You don’t need any Cordova File API or alikes.
Build it (grunt)
Because I used yeoman, grunt and bower I needed a way to automatically move app/data into dist/data. That’s simple too, just modify copy task:
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'.htaccess',
'*.html',
'views/{,*/}*.html',
'images/{,*/}*.{webp}',
'fonts/*',
'data/*'
]
//...
}
}