$(document).ready(function() {
var galaxyRoot = typeof Galaxy != 'undefined' ? Galaxy.root : '/';
var PHDComicsAppView = Backbone.View.extend({
el: '#phdcomics',
appTemplate: _.template(
'
' +
''
),
imgTemplate: _.template('
'),
events: {
'click #phdcomics-random': 'getRandomComic'
},
initialize: function() {
this.render();
},
render: function() {
this.$el.html(this.appTemplate());
this.$comicImg = this.$('#phdcomics-img');
this.getRandomComic();
return this;
},
getRandomComic: function() {
var me = this,
url = galaxyRoot + 'api/webhooks/phdcomics/data';
this.$comicImg.html($('', {
id: 'phdcomics-loader'
}));
$.getJSON(url, function(data) {
if (data.success) {
me.renderImg(data.src);
} else {
console.error('[ERROR] "' + url + '":\n' + data.error);
}
});
},
renderImg: function(src) {
this.$comicImg.html(this.imgTemplate({src: src}));
}
});
new PHDComicsAppView();
});