diff --git a/src/ui/public/metadata.js b/src/ui/public/metadata.js index 0b3897bb28e16..96adb868af494 100644 --- a/src/ui/public/metadata.js +++ b/src/ui/public/metadata.js @@ -1,12 +1,7 @@ import _ from 'lodash'; -// singleton for immutable copy of window.__KBN__ +import $ from 'jquery'; -if (!_.has(window, '__KBN__')) { - throw new Error('window.__KBN__ must be set for metadata'); -} - -const kbn = _.cloneDeep(window.__KBN__ || {}); -export default deepFreeze(kbn); +export default deepFreeze(getState()); function deepFreeze(object) { // for any properties that reference an object, makes sure that object is @@ -20,3 +15,12 @@ function deepFreeze(object) { return Object.freeze(object); } + +function getState() { + const stateKey = '__KBN__'; + if (!(stateKey in window)) { + const state = $('kbn-initial-state').attr('data'); + window[stateKey] = JSON.parse(state); + } + return window[stateKey]; +}