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
8 years ago
|
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");
|
||
|
},
|
||
|
|
||
6 years ago
|
navigate: function(method, uri, data = {}, options = {}) {
|
||
8 years ago
|
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);
|
||
|
}
|
||
6 years ago
|
|
||
|
// MARKER: Emit notifications!
|
||
8 years ago
|
});
|
||
|
|
||
|
this.trigger("navigated");
|
||
|
})
|
||
|
}
|
||
|
});
|
||
|
|
||
|
["get", "post", "put", "delete", "patch", "head"].forEach((method) => {
|
||
|
this[method] = this.navigate.bind(this, method);
|
||
7 years ago
|
});
|
||
7 years ago
|
|
||
|
// 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();
|
||
|
|
||
6 years ago
|
return this.navigate(form.method, form.action, new FormData(form), {
|
||
|
multipart: (form.enctype === "multipart/form-data")
|
||
|
});
|
||
7 years ago
|
});
|
||
|
});
|