Skip to content

Instantly share code, notes, and snippets.

@taras
Created March 17, 2014 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taras/9594049 to your computer and use it in GitHub Desktop.
Save taras/9594049 to your computer and use it in GitHub Desktop.
var Night = Ember.Object.extend({
// Returns instance of moment for the given timestamp
moment: function() {
return window.moment(this.get("timestamp"));
}.property("timestamp"),
// Return formatted date
formatted: function() {
return this.get("moment").format("YYYY-MM-DD");
}.property("moment"),
// We'll use this as a key for the storage
storageKey: function() {
return "%@|%@".fmt(Night.namespace, this.get("formatted"));
}.property("formatted"),
// Sleep & Dream
sleep: function() {
var sleep = window.localStorage[this.get("storageKey")];
//sleep = this.get("selectedDreamType") + ":::" + this.get("newDream");
return sleep;
}.property("storageKey"),
// Return next day
tomorrow: function() {
var tommorow = this.get("moment").clone().add(1, "day");
if(window.moment().isAfter(tommorow)) {
return Night.create({ timestamp: tommorow.valueOf() });
}
}.property("moment"),
// Return previous day
yesterday: function() {
return Night.create({ timestamp: this.get("moment").clone().subtract(1, "day").valueOf() });
}.property("moment"),
// Write value into localStorage
save: function() {
var props = this.getProperties(["sleep", "selectedDreamType"]);
var string = JSON.stringify(props);
window.localStorage.setItem(this.get("storageKey"), string);
}
});
Night.reopenClass({
namespace: "dreamz", // Namespace in the localStorage
// Query by timestamp
query: function(conditions) {
var timestamp = window.moment(conditions.formatted).startOf("day").valueOf();
return Night.create({ timestamp: timestamp });
},
today: function() {
return Night.query({ formatted: window.moment() });
}
});
export default Night;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment