Created
June 27, 2018 04:28
-
-
Save alexdiliberto/d23972242b3034764caa116fae2fc1c4 to your computer and use it in GitHub Desktop.
Fastboot `renderingContext` macro: https://twitter.com/ryantotweets/status/1011745553447866368
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
// app/routes/application.js | |
export default Route.extend({ | |
currentUser: service(), | |
beforeModel: renderingContext({ | |
server() {}, | |
client() { | |
return this.get('currentSession.fetch').perform().catch(console.error); | |
} | |
}) | |
}); |
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
// app/utils/rendering-context.js | |
export default function renderingContext(context) { | |
return (typeof Fastboot === "undefined") ? context.client : client.server; | |
} |
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
// app/services/session.js | |
export default Service.extend({ | |
setupCSRF: renderingContext({ | |
server() {}, | |
client(session) { | |
let prefilter = (options, originalOptions, jqXHR) => { | |
let token = session.get('csrfToken'); | |
if (token && !options.crossDomain) { | |
jqXHR.setRequestHeader('X-CSRF-Token', token); | |
} | |
}; | |
$.ajaxPrefilter(prefilter); | |
} | |
}) | |
}); |
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
// app/components/site-header.js | |
export default Component.extend({ | |
currentUser: service(), | |
showAvatar: renderingContext({ | |
server: false, | |
client: readOnly('currentUser.hasAvatar') | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment