lots of changes to xosHelper
authorScott Baker <smbaker@gmail.com>
Tue, 4 Nov 2014 07:54:24 +0000 (23:54 -0800)
committerScott Baker <smbaker@gmail.com>
Tue, 4 Nov 2014 07:54:24 +0000 (23:54 -0800)
planetstack/core/xoslib/static/js/xoslib/xosHelper.js

index c799794..0a1be26 100644 (file)
@@ -1,4 +1,5 @@
 XOSApplication = Marionette.Application.extend({
+    detailBoxId: "#detailBox",
     errorBoxId: "#errorBox",
     errorCloseButtonId: "#close-error-box",
     successBoxId: "#successBox",
@@ -28,6 +29,125 @@ XOSApplication = Marionette.Application.extend({
     },
 });
 
+/* XOSDetailView
+      extend with:
+         app - MarionetteApplication
+         template - template (See XOSHelper.html)
+*/
+
+XOSDetailView = Marionette.ItemView.extend({
+            tagName: "div",
+
+            events: {"click button.js-submit": "submitClicked",
+                     "change input": "inputChanged"},
+
+            events: {"click button.js-submit": "submitClicked",
+                     "change input": "inputChanged"},
+
+            /* inputChanged is watching the onChange events of the input controls. We
+               do this to track when this view is 'dirty', so we can throw up a warning\r
+               if the user tries to change his slices without saving first.\r
+            */\r
+\r
+            inputChanged: function(e) {\r
+                this.dirty = true;\r
+            },\r
+\r
+            saveError: function(model, result, xhr) {\r
+                this.app.showError(result);\r
+            },\r
+\r
+            saveSuccess: function(model, result, xhr) {\r
+                this.app.showSuccess({status: xhr.xhr.status, statusText: xhr.xhr.statusText});\r
+            },
+
+            submitClicked: function(e) {
+                this.app.hideError();\r
+                e.preventDefault();\r
+                var data = Backbone.Syphon.serialize(this);\r
+                var thisView = this;\r
+                this.model.save(data, {error: function(model, result, xhr) { thisView.saveError(model, result, xhr); },\r
+                                       success: function(model, result, xhr) { thisView.saveSuccess(model, result, xhr); }});\r
+                this.dirty = false;\r
+            },
+
+            showLinkedItems: function() {
+                    index=0;\r
+                    for (relatedName in this.model.collection.relatedCollections) {\r
+                        relatedField = this.model.collection.relatedCollections[relatedName];\r
+\r
+                        relatedListViewClassName = relatedName + "ListView";\r
+                        if (this.app[relatedListViewClassName] == undefined) {\r
+                            console.log("warning: " + relatedListViewClassName + " not found");\r
+                        }\r
+                        relatedListViewClass = this.app[relatedListViewClassName].extend({collection: xos[relatedName].filterBy(relatedField,this.model.id)});\r
+                        this.app["linkedObjs" + (index+1)].show(new relatedListViewClass());\r
+                        index = index + 1;\r
+                    }\r
+\r
+                    while (index<4) {\r
+                        this.app["linkedObjs" + (index+1)].empty();\r
+                        index = index + 1;\r
+                    }\r
+              },
+});
+
+/* XOSItemView
+      This is for items that will be displayed as table rows.
+      extend with:
+         app - MarionetteApplication
+         template - template (See XOSHelper.html)
+         detailClass - class of detail view, probably an XOSDetailView
+*/
+
+XOSItemView = Marionette.ItemView.extend({
+             tagName: 'tr',
+             className: 'test-tablerow',
+
+             events: {"click": "changeItem"},
+
+             changeItem: function(e) {\r
+                    this.app.hideError();\r
+                    e.preventDefault();\r
+                    e.stopPropagation();\r
+\r
+                    this.app.navigateToModel(this.app, this.detailClass, this.detailNavLink, this.model);\r
+             },\r
+});
+
+/* XOSListView:
+      extend with:
+         app - MarionetteApplication
+         childView - class of ItemView, probably an XOSItemView
+         template - template (see xosHelper.html)
+         collection - collection that holds these objects
+         title - title to display in template
+*/
+
+XOSListView = Marionette.CompositeView.extend({
+             childViewContainer: 'tbody',\r
+\r
+             initialize: function() {\r
+                 this.listenTo(this.collection, 'change', this._renderChildren)
+
+                 // Because many of the templates use idToName(), we need to
+                 // listen to the collections that hold the names for the ids
+                 // that we want to display.
+                 for (i in this.collection.foreignCollections) {
+                     foreignName = this.collection.foreignCollections[i];
+                     if (xos[foreignName] == undefined) {
+                         console.log("Failed to find xos class " + foreignName);
+                     }
+                     this.listenTo(xos[foreignName], 'change', this._renderChildren);
+                     this.listenTo(xos[foreignName], 'sort', this._renderChildren);
+                 }
+             },
+
+             templateHelpers: function() {
+                return { title: this.title };
+             },\r
+});
+
 /* Give an id, the name of a collection, and the name of a field for models
    within that collection, lookup the id and return the value of the field.
 */