add raw error dialog support, only add model to collection once it has been saved
authorScott Baker <smbaker@gmail.com>
Tue, 2 Dec 2014 01:06:31 +0000 (17:06 -0800)
committerScott Baker <smbaker@gmail.com>
Tue, 2 Dec 2014 01:06:31 +0000 (17:06 -0800)
planetstack/core/xoslib/static/js/xoslib/xos-backbone.js
planetstack/core/xoslib/static/js/xoslib/xos-util.js
planetstack/core/xoslib/static/js/xoslib/xosHelper.js
planetstack/core/xoslib/templates/xosAdmin.html

index 2930014..918e4a2 100644 (file)
@@ -70,7 +70,7 @@ if (! window.XOSLIB_LOADED ) {
                 return res;\r
             },
 
-            validate: function(attrs, options) {
+            xosValidate: function(attrs, options) {
                 errors = {};
                 foundErrors = false;
                 _.each(this.validators, function(validatorList, fieldName) {
index d03ab14..5e91a3d 100644 (file)
@@ -22,7 +22,7 @@ function limitTableRows(tableSelector, maxRows) {
     }
 }
 
-function validateField(validatorName, value) {
+function validateField(validatorName, value, obj) {
     if (validatorName=="notBlank") {
         if ((value==undefined) || (value=="")) {
             return "can not be blank";
@@ -41,5 +41,6 @@ function validateField(validatorName, value) {
             }
             break;
     }
+
     return true;
 }
index 2fa9554..2eb3e42 100644 (file)
@@ -68,9 +68,23 @@ XOSApplication = Marionette.Application.extend({
     },
 
     popupErrorDialog: function(responseText) {
-        $("#xos-error-dialog").html(templateFromId("#xos-error-response")($.parseJSON(responseText)));
+        try {
+            parsed_error=$.parseJSON(responseText);
+            width=300;
+        }
+        catch(err) {
+            parsed_error=undefined;
+            width=640;    // django stacktraces like wide width
+        }
+        if (parsed_error) {
+            $("#xos-error-dialog").html(templateFromId("#xos-error-response")(json));
+        } else {
+            $("#xos-error-dialog").html(templateFromId("#xos-error-rawresponse")({responseText: responseText}))
+        }
+
         $("#xos-error-dialog").dialog({
             modal: true,
+            width: width,
             buttons: {
                 Ok: function() { $(this).dialog("close"); }
             }
@@ -222,7 +236,12 @@ XOSDetailView = Marionette.ItemView.extend({
                 this.app.showError(result);\r
             },\r
 \r
-            saveSuccess: function(model, result, xhr, infoMsgId) {\r
+            saveSuccess: function(model, result, xhr, infoMsgId, isNew) {\r
+                console.log("saveSuccess");\r
+                if (isNew) {\r
+                    this.collection.add(model);\r
+                    this.collection.sort();\r
+                }\r
                 result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText};\r
                 result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName;\r
                 result["infoMsgId"] = infoMsgId;\r
@@ -274,21 +293,25 @@ XOSDetailView = Marionette.ItemView.extend({
                    model.save, we call it ourselves, so we can throw up our\r
                    validation error before creating the infoMsg in the log\r
                 */\r
-                errors =  this.model.validate(data);\r
+                errors =  this.model.xosValidate(data);\r
                 if (errors) {\r
                     this.onFormDataInvalid(errors);\r
                     return;\r
                 }\r
 \r
+                if (isNew) {\r
+                    this.model.attributes.humanReadableName = "new " + model.modelName;\r
+                }\r
+\r
                 var infoMsgId = this.app.showInformational( {what: "save " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );\r
 \r
                 this.model.save(data, {error: function(model, result, xhr) { that.saveError(model,result,xhr,infoMsgId);},\r
-                                       success: function(model, result, xhr) { that.saveSuccess(model,result,xhr,infoMsgId);}});\r
-                if (isNew) {\r
-                    console.log(this.model);\r
+                                       invalid: function(model, result, xhr) { console.log("invalid!"); that.saveError(model,result,xhr,infoMsgId);},\r
+                                       success: function(model, result, xhr) { that.saveSuccess(model,result,xhr,infoMsgId, isNew);}});\r
+                /*if (isNew) {\r
                     this.collection.add(this.model);\r
                     this.collection.sort();\r
-                }\r
+                }*/\r
                 this.dirty = false;\r
             },
 
index daccf88..8e0448d 100644 (file)
       </table>
 </script>
 
+<script type="text/template" id="xos-error-rawresponse">
+  <h5>Error</h5>
+  <pre>The server returned:
+"<%= responseText %>"</pre>
+</script>
+
 <script type="text/template" id="xos-error-template">
   <button id="close-error-box">Close Error Message</button>
   <h3>An error has occurred.</h3>