You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
Plaintext
57 lines
1.6 KiB
Plaintext
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 = {}, options = {}) {
|
|
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);
|
|
}
|
|
|
|
// MARKER: Emit notifications!
|
|
});
|
|
|
|
this.trigger("navigated");
|
|
})
|
|
}
|
|
});
|
|
|
|
["get", "post", "put", "delete", "patch", "head"].forEach((method) => {
|
|
this[method] = this.navigate.bind(this, method);
|
|
});
|
|
|
|
// FIXME: Get rid of jQuery here?
|
|
$(this.root).on("submit", "form:not(.no-intercept)", (event) => {
|
|
Promise.try(() => {
|
|
let form = event.target;
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
return this.navigate(form.method, form.action, new FormData(form), {
|
|
multipart: (form.enctype === "multipart/form-data")
|
|
});
|
|
});
|
|
});
|