view-manager script. const Promise = require("bluebird"); const $ = require("jquery"); const defaultValue = require("default-value"); Object.assign(this, { currentView: null, viewPrefix: defaultValue(opts.viewPrefix, "uikit-view"), switchView: function(viewName, locals) { if (this.currentView != null) { this.currentView.unmount(); // FIXME: Do we need more cleanup here? } let viewElementName = `${this.viewPrefix}-${viewName}`; let newViewElement = $(`<${viewElementName}>`).appendTo(this.root); this.currentView = riot.mount(newViewElement[0], viewElementName, locals)[0]; this.trigger("switched"); }, navigate: function(method, uri, data = {}) { return Promise.try(() => { this.trigger("navigating"); return opts.router.handle(method, uri, data); }).then((response) => { response.actions.forEach((action) => { if (action.type === "render") { this.switchView(action.viewName, action.locals); } }); this.trigger("navigated"); }) } }); ["get", "post", "put", "delete", "patch", "head"].forEach((method) => { this[method] = this.navigate.bind(this, method); });