Dynamically extending object instances with Mixin


#1

I have the situation where I would like to extend an instance AFTER it has been created, based on the result of a promise (ie data from a server). I basically want to implement polymorphic behaviour on my model object based on a type attribute but that is only known long after the object has been instantiated.

My understanding after reading the Ember documentation is that Mixins extend the constructor, and I certainly DO NOT want to extend the constructor, just the object.

What is the recommended approach to doing something like this in Ember given I want _super to work as well as object properties.


#2

You’ll want to use reopen http://emberjs.com/guides/object-model/reopening-classes-and-instances/


#3

Thanks I was using reopen with plain objects which work fine but the docs for Ember.Mixin strongly suggest that mixins can’t be used like that and that mixins obyl extend the constructor.

If the docs are wrong for Ember.Mixin then perhaps we can get confirmation. I’m happy to rectify with a PR if required.


#4

I am not sure why it wouldn’t work. Here is a bad example I threw together to confirm a mixin works with reopen.

http://jsfiddle.net/NQKvy/995/

Adapt it to your scenario so I can help resolve any issues you’re seeing.


#5

Extending instances doesn’t work properly when properties on the mixin have meta data, as is the case when using say Ember.Model’s Ember.attr() method for defining model attributes.

Calls to metaForProperty() raise an exception because the meta data is stored on the mixin and using reopen() on an object instance does not make the meta data available on the object s constructor.

It would be great if instances in Ember could be extended so that properties work correctly, but without some internal changes this does not seem at all possible.