Skip to content

Instantly share code, notes, and snippets.

@dayaki
Created September 20, 2017 10:37
Show Gist options
  • Save dayaki/458bd52cdc7bbc562182b23d091be1dd to your computer and use it in GitHub Desktop.
Save dayaki/458bd52cdc7bbc562182b23d091be1dd to your computer and use it in GitHub Desktop.
My code samples
/// Storage
// Check if data is stored locallyget new
this.storage.ready().then(() => {
this.storage.get('articles').then((value) => {
if (value === null) {
this.article.loadData().then((data) => {
this.storage.set('articles', data)
this.newsData = data;
});
} else {
this.newsData = value;
// Check if new data is available
this.article.loadData().then((newData) => {
let hold; hold = newData;
if (hold.length > value.length) {
this.storage.set('articles', hold);
this.newsData = hold;
}
});
}
});
});
// Observable to check for new data
Observable.interval(500 * 60).subscribe(x => {
let hold = [];
this.article.loadData().then((data) => {
hold.push(data);
if (hold[0].length > this.newsData.length) {
this.storage.set('articles', data);
this.events.publish('new data', data);
}
});
});
// Normal Angular API call
let body = JSON.stringify({ newspaper: this.newspaper });
let headers = new Headers({'Content-Type': 'application/json'});
this.http.post('http://demo.sprypixels.com/thescroll/public/api/newspaper', body, {headers: headers}).subscribe((data) => {
if(data.json().success.length > 0) {
this.loaded = true;
this.newspapers = data.json().success;
} else {
this.loaded = true;
this.nothingFound = true;
}
}, (err) => {
console.log(err.json());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment