Created
January 19, 2016 03:58
-
-
Save mikebranstein/2056195bfea22165dea9 to your computer and use it in GitHub Desktop.
NativeScript offline view model example that persists data loaded into the view model
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 observableArrayModule = require("data/observable-array"); | |
var fsModule = require("file-system"); | |
var offline_view_model = { | |
OfflineViewModel: function(fileName, items) { | |
var viewModel = new observableArrayModule.ObservableArray(items); | |
viewModel._fileName = fileName; | |
viewModel.loadOffline = function() { | |
// clear out current view model | |
viewModel.empty(); | |
// refresh view model from memory | |
var file = fsModule.knownFolders.documents().getFile(viewModel._fileName); | |
return file.readText() | |
.then(function(contents) { | |
JSON.parse(contents).forEach(function(item) { | |
viewModel.push(item); | |
}); | |
}); | |
}; | |
viewModel.loadOnline = function() { | |
// include your api call here, assuming data comes into variable apiData as an array of data | |
// this is simply sample data right now | |
var apiData = [{"id":"1","value":"mike"},{"id":"2","value":"josh"},{"id":"3","value":"jason"}]; | |
// clear the view model | |
viewModel.empty(); | |
// repopulate vm from retrieved data | |
apiData.forEach(function(item) { | |
// could do more advanced data conversion/mapping here, but i'm assuming data from | |
// the api is exactly what I want to persist and advertise | |
viewModel.push(item); | |
}); | |
// persist retrieved data to file system | |
return viewModel.persist(); | |
}; | |
viewModel.delete = function(index) { | |
viewModel.splice(index, 1); | |
return viewModel.persist(); | |
} | |
viewModel.empty = function() { | |
while (viewModel.length) { | |
viewModel.pop(); | |
} | |
}; | |
viewModel.persist = function() { | |
var file = fsModule.knownFolders.documents().getFile(viewModel._fileName); | |
var items = []; | |
viewModel.forEach(function(item) { | |
items.push(item); | |
}) | |
return file.writeText(JSON.stringify(items)); | |
}; | |
return viewModel; | |
} | |
}; | |
module.exports = offline_view_model; | |
// to use, see below | |
var offlineViewModelModule = require("./offline-view-model"); | |
var offlineViewModel = new offlineViewModelModule.OfflineViewModel("my-custom-file-name.json"); | |
offlineViewModel.loadOnline() | |
.then(function() { | |
console.log("finished loading view model data from api call..."); | |
offlineViewModel.forEach(function (item) { | |
console.log(JSON.stringify(item)); | |
}) | |
var message = "finished logging " + offlineViewModel.length + " items from viewModel..."; | |
console.log(message); | |
alert(message); | |
}); | |
offlineViewModel.loadOffline() | |
.then(function() { | |
console.log("finished loading view model data from file-system..."); | |
offlineViewModel.forEach(function (item) { | |
console.log(JSON.stringify(item)); | |
}) | |
var message = "finished logging " + offlineViewModel.length + " items from viewModel..."; | |
console.log(message); | |
alert(message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment