Last active
April 12, 2016 12:07
-
-
Save stuf/b966ea0fc1191dff17f32e664eab69b6 to your computer and use it in GitHub Desktop.
Read a file in FuseJS (or create it if it doesn't exist)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Storage = require('FuseJS/Storage'); | |
| var Observable = require('FuseJS/Observable'); | |
| var shots = Observable(); | |
| var readSuccessful; | |
| var readFun = function (result) { | |
| readSuccessful = true; | |
| var parsed = JSON.parse(result); | |
| var count = parsed.length; | |
| shots.replaceAll(parsed.map(function (it) { return new Shot(it) })); | |
| }; | |
| var readCatchFun = function (err) { | |
| readSuccessful = false; | |
| if (/File does not exist/i.test(err)) { | |
| console.log('Storage file did not exist, creating one (with dummy stuff included).'); | |
| var _shots = [ | |
| new Shot({ name: 'dummy', iso: 100, exposure: 0.125, aperture: 2.8 }).toJSON(), | |
| new Shot({ name: 'Another', iso: 200, exposure: 0.005, aperture: 8 }).toJSON(), | |
| new Shot({ name: 'Third', iso: 800, exposure: 30, aperture: 16 }).toJSON() | |
| ]; | |
| Storage.write(settings.stores.shotStore, JSON.stringify(_shots)).then(function (result) { | |
| if (result) console.log('Successfully created new storage file; ' + settings.stores.shotStore); | |
| }).catch(function (_err) { | |
| console.log('Failed to create storage file; ' + _err); | |
| }) | |
| } | |
| }; | |
| var read = function () { | |
| Storage | |
| .read(settings.stores.shotStore) | |
| .then(readFun) | |
| .catch(readCatchFun); | |
| } | |
| read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment