Created
May 21, 2014 02:16
-
-
Save eyedol/8d5c7def5d973c07a5e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['App', 'marionette', 'handlebars','underscore', 'alertify', 'models/ApiExplorerModel', | |
'text!templates/api-explorer/ApiExplorerView.html'], | |
function( App, Marionette, Handlebars, _, alertify, ApiExplorerModel, template) | |
{ | |
return Marionette.ItemView.extend( | |
{ | |
template: Handlebars.compile(template), | |
apiUrl : App.config.baseurl + App.config.apiuri +'/', | |
initialize : function() | |
{ | |
this.model = new ApiExplorerModel(); | |
}, | |
events: | |
{ | |
'click .js-api-endpoint' : 'toggleSection', | |
'submit .js-api-explorer-form' : 'submitForm' | |
}, | |
modelEvents : | |
{ | |
'change' : 'render' | |
}, | |
//@TODO::// Rename this function to an appropriate one | |
toggleSection : function(e) | |
{ | |
var $el = this.$(e.currentTarget); | |
var endpoint = $el.data('endpoint'); | |
var requestMethod = $el.data('method'); | |
// Initialize the from with the clicked endpoint | |
this.$('.js-api-url').val(endpoint); | |
// Change to the selected request method in the select element | |
this.$('.js-request-method').val(requestMethod).change(); | |
e.preventDefault(); | |
}, | |
serializeData : function () | |
{ | |
var data = { model : JSON.stringify(this.model), | |
apiUrl : this.apiUrl | |
}; | |
return data; | |
}, | |
executeRequest : function(apiUri, requestMethod, extraData) | |
{ | |
var that = this; | |
// Mostly borrowed from the api-explorer main.js file | |
// Show spinnner | |
that.$('.js-loading').show(); | |
// execute the call | |
App.oauth.ajax({ | |
url : this.apiUrl + apiUri, | |
type: requestMethod, | |
dataType : 'json', | |
data : extraData | |
}).success(function(data, textStatus, jqXHR) { | |
if (data) { | |
that.model.set('data', data); | |
} else { | |
//this.$.JSONView({}, $('.js-response-code')); | |
console.log("response: "+{}); | |
that.model.set('data','<ol><li>Server returned no data. (HTTP Status Code: ' + jqXHR.status + ' – ' + jqXHR.statusText + ')</li></ol>'); | |
} | |
that.$('.js-loading').hide(); | |
that.render(); | |
}).error(function(jqXHR, textStatus, errorThrown) { | |
that.model.set('data',jqXHR.responseText ? JSON.parse(jqXHR.responseText) : {}); | |
that.render(); | |
}); | |
}, | |
submitForm : function(e) | |
{ | |
e.preventDefault(); | |
var url = this.$('.js-api-url').val(); | |
var requestmethod = 'get'; | |
this.executeRequest( url, requestmethod, {}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment