Created
July 11, 2018 18:03
-
-
Save corbanbrook/5c8384377bdc3bbc733abecf05c1f2b9 to your computer and use it in GitHub Desktop.
This file contains 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
ShowsStore = { | |
shows: new Map(), | |
getById: function(id) { | |
return this.shows.get(id) | |
}, | |
add: function(data) { | |
this.shows.set(data.id, data) | |
} | |
}; | |
autoruns = [] | |
function observable(obj, key, initialValue) { | |
let value = initialValue | |
Object.defineProperty(obj, key, { | |
set: function(newValue) { | |
autoruns.forEach(function(cb) { cb({ obj: obj, key: key, newValue: newValue, oldValue: value }) }) | |
value = newValue; | |
}, | |
get: function() { | |
return value; | |
} | |
}) | |
} | |
function computed(obj, key, cb) { | |
Object.defineProperty(obj, key, { | |
get: function() { | |
return cb.bind(this)() | |
} | |
}) | |
} | |
UIStore = {} | |
observable(UIStore, 'showId', null) | |
computed(UIStore, 'show', function() { return ShowsStore.getById(this.showId) }) | |
autoruns.push(function (change) { | |
if (change.key === 'showId') { | |
fetch(`https://api.tvmaze.com/shows/${change.newValue}?embed=cast`) | |
.then(function (res) { return res.json() }) | |
.then(function (show) { | |
console.log("- Got show", show.id, show) | |
ShowsStore.add(show) | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment