-
-
Notifications
You must be signed in to change notification settings - Fork 3
Component Shifter
Fatih Koca edited this page Nov 27, 2019
·
3 revisions
With componentShifter() you can load (with Vue.ajax
) and render your Vue template
(html) in your application by dynamic & async Vue.component()
. You can also add components and run them nested.
this.componentShifter(object configurations[, function success] [,function error])
- You can organize the
async and dynamic components
by typing less. Check out the events for listeners. - You can easily prepare common
callbacks
andlisteners
for dynamic components. - With the
keepAlive
option caches the active components. Thus, when inactive components are called, they are loaded quickly without consuming data. - With the
library
option you can create dynamic options for dynamic component instances (data
,props
,computed
, ..., etc). - And supports
Vue.ajax
's all features (history
,data
,title
, ..., etc).
this.componentShifter({
is: {componentHolder: componentName},
url: url,
}, function() {
console.log("Component changed!");
});
Property | Required | Type | Description |
---|---|---|---|
is |
Yes | Object | Component holder (key) and unique component name (value). |
url |
Yes | String | Component resources url. |
keepAlive |
No | Boolean or object | Caches the inactive components. Default: false
|
library |
No | Object | Options of the new component instance (data , props , ..., etc) |
Property | Required | Type | Description |
---|---|---|---|
include |
No | Array, string (comma-delimited), regex | Only components with matching names will be cached. |
exclude |
No | Array, string (comma-delimited), regex | Any component with a matching name will not be cached. |
max |
No | Number | The maximum number of component instances to cache. |
Property | Required | Type | Description |
---|---|---|---|
success |
No | Function | Your custom callback on success. (Second argument) |
error |
No | Function | Your custom callback on error. (Third argument) |
HTML
<div id="app">
<a href="/page-1" @click.prevent="openPage('page1', '/page-1', 'Page 1')">Page 1</a>
<a href="/page-2" @click.prevent="openPage('page2', '/page-2', 'Page 2')">Page 2</a>
<a href="/page-3" @click.prevent="openPage('page3', '/page-3', 'Page 3')">Page 3</a>
<!-- Your container component -->
<component :is="pageComponent"></component>
</div>
Vue.js
new Vue({
el: "#app",
data() {
return {
pageComponent: null, // Component holder
pageLoaded: false
}
},
methods: {
openPage(componentName, url, title) {
// Calling componentShifter
this.componentShifter({
is: {pageComponent: componentName},
url: url,
keepAlive: {
max: 10,
// Another usages: "page1,page2" and /page1|page2/
include: ["page1", "page2"],
// Another usages: "page3,page4" and /page3|page4/
exclude: ["page3", "page4"]
},
library: {
data() {
return {
hasFooter: true
}
},
props: ["custom"]
}
}, function() {
console.log("Component changed!");
this.pageLoaded = true;
}, function(response) {
console.log("Component could not be changed!", response);
this.pageLoaded = false;
});
}
},
mounted() {
if(!pageLoaded) {
this.openPage("page1", "/page-1", "Page 1")
}
}
});
Register a handler to be called when componentShifter()
requests complete.
window.addEventListener("componentshiftercomplete", function(e) {
console.log(e);
});
Register a handler to be called when componentShifter()
requests complete with an error.
window.addEventListener("componentshiftererror", function(e) {
console.log(e);
});
Register a handler to be called when componentShifter()
requests begins.
window.addEventListener("componentshifterstart", function(e) {
console.log(e);
});
Attach a function to be executed whenever an componentShifter()
request completes successfully.
window.addEventListener("componentshiftersuccess", function(e) {
console.log(e);
});