Working with classes


#1

Hi, What I want is to be able to create objects from classes. (I know there are no classes in javascript and about delegation)

Now, I have singletons in services. But if I do not want singletons, how should I proceed? Where I can define object types? and then use them?

In order to simulate that I created some utils as suggested here, https://guides.emberjs.com/v2.5.0/tutorial/service/ but does not feel right.

Thansks.


#2

@ioanszabo what do you mean by “class” and “object”? Those are both pretty abstract terms in this case so if you had an example of what you were trying to do it might be a little easier to suggest something. What do you want these objects to be and to do? Services are supposed to be singletons so I wouldn’t try and use those as a class definition. From what I gather from your question maybe a mixin is the closest thing to what you’re looking for? You can add properties and functions and actions and then extend the mixin anywhere (or multiple mixins) so if you wanted to represent a “class” with a mixin that might get you closer to where you want to be.

One thing to note with mixins is that any properties that you define on the mixin itself are shared across anything that extends the mixin, so if you want to add non-shared properties to a mixin you’d want to do it on init, something like:

...
setupClassAttributes: function(){
  this.set('foo', "bar");
  this.set('foos', []);
}.on('init'),
...

instead of

...
foo: "bar",
foos: [],
...