Last active
February 27, 2018 13:00
-
-
Save nickwesselman/8217416535bc26f128bd3dcbe5564236 to your computer and use it in GitHub Desktop.
Headless on Sitecore 9 Examples
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
/* Using the Sitecore 9 Item OData service with odata.js */ | |
var o = require('odata'); | |
var moment = require('moment'); | |
o().config({ | |
endpoint: 'http://symheadless.local/sitecore/api/ssc/aggregate/content/', | |
appending: [ | |
{ name: 'sc_apikey', value: 'D510CB0F-B2EF-4224-8F7A-7F04C020BDFE' } | |
] | |
}); | |
const query = o('Items') | |
.filter("TemplateName eq 'Tour Date' and startswith(Path,'/sitecore/content/Ocelots/Tour Dates')") | |
.expand('Fields'); | |
query.get(function(data) { | |
const gigs = data.map((item) => { | |
var date = moment(item.Fields.find(x => x.Name === 'Date').Value); | |
return { | |
id: item.Id, | |
date: date.toDate(), | |
month: date.format('MMM'), | |
day: date.format('D'), | |
time: date.format('h:mma'), | |
dayofweek: date.format('dddd'), | |
venue: item.Fields.find(x => x.Name === 'Venue').Value, | |
location: item.Fields.find(x => x.Name === 'Location').Value | |
} | |
}).sort((a,b) => a.date - b.date); | |
console.log(JSON.stringify(gigs, null, 2)); | |
}); |
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
/* Invoking the Layout Service with JSS. Typically used with <Placeholder /> */ | |
var dataApi = require('@sitecore-jss/sitecore-jss-react').dataApi; | |
var queryString = require('query-string'); | |
let params = {}; | |
//if (window && window.location) { | |
//retain any query string params on the doc (e.g. sc_camp) | |
// params = queryString.parse(window.location.search); | |
//} | |
const fetchOptions = { | |
host: 'http://oscillatingocelots', | |
params | |
} | |
dataApi.fetchRouteData('/', fetchOptions).then(route => { | |
console.log(JSON.stringify(route, null, 2)); | |
}); |
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
/* Invoking a custom SXA JSON Model with fetch */ | |
require('isomorphic-fetch'); | |
var queryString = require('query-string'); | |
fetch(`http://store.oscillatingocelots/Albums?sc_device=json`) | |
.then(response => response.json()) | |
.then(json => { | |
const albums = json.data; | |
console.log(JSON.stringify(albums, null, 2)); | |
}); |
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
/* Invoking the SXA JSON Device with fetch */ | |
require('isomorphic-fetch'); | |
var queryString = require('query-string'); | |
let params = {}; | |
//if (window && window.location) { | |
//retain any query string params on the doc (e.g. sc_camp) | |
// params = queryString.parse(window.location.search); | |
//} | |
params.sc_device = 'json'; | |
params.sc_filter_name = 'promo'; | |
fetch(`http://store.oscillatingocelots/Merch?${queryString.stringify(params)}`) | |
.then(response => response.json()) | |
.then(json => { | |
const merch = json.components.map((item) => item.contents); | |
console.log(JSON.stringify(merch, null, 2)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment