question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom renderer class properties

See original GitHub issue

Hey guys! I’m still new to the framework (moving over from Barba), and I’m running into a bit of an issue with my custom renderer class. I’m using v2.1.2, on the ES6 version of the library, and I’m using Webpack to compile and bundle my scripts.

I need to add a property to the renderer class that holds my main controller for my page scripts, so I can initialize and destroy them properly as pages are loaded and unloaded. However, if I add a constructor to the renderer, I get an Uncaught TypeError: Cannot set property 'mainController' of undefined error.

If I add a super() call before I set the property, I get this error: Uncaught TypeError: Cannot read property 'transition' of undefined at DefaultRenderer.Renderer (highway.js:2575) at new DefaultRenderer (DefaultRenderer.js:6), so obviously that doesn’t work.

Do you know how I can extend the Highway.Renderer class and add class properties?

Here’s my full custom renderer file:

import Highway from '@dogstudio/highway';
import MainController from './MainController';

class DefaultRenderer extends Highway.Renderer {
    constructor() {
        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;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Anthodpntcommented, Oct 29, 2019

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

0reactions
tiansialcommented, Oct 29, 2019

It did, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to Custom Renderers - Xamarin - Microsoft Learn
This article provides an introduction to custom renderers, and outlines the process for creating a custom renderer.
Read more >
Guidelines for using the custom-output element - IBM
Within custom renderer classes, where it is possible to retrieve data such as images and properties, ensure that you retrieve data only from...
Read more >
Registering a Custom Renderer with a Render Kit
When the application developer creates a custom renderer, as described in Delegating Rendering to a Renderer, you must register it using the appropriate...
Read more >
How to implement custom property pages for custom feature ...
This topic discusses how to implement custom property pages for custom feature renderers by creating a Java class for a GUI form, creating...
Read more >
Custom renderers for properties - Optimizely World
The second is the TemplateDescriptor attribute. This tells the system that this editor is preferred when rendering a model property with a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found