diff --git a/src/main/coffeescript/view/ResourceView.js b/src/main/coffeescript/view/ResourceView.js new file mode 100644 index 00000000..4d0ec3ca --- /dev/null +++ b/src/main/coffeescript/view/ResourceView.js @@ -0,0 +1,71 @@ +'use strict'; + +var ResourceView = Backbone.View.extend({ + initialize: function(opts) { + opts = opts || {}; + this.auths = opts.auths; + if ('' === this.model.description) { + this.model.description = null; + } + if (this.model.description) { + this.model.summary = this.model.description; + } + }, + + render: function(){ + var methods = {}; + + + $(this.el).html(Handlebars.templates.resource(this.model)); + + // Render each operation + this.model.operationsArray.forEach(function(operation){ + + var counter = 0; + var id = operation.nickname; + + while (typeof methods[id] !== 'undefined') { + id = id + '_' + counter; + counter += 1; + } + + methods[id] = operation; + + operation.nickname = id; + operation.parentId = this.model.id; + this.addOperation(operation); + }); + + $('.toggleEndpointList', this.el).click(this.callDocs.bind(this, 'toggleEndpointListForResource')); + $('.collapseResource', this.el).click(this.callDocs.bind(this, 'collapseOperationsForResource')); + $('.expandResource', this.el).click(this.callDocs.bind(this, 'expandOperationsForResource')); + + return this; + }, + + addOperation: function(operation) { + + operation.number = this.number; + + // Render an operation and add it to operations li + var operationView = new OperationView({ + model: operation, + tagName: 'li', + className: 'endpoint', + swaggerOptions: this.options.swaggerOptions, + auths: this.auths + }); + + $('.endpoints', $(this.el)).append(operationView.render().el); + + this.number++; + + }, + // Generic Event handler (`Docs` is global) + + + callDocs: function(fnName, e) { + e.preventDefault(); + Docs[fnName](e.currentTarget.getAttribute('data-id')); + } +}); \ No newline at end of file