Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4827

What is the idiomatic Ember 2 way to create instances of a custom class, with access to services

$
0
0

@kbullaughey wrote:

I have a custom class, Tile, in which I want access to a service:

export default Ember.Object.extend({
  linker: Ember.inject.service()
});

This required registering the class as a factory in an initializer:

application.register('concern:tile', Tile, { singleton: false });

I then want to be able to create several instances of this, initialized with different properties. I was hoping to do the following:

let tileFactory = Ember.getOwner(this).lookupFactory('concern:tile');
tileFactory.create(details1);
tileFactory.create(details2);

But the only way I can figure out how to use lookupFactory is like this:

let factory = Ember.getOwner(this).__container__.lookupFactory('concern:tile');
factory.create(details1);

I am under the impression that we shouldn't use __container__.

Am I doing something gravely wrong in trying to have this concept of a concern which is a container-aware class of which I can make several instances?

I realize I can do the following:

tile = Ember.getOwner(this).lookup('concern:tile');
tile.setProperties(details1);

But this seems inefficient, but more importantly is problematic because init is called when nothing is set and then when I do setProperties the various bindings or observers update, again.

I feel there is something fundamental I'm missing.

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4827

Trending Articles