diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index ff01ec1..52d2c4f 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -423,50 +423,34 @@ GameLib.Component.prototype.saveToRemoteAPI = function() { GameLib.Component.prototype.save = function(remote) { - var event = GameLib.Event.GET_API_URL; + this.buildIdToObject(); - if (remote) { - event = GameLib.Event.GET_REMOTE_API_URL - } + for (var property in this.idToObject) { + if ( + this.idToObject.hasOwnProperty(property) && + this.idToObject[property] instanceof GameLib.Component + ) { - this.publish( - event, - null, - function(data) { - this.buildIdToObject(); + var apiObject = this.idToObject[property].toApiObject(); - for (var property in this.idToObject) { - if ( - this.idToObject.hasOwnProperty(property) && - this.idToObject[property] instanceof GameLib.Component - ) { + apiObject.componentType = this.idToObject[property].componentType; - var apiObject = this.idToObject[property].toApiObject(); + var storageDependencies = this.idToObject[property].getStorageDependencies(); - apiObject.componentType = this.idToObject[property].componentType; - - var storageDependencies = this.idToObject[property].getStorageDependencies(); - - for (var storageProperty in storageDependencies) { - if (storageDependencies.hasOwnProperty(storageProperty)) { - apiObject[storageProperty] = storageDependencies[storageProperty]; - } - } - - this.publish( - GameLib.Event.SAVE_COMPONENT, - { - apiUrl : data.apiUrl, - apiObject : apiObject - } - ); + for (var storageProperty in storageDependencies) { + if (storageDependencies.hasOwnProperty(storageProperty)) { + apiObject[storageProperty] = storageDependencies[storageProperty]; } } - }.bind(this), - function(error) { - console.error(error); - throw new Error('Failed to get API URL: ' + error.message); + + this.publish( + GameLib.Event.SAVE_COMPONENT, + { + apiObject: apiObject, + remote: remote + } + ); } - ); + } }; \ No newline at end of file diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index dbb1596..f1be0bc 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -179,6 +179,7 @@ GameLib.System.Storage.prototype.delete = function(data) { xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("x-authorization", urlData.passwoid); xhr.onreadystatechange = function () { if (this.readyState === 4) { @@ -226,62 +227,74 @@ GameLib.System.Storage.prototype.delete = function(data) { }; - /** - * 'Saves' data to baseURL + * 'Saves' data to somewhere */ GameLib.System.Storage.prototype.save = function(data) { - if (typeof XMLHttpRequest === 'undefined') { - console.log('Implement server side save here'); - return; + var event = GameLib.Event.GET_API_URL; + + if (data.remote) { + event = GameLib.Event.GET_REMOTE_API_URL } - var xhr = new XMLHttpRequest(); - - xhr.open( - 'POST', - data.apiUrl + '/component/create' - ); - - xhr.setRequestHeader("Accept", "application/json"); - xhr.setRequestHeader("Content-Type", "application/json"); - - xhr.onreadystatechange = function () { - if (this.readyState === 4) { - try { - var response = JSON.parse(this.responseText) - } catch (error) { - GameLib.Event.Emit( - GameLib.Event.SAVE_COMPONENT_ERROR, - { - message: this.responseText - } - ) + this.publish( + event, + null, + function(urlData) { + if (typeof XMLHttpRequest === 'undefined') { + console.log('Implement server side save here'); + return; } - if (response.result === 'success') { - GameLib.Event.Emit( - GameLib.Event.COMPONENT_SAVED, - { - message: response.message || 'Successfully saved the component' + var xhr = new XMLHttpRequest(); + + xhr.open( + 'POST', + urlData.apiUrl + '/component/create' + ); + + xhr.setRequestHeader("Accept", "application/json"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("x-authorization", urlData.passwoid); + + xhr.onreadystatechange = function () { + if (this.readyState === 4) { + try { + var response = JSON.parse(this.responseText) + } catch (error) { + GameLib.Event.Emit( + GameLib.Event.SAVE_COMPONENT_ERROR, + { + message: this.responseText + } + ) } - ) - } else { - GameLib.Event.Emit( - GameLib.Event.SAVE_COMPONENT_ERROR, - { - message: response.message || 'The server responded but failed to save the component' + + if (response.result === 'success') { + GameLib.Event.Emit( + GameLib.Event.COMPONENT_SAVED, + { + message: response.message || 'Successfully saved the component' + } + ) + } else { + GameLib.Event.Emit( + GameLib.Event.SAVE_COMPONENT_ERROR, + { + message: response.message || 'The server responded but failed to save the component' + } + ) } - ) - } + } + }; + + xhr.send(JSON.stringify({ + component : data.apiObject, + session : this.token + })); } - }; - - xhr.send(JSON.stringify({ - component : data.apiObject, - session : this.token - })); + ); };