// Our template and the marko runtime will automatically be bundled
// by the CommonJS module bundler
var template = require('marko').load(require.resolve('./template.marko'));
/**
* A client-side widget that is bound to an HTML DOM element. One or more
* instances of this Widget will be created when the app is
* bootstrapped up in the browser.
*
* @param {HTMLElement} el The DOM element that the widget is bound to
*/
function Widget(el) {
this.el = el;
}
Widget.prototype = {
/**
* Makes an AJAX call to the server to get new data and then
* renders a template with the data and injects the HTML into
* the DOM by assigning the html to this.el.innerHTML
*/
updateView: function() {
// Make an AJAX call to get JSON data from the server
$.getJSON("/api/my-data", function(data) {
// Now render the template using the data that was returned
// from the server
template.render(data, function(err, html) {
// Add the HTML to the DOM by assigning it to this.el.innerHTML
this.el.innerHTML = html;
});
});
}
};
module.exports = widget;