added some spine: view controllers

This commit is contained in:
Ayush Gupta
2011-07-27 15:28:19 -07:00
parent 6486cf7ba3
commit 668a8a372e

View File

@@ -1,84 +1,149 @@
jQuery(function($) { jQuery(function($) {
// create and initialize SwaggerService
var hostUrl = "http://swagr.api.wordnik.com/v4";
$("#api_host_url").html(hostUrl); // The following heirarchy is followed by these view controllers
var swaggerService = new SwaggerService(hostUrl); // ResourceListController
swaggerService.init(); // >>> ResourceController
// >>> ApiController
// >>> OperationController
// Create convenience references to Spine models var ResourceListController = Spine.Controller.create({
var ApiResource = swaggerService.ApiResource(); proxied: ["addAll", "addOne"],
// Register a callback for when apis are loaded ApiResource: null,
ApiResource.bind("refresh", apisLoaded);
function apisLoaded() { init: function() {
for (var i = 0; i < ApiResource.all().length; i++) {
var apiResource = ApiResource.all()[i];
log("------------- apiResource : " + apiResource.name);
$("#resourceTemplate").tmpl(apiResource).appendTo("#resources"); // create and initialize SwaggerService
var hostUrl = "http://swagr.api.wordnik.com/v4";
// apiResource.apiList.logAll(); $("#api_host_url").html(hostUrl);
var resourceApisContainer = "#" + apiResource.name + "_endpoint_list"; var swaggerService = new SwaggerService(hostUrl);
log("resourceApisContainer = " + resourceApisContainer); swaggerService.init();
for (var j = 0; j < apiResource.apiList.all().length; j++) {
var api = apiResource.apiList.all()[j];
$("#apiTemplate").tmpl(api).appendTo(resourceApisContainer);
renderOperations(api);
}
// apiResource.modelList.logAll(); // Create convenience references to Spine models
for (var k = 0; k < apiResource.modelList.all().length; k++) { this.ApiResource = swaggerService.ApiResource();
var apiModel = apiResource.modelList.all()[k];
} this.ApiResource.bind("refresh", this.addAll);
},
addAll: function() {
this.ApiResource.each(this.addOne);
},
addOne: function(apiResource) {
ResourceController.init({item: apiResource, container: "#resources"});
}
});
var ResourceController = Spine.Controller.create({
proxied: ["renderApi", "renderOperation"],
templateName: "#resourceTemplate",
apiResource: null,
apiList: null,
modelList: null,
init: function() {
this.render();
this.apiResource = this.item;
this.apiList = this.apiResource.apiList;
this.modelList = this.apiResource.modelList;
// log("------------- apiResource : " + this.apiResource.name);
// this.apiList.logAll();
// this.modelList.logAll();
this.apiList.each(this.renderApi);
},
render: function() {
$(this.templateName).tmpl(this.item).appendTo(this.container);
},
renderApi: function(api) {
var resourceApisContainer = "#" + this.apiResource.name + "_endpoint_list";
ApiController.init({item: api, container: resourceApisContainer});
} }
if (window.console) console.log("apis loaded"); });
}
function renderOperations(api) {
if (api.operations && api.operations.count() > 0) { var ApiController = Spine.Controller.create({
var operationsContainer = "#" + api.name + "_endpoint_" + api.id + "_operations"; proxied: ["renderOperation"],
for (var o = 0; o < api.operations.all().length; o++) {
var operation = api.operations.all()[o]; api: null,
$("#operationTemplate").tmpl(operation).appendTo(operationsContainer); templateName: "#apiTemplate",
renderParameters(operation, $);
} init: function() {
this.render();
this.api = this.item;
this.api.operations.each(this.renderOperation);
},
render: function() {
$(this.templateName).tmpl(this.item).appendTo(this.container);
},
renderOperation: function(operation) {
var operationsContainer = "#" + this.api.name + "_endpoint_" + this.api.id + "_operations";
OperationController.init({item: operation, container: operationsContainer});
} }
} });
function renderParameters(operation) { var OperationController = Spine.Controller.create({
if(operation.parameters && operation.parameters.count() > 0) { operation: null,
var isGetOpetation = (operation.httpMethodLowercase == "get"); templateName: "#operationTemplate",
var operationParamsContainer = "#" + operation.apiName + "_" + operation.nickname + "_" + operation.id + "_params"; init: function() {
log("operationParamsContainer= " + operationParamsContainer); this.render();
for (var p = 0; p < operation.parameters.all().length; p++) {
var param = operation.parameters.all()[p];
var templateName = "#paramTemplate"; this.operation = this.item;
if (param.required) this.renderParams();
templateName += "Required"; },
if (!isGetOpetation) render: function() {
templateName += "ReadOnly"; $(this.templateName).tmpl(this.item).appendTo(this.container);
},
$(templateName).tmpl(param).appendTo(operationParamsContainer); renderParams: function() {
if (this.operation.parameters && this.operation.parameters.count() > 0) {
var isGetOpetation = (this.operation.httpMethodLowercase == "get");
if (!isGetOpetation) { var operationParamsContainer = "#" + this.operation.apiName + "_" + this.operation.nickname + "_" + this.operation.id + "_params";
var submitButtonId = "#" + operation.apiName + "_" + operation.nickname + "_" + operation.id + "_content_sandbox_response_button"; log("operationParamsContainer = " + operationParamsContainer);
$(submitButtonId).hide(); for (var p = 0; p < this.operation.parameters.count(); p++) {
var param = this.operation.parameters.all()[p];
var templateName = "#paramTemplate";
if (param.required)
templateName += "Required";
if (!isGetOpetation)
templateName += "ReadOnly";
$(templateName).tmpl(param).appendTo(operationParamsContainer);
log("adding " + $(templateName).tmpl(param) + " TO " + operationParamsContainer);
if (!isGetOpetation) {
var submitButtonId = "#" + this.operation.apiName + "_" + this.operation.nickname + "_" + this.operation.id + "_content_sandbox_response_button";
$(submitButtonId).hide();
var valueHeader = "#" + this.operation.apiName + "_" + this.operation.nickname + "_" + this.operation.id + "_value_header";
$(valueHeader).html("Default Value");
}
var valueHeader = "#" + operation.apiName + "_" + operation.nickname + "_" + operation.id + "_value_header";
$(valueHeader).html("Default Value");
} }
} }
} }
} });
var resourceListController = ResourceListController.init();
}); });