add raw error dialog support, only add model to collection once it has been saved
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
index f615c30..918e4a2 100644 (file)
@@ -68,6 +68,26 @@ if (! window.XOSLIB_LOADED ) {
                     }\r
                 }\r
                 return res;\r
+            },
+
+            xosValidate: function(attrs, options) {
+                errors = {};
+                foundErrors = false;
+                _.each(this.validators, function(validatorList, fieldName) {
+                    _.each(validatorList, function(validator) {
+                        if (fieldName in attrs) {
+                            validatorResult = validateField(validator, attrs[fieldName], this)
+                            if (validatorResult != true) {
+                                errors[fieldName] = validatorResult;
+                                foundErrors = true;
+                            }
+                        }
+                    });
+                });
+                if (foundErrors) {
+                    return errors;
+                }
+                // backbone.js semantics -- on successful validate, return nothing
             }
     });
 
@@ -264,14 +284,18 @@ if (! window.XOSLIB_LOADED ) {
         modelName = attrs.modelName;
         modelClassName = modelName;
         collectionClassName = modelName + "Collection";
-        collectionName = modelName + "s";
+
+        if (!attrs.collectionName) {
+            attrs.collectionName = modelName + "s";
+        }
+        collectionName = attrs.collectionName;
 
         modelAttrs = {}
         collectionAttrs = {}
 
         for (key in attrs) {
             value = attrs[key];
-            if ($.inArray(key, ["urlRoot", "modelName"])>=0) {
+            if ($.inArray(key, ["urlRoot", "modelName", "validate"])>=0) {
                 modelAttrs[key] = value;
             }
             if ($.inArray(key, ["urlRoot", "modelName", "relatedCollections", "foreignCollections"])>=0) {
@@ -283,6 +307,10 @@ if (! window.XOSLIB_LOADED ) {
             modelAttrs["defaults"] = xosdefaults[modelName];
         }
 
+        if (xosvalidators && xosvalidators[modelName]) {
+            modelAttrs["validators"] = xosvalidators[modelName];
+        }
+
         lib[modelName] = XOSModel.extend(modelAttrs);
 
         collectionAttrs["model"] = lib[modelName];
@@ -306,7 +334,21 @@ if (! window.XOSLIB_LOADED ) {
         define_model(this, {urlRoot: SLICE_API,
                            relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"},
                            foreignCollections: ["services", "sites"],
-                           modelName: "slice"});
+                           modelName: "slice",
+                           validate: function(attrs, options) {
+                               errors = XOSModel.prototype.validate(this, attrs, options);
+                               // validate that slice.name starts with site.login_base
+                               site = attrs.site || this.site;
+                               if ((site!=undefined) && (attrs.name!=undefined)) {
+                                   site = xos.sites.get(site);
+                                   if (attrs.name.indexOf(site.attributes.login_base+"_") != 0) {
+                                        errors = errors || {};
+                                        errors["name"] = "must start with " + site.attributes.login_base + "_";
+                                   }
+                               }
+                               return errors;
+                             },
+                           });
 
         define_model(this, {urlRoot: SLICEDEPLOYMENT_API,
                            foreignCollections: ["slices", "deployments"],
@@ -364,7 +406,8 @@ if (! window.XOSLIB_LOADED ) {
         // enhanced REST
         define_model(this, {urlRoot: SLICEPLUS_API,
                             relatedCollections: {'slivers': "slice"},
-                            modelName: "slicePlus"});
+                            modelName: "slicePlus",
+                            collectionName: "slicesPlus"});
 
         this.listObjects = function() { return this.allCollectionNames; };