Skip to content

Instantly share code, notes, and snippets.

@eyedol
Created May 21, 2014 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyedol/8d5c7def5d973c07a5e1 to your computer and use it in GitHub Desktop.
Save eyedol/8d5c7def5d973c07a5e1 to your computer and use it in GitHub Desktop.
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