add sliceDeployment; add relatedCollections, add filterBy
authorScott Baker <smbaker@gmail.com>
Wed, 29 Oct 2014 17:05:52 +0000 (10:05 -0700)
committerScott Baker <smbaker@gmail.com>
Wed, 29 Oct 2014 17:05:52 +0000 (10:05 -0700)
planetstack/core/xoslib/static/js/xoslib/xos-backbone.js

index cd1c305..1b95c4c 100644 (file)
@@ -3,6 +3,7 @@ if (! window.XOSLIB_LOADED ) {
 
     SLIVER_API = "/plstackapi/slivers/";
     SLICE_API = "/plstackapi/slices/";
+    SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
     NODE_API = "/plstackapi/nodes/";
     SITE_API = "/plstackapi/sites/";
     USER_API = "/plstackapi/users/";
@@ -14,22 +15,6 @@ if (! window.XOSLIB_LOADED ) {
 
     SLICEPLUS_API = "/xoslib/slicesplus/";
 
-    function getCookie(name) {
-        var cookieValue = null;\r
-        if (document.cookie && document.cookie != '') {\r
-            var cookies = document.cookie.split(';');\r
-            for (var i = 0; i < cookies.length; i++) {\r
-                var cookie = jQuery.trim(cookies[i]);\r
-                // Does this cookie string begin with the name we want?\r
-                if (cookie.substring(0, name.length + 1) == (name + '=')) {\r
-                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r
-                    break;\r
-                }\r
-            }\r
-        }\r
-        return cookieValue;\r
-    }
-
     XOSModel = Backbone.Model.extend({
         /* from backbone-tastypie.js */
         //idAttribute: 'resource_uri',
@@ -79,6 +64,8 @@ if (! window.XOSLIB_LOADED ) {
           this.sortOrder = 'asc';\r
         },\r
 \r
+        relatedCollections: [],\r
+\r
         simpleComparator: function( model ){\r
           parts=this.sortVar.split(".");\r
           result = model.get(parts[0]);\r
@@ -149,6 +136,13 @@ if (! window.XOSLIB_LOADED ) {
             model.fetch(options);
         },
 
+        filterBy: function(fieldName, value) {
+             filtered = this.filter(function(obj) {
+                 return obj.get(fieldName) == value;
+                 });
+             return new this.constructor(filtered);
+        },
+
         /* from backbone-tastypie.js */
         url: function( models ) {
                     var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
@@ -169,7 +163,7 @@ if (! window.XOSLIB_LOADED ) {
                     return url;
             },
 
-            listMethods: function() {
+        listMethods: function() {
                 var res = [];\r
                 for(var m in this) {\r
                     if(typeof this[m] == "function") {\r
@@ -177,6 +171,10 @@ if (! window.XOSLIB_LOADED ) {
                     }\r
                 }\r
                 return res;\r
+            },
+
+        templateHelpers: function() {
+            return { title: "foo" };
             }
     });
 
@@ -189,9 +187,15 @@ if (! window.XOSLIB_LOADED ) {
 
         this.slice = XOSModel.extend({ urlRoot: SLICE_API });
         this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
+                                                       relatedCollections: {"slivers": "slice", "sliceDeployments": "slice"},
                                                        model: this.slice});
         this.slices = new this.sliceCollection();
 
+        this.sliceDeployment = XOSModel.extend({ urlRoot: SLICEDEPLOYMENT_API });
+        this.sliceDeploymentCollection = XOSCollection.extend({ urlRoot: SLICEDEPLOYMENT_API,
+                                                       model: this.slice});
+        this.sliceDeployments = new this.sliceDeploymentCollection();
+
         this.node = XOSModel.extend({ urlRoot: NODE_API });
         this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
                                                        model: this.node});
@@ -233,7 +237,7 @@ if (! window.XOSLIB_LOADED ) {
         this.services = new this.serviceCollection();
 
         // enhanced REST
-        this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
+        this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API, relatedCollections: {'slivers': "slice"} });
         this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
                                                           model: this.slicePlus});
         this.slicesPlus = new this.slicePlusCollection();
@@ -243,14 +247,28 @@ if (! window.XOSLIB_LOADED ) {
 
     xos = new xoslib();
 
+    function getCookie(name) {
+        var cookieValue = null;\r
+        if (document.cookie && document.cookie != '') {\r
+            var cookies = document.cookie.split(';');\r
+            for (var i = 0; i < cookies.length; i++) {\r
+                var cookie = jQuery.trim(cookies[i]);\r
+                // Does this cookie string begin with the name we want?\r
+                if (cookie.substring(0, name.length + 1) == (name + '=')) {\r
+                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r
+                    break;\r
+                }\r
+            }\r
+        }\r
+        return cookieValue;\r
+    }
+
     (function() {
       var _sync = Backbone.sync;\r
       Backbone.sync = function(method, model, options){\r
         options.beforeSend = function(xhr){\r
-          //var token = $('meta[name="csrf-token"]').attr('content');\r
           var token = getCookie("csrftoken");\r
           xhr.setRequestHeader('X-CSRFToken', token);\r
-          console.log(token);\r
         };\r
         return _sync(method, model, options);\r
       };\r