Commits

berwin9 committed 9b3eac8

additional segment/element views/models/collections

Comments (0)

Files changed (10)

 // Routes
 app.get('/', routes.index);
 app.get('/segments', routes.segments);
+app.get('/elements', routes.elements);
 
 app.listen(process.env.PORT || 5000);
 console.log("Express server listening on port %d in %s mode",

public/js/collections/ElementsCollection.js

+define(function(require) {
+  var $ = require('jquery');
+  var _ = require('underscore');
+  var Backbone = require('backbone');
+
+  var ElementModel = require('models/ElementModel');
+
+  var ElementsCollection = Backbone.Collection.extend({
+    model: ElementModel,
+    url: '/elements'
+  });
+  return ElementsCollection;
+});
+

public/js/models/ElementModel.js

+define(function(require) {
+  var $ = require('jquery');
+  var _ = require('underscore');
+  var Backbone = require('backbone');
+
+  var ElementModel = Backbone.Model.extend({
+    initialize: function(options) {
+
+    }
+  });
+
+  return ElementModel;
+});
+

public/js/models/SegmentModel.js

   var Backbone = require('backbone');
 
   var SegmentModel = Backbone.Model.extend({
-    initialize: function() {
+    initialize: function(options) {
 
     }
   });

public/js/views/ControlPanelView.js

 
     render: function() {
       $(this.el).empty();
-      this.segments.fetch({ success: this.addAll });
+      this.addAll();
       return this;
     },
 

public/js/views/DocumentizrView.js

     initialize: function() {
       _.bindAll(this, 'render');
       this.segments = new SegmentsCollection();
-      this.segments.fetch();
       this.control = new ControlPanelView({
         el: 'div#control-panel',
         collection: this.segments
         el: 'div#report-panel',
         collection: this.segments
       });
-      this.control.render();
+      this.segments.fetch({ success: this.control.render });
       this.report.render();
     },
 

public/js/views/ElementReportView.js

+define(function(require) {
+  var $ = require('jquery');
+  var _ = require('underscore');
+  var Backbone = require('backbone');
+
+  var ElementReportView = Backbone.View.extend({
+    initialize: function(options) {
+      _.bindAll(this, 'render');
+    },
+
+    render: function() {
+      $(this.el).append('<h5>' + this.model.get('segment') +
+                        this.model.get('ref') + '</h5>');
+      return this;
+    }
+
+  });
+  return ElementReportView;
+});
+

public/js/views/ReportPanelView.js

   var _ = require('underscore');
   var Backbone = require('backbone');
 
+  var SegmentReportView = require('views/SegmentReportView');
+
   var ReportPanelView = Backbone.View.extend({
     initialize: function(options) {
       _.bindAll(this, 'render', 'getSegmentModelById',
     },
 
     render: function() {
-
+      $(this.el).empty();
+      return this;
     },
 
     itemDropped: function(event, ui) {
     },
 
     addOne: function(model) {
-      $(this.el).append(model.get('segment'));
+      var view = new SegmentReportView({ model: model });
+      view.render();
+      $(this.el).append(view.el);
     },
 
     addAll: function() {

public/js/views/SegmentReportView.js

+define(function(require) {
+  var $ = require('jquery');
+  var _ = require('underscore');
+  var Backbone = require('backbone');
+
+  var ElementsCollection = require('collections/ElementsCollection');
+  var ElementReportView = require('views/ElementReportView');
+
+  var SegmentReportView = Backbone.View.extend({
+    initialize: function(options) {
+      _.bindAll(this, 'render', 'addOne', 'addAll');
+      this.elements = options.collection || new ElementsCollection();
+    },
+
+    render: function() {
+      this.elements.fetch({
+        success: this.addAll,
+        data: { name: this.model.get('segment') }
+      });
+      return this;
+    },
+
+    addOne: function(model) {
+      console.log(model);
+      var view = new ElementReportView({ model: model });
+      view.render();
+      $(this.el).append(view.el);
+    },
+
+    addAll: function() {
+      this.elements.each(this.addOne);
+    }
+  });
+  return SegmentReportView;
+});
+
   });
 };
 
+exports.elements = function(req, res) {
+  if (req.query.name) {
+    var db = pg.connect(process.env.DATABASE_URL || conString, function(err, client) {
+      var query = client.query('SELECT * FROM "EDISegDef" WHERE segment = $1 ORDER BY ref', [req.query.name]);
+      var results = [];
+
+      query.on('row', function(row) {
+        results.push(row);
+      });
+
+      query.on('end', function() {
+        res.contentType('json');
+        res.send(JSON.stringify(results));
+      });
+    });
+  }
+};
+