Comments (8)
You could follow your logic...
const H = new Highway.Core({
renderers: {
'home': HomeRenderer
'default': DefaultRenderer
}
});
... But then you have to define default
as the value of data-router-view
for all views that should use that renderer. Doing so, the renderer will be used but you won't be able to add extra code for a specific page.
<div data-router-wrapper>
<div data-router-view="default">
<!-- [...] -->
</div>
</div>
For this reason, when you need to add extra code to DefaultRenderer
for a specific view, you have to extend it and thus create another renderer that extends DefaultRenderer
and add your extra code.
Let's say your contact page needs all the code from DefaultRenderer
AND some extra code for this specific page. You would create a ContactRenderer
that would extend DefaultRenderer
to inherit all the code from DefaultRenderer
and you could add some extra code for your contact page.
import DefaultRenderer from 'path/to/default.js'
class ContactRenderer extends DefaultRenderer {
onEnter() {
// Inherit the code from the `onEnter` method of `DefaultRenderer` if it exists
super.onEnter();
// Extra Code
console.log('This is some extra code');
}
}
export default ContactRenderer;
Finally, to use this ContactRenderer
you have to attach it to a view name and use that name for the data-router-view
HTML attribute like so:
const H = new Highway.Core({
renderers: {
'home': HomeRenderer,
'contact': ContactRenderer,
'default': DefaultRenderer
}
});
<div data-router-wrapper>
<div data-router-view="contact">
<!-- [...] -->
</div>
</div>
I hope that will help you @tiansial!
Best regards,
Anthodpnt
from highway.
Hey @joeRob2468
Since you are extending Highway.Renderer
which is a class with a constructor you should call super()
in the constructor of your custom renderer like this:
class DefaultRenderer extends Highway.Renderer {
constructor() {
// Extension
super();
// Variables
this.mainController = new MainController();
}
}
from highway.
Hey @Anthodpnt, thanks for taking a look at this! Here's what the constructor looks like:
constructor() {
super();
this.mainController = new MainController();
}
I'd previously tried that, and it causes a Uncaught (in promise) TypeError: Cannot read property 'transition' of undefined at DefaultRenderer.Renderer (highway.js:2575)
error. However, I took a peek at the source code, and it looks like the Highway.Renderer
class expects some properties to be passed into the constructor, so I added those parameters to the inherited class constructor as well, and it works!
For anyone else trying to extend the Highway.Renderer
class to include class properties, just add a properties
parameter to the constructor of your class, and pass those properties into the super()
call. Here's a full working example of a custom renderer with class properties:
import Highway from '@dogstudio/highway';
import MainController from './MainController';
class DefaultRenderer extends Highway.Renderer {
constructor(properties) {
// pass properties into constructor of parent class
super(properties);
// initialize class variables
this.mainController = new MainController();
}
// Hooks/methods
onEnter() {
this.mainController.init();
}
onLeave() {
this.mainController.destroy();
}
onEnterCompleted() {
// [...]
}
onLeaveCompleted() {
// [...]
}
}
// Don`t forget to export your renderer
export default DefaultRenderer;
from highway.
Nice, thanks @joeRob2468!
from highway.
How can we call the customized default renderer on Higway.Core?
Right now I'm calling it this way, but it's not working:
const H = new Highway.Core({
renderers: {
default: defaultRenderer,
home: home,
template_careers: careers,
template_aboutus: about,
},
transitions: {
default: Fade,
},
});
from highway.
Hey @tiansial,
Nope, there is no default
property for the renderers
object in Highway.Core
. You can have pages without any renderer attached to them. If so, Highway will use the default Highway.Renderer
.
But if you like to have a default renderer that has pieces of code shared on multiple pages, you have to follow the example above and create a DefaultRenderer
that will be extended by all other renderers that need the code in it.
Renderers are working this way because all pages don't need a renderer attached to them. It's thus easier to let you manage the inheritance of a DefaultRenderer
and create the renderers you need that will extend this default one.
from highway.
I did follow the example and have a DefaultRenderer too, but the code in it is not being triggered on any page.
Is it enough to export the class in the end of the file?
export default DefaultRenderer;
from highway.
It did, thank you!
from highway.
Related Issues (20)
- Javascript functions doesn't works on the second time HOT 1
- Locomotive scroll not updating after page transition HOT 3
- I can't get the HIGHWAY.redirect() to work HOT 1
- Updating CSS for incoming page – (Highway maintains the outgoing page's CSS) HOT 5
- data-transition attribute no longer works in 2.2.2 HOT 2
- Update /refresh/bind links dynamically created with javascript HOT 1
- New href with highway HOT 13
- Adding additional headers to page requests
- Add index.d.ts file for better IDE typehinting
- back/forward buttons pressed during transition break the website
- Facebook Pixel Tracking HOT 3
- Fatal error on transition to mismatching URL scheme, while remaining on the same domain
- Can't work with CDN
- Improper use of DOM events
- Highway not working with DOM elements rendered by javascript HOT 1
- H.redirect always refresh page HOT 1
- Help
- H is undefined HOT 1
- Navigation with parameters and anchor not working
- Javascript not working after page transition HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from highway.