From 41b06517d2f45ab767b02a3f81e80e8e745c01d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Wed, 29 Jun 2016 11:52:19 -0300 Subject: [PATCH] [fix] Tests passing. --- src/ui/public/metadata.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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]; +}