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.
42 lines
1.2 KiB
Plaintext
42 lines
1.2 KiB
Plaintext
9 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");
|
||
|
},
|
||
|
|
||
|
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);
|
||
8 years ago
|
});
|