diff --git a/src/r3-api-customCode.js b/src/r3-api-customCode.js index e0e0d28..9dece80 100644 --- a/src/r3-api-customCode.js +++ b/src/r3-api-customCode.js @@ -14,6 +14,16 @@ R3.API.CustomCode = function( } this.eventId = apiComponent.eventId; + if (R3.Utils.UndefinedOrNull(apiComponent.compiled)) { + apiComponent.compiled = true; + } + this.compiled = apiComponent.compiled; + + if (R3.Utils.UndefinedOrNull(apiComponent.domId)) { + apiComponent.domId = 'r3-code-editor'; + } + this.domId = apiComponent.domId; + if (R3.Utils.UndefinedOrNull(apiComponent.code)) { if (this.eventId === R3.Event.BEFORE_RENDER) { diff --git a/src/r3-customCode.js b/src/r3-customCode.js index 746c7a4..5fa59a5 100644 --- a/src/r3-customCode.js +++ b/src/r3-customCode.js @@ -21,15 +21,22 @@ R3.CustomCode.prototype.createInstance = function() { try { + this.compiled = false; + this.instance = new Function('data', this.code).bind(this); + this.compiled = true; + } catch (error) { /** * Set the instance to true here to indicate that even though the compilation failed, the instance will be fine and * this component loaded fine. */ + this.compiled = false; + this.instance = new Function('data', "console.log('compilation failed for : " + this.name + "');").bind(this); - } + + } __CREATE_INSTANCE__; }; @@ -52,7 +59,9 @@ R3.CustomCode.prototype.updateInstance = function(property) { if (property === 'code') { try { + this.compiled = false; this.instance = new Function('data', this.code).bind(this); + this.compiled = true; this.emit( R3.Event.COMPILE_SUCCESS, { @@ -60,6 +69,7 @@ R3.CustomCode.prototype.updateInstance = function(property) { } ) } catch (error) { + this.compiled = false; this.instance = new Function('data', "console.log('compilation update failed for : " + this.name + "');").bind(this); this.emit( R3.Event.COMPILE_FAILED, @@ -139,6 +149,8 @@ R3.CustomCode.prototype.launchEditor = function(){ this.updateInstance('code'); }.bind(this)) + + this.editor.getWrapperElement().setAttribute('id', this.domId); }; R3.CustomCode.prototype.closeEditor = function(){ diff --git a/src/r3-system-storage.js b/src/r3-system-storage.js index 6b2cd77..11618cd 100644 --- a/src/r3-system-storage.js +++ b/src/r3-system-storage.js @@ -278,11 +278,6 @@ R3.System.Storage.prototype.save = function(data, callback, errorCallback) { return; } - if (this.savedSubscription || this.savedErrorSubscription) { - console.warn('Another save is in progress'); - return; - } - R3.Event.Emit( R3.Event.SAVING, { @@ -404,59 +399,103 @@ R3.System.Storage.prototype.save = function(data, callback, errorCallback) { try { - apiObjects.map( + if (data.bulk) { - function(apiObject) { + var xhr = new XMLHttpRequest(); - var xhr = new XMLHttpRequest(); + xhr.open( + 'POST', + apiUrl + '/component/save' + ); - xhr.open( - 'POST', - apiUrl + '/component/create' - ); + xhr.setRequestHeader("Accept", "application/json"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("x-api-authorization", apiAuthorization); + xhr.setRequestHeader('x-api-user-token', this.apiUserToken); - xhr.setRequestHeader("Accept", "application/json"); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.setRequestHeader("x-api-authorization", apiAuthorization); - xhr.setRequestHeader('x-api-user-token', this.apiUserToken); + xhr.onload = function () { - xhr.onload = function() { + var response = JSON.parse(xhr.responseText); - var response = JSON.parse(xhr.responseText); + if (response.result === 'success') { - if (response.result === 'success') { + R3.Event.Emit( + R3.Event.COMPONENT_SAVED, + { + message: response.message || 'Successfully saved the component(s)', + component: apiObjects + } + ); - R3.Event.Emit( - R3.Event.COMPONENT_SAVED, - { - message: response.message || 'Successfully saved the component', - component : apiObject - } - ); + } else { - } else { + R3.Event.Emit( + R3.Event.SAVE_COMPONENT_ERROR, + { + message: response.message || 'The server responded but failed to save the component(s)', + component: apiObjects + } + ) - R3.Event.Emit( - R3.Event.SAVE_COMPONENT_ERROR, - { - message: response.message || 'The server responded but failed to save the component', - component : apiObject - } - ) + } - } + }; - }; + var data = JSON.stringify({components: apiObjects}); - var data = JSON.stringify(apiObject); + xhr.send(data); - xhr.send( - { - component : data - } - ); - } - ); + } else { + apiObjects.map( + function (apiObject) { + + var xhr = new XMLHttpRequest(); + + xhr.open( + 'POST', + apiUrl + '/component/save' + ); + + xhr.setRequestHeader("Accept", "application/json"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("x-api-authorization", apiAuthorization); + xhr.setRequestHeader('x-api-user-token', this.apiUserToken); + + xhr.onload = function () { + + var response = JSON.parse(xhr.responseText); + + if (response.result === 'success') { + + R3.Event.Emit( + R3.Event.COMPONENT_SAVED, + { + message: response.message || 'Successfully saved the component', + component: apiObject + } + ); + + } else { + + R3.Event.Emit( + R3.Event.SAVE_COMPONENT_ERROR, + { + message: response.message || 'The server responded but failed to save the component', + component: apiObject + } + ) + + } + + }; + + var data = JSON.stringify({component: apiObject}); + + xhr.send(data); + + }.bind(this) + ); + } } catch (error) { @@ -464,10 +503,17 @@ R3.System.Storage.prototype.save = function(data, callback, errorCallback) { R3.Event.SAVE_COMPONENT_ERROR, { message: this.responseText, - component : apiObject + component : apiObjects } ); + this.savedSubscription.remove(); + this.savedErrorSubscription.remove(); + + this.savedSubscription = null; + this.savedErrorSubscription = null; + + } };