Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a singleton service in Aurelia?

Tags:

aurelia

I'm pretty new to Aurelia (only been using it a few days) and I love it!

I know how to make a service with Aurelia, but how can I make that service a singleton that I can then share data with between multiple ViewModels?

Thanks

like image 722
Michael Huntington Avatar asked Jul 17 '15 23:07

Michael Huntington


2 Answers

Just inject it

By default, the DI container assumes that everything is a singleton instance; one instance for the app. However, you can use a registration decorator to change this.

like image 138
JamesCarters Avatar answered Oct 18 '22 11:10

JamesCarters


So I realized I was thinking about this too hard. I was trying to depend on the framework (Aurelia) to do all the work, but actually it was a simple ES6 class change that makes it an instance.

let instance = null;

export class SingletonService {

	constructor() {
		if(!instance) {
			instance = this;
		}

		return instance;
	}
}
like image 38
Michael Huntington Avatar answered Oct 18 '22 12:10

Michael Huntington