Given this documentation:
create(_arguments_)
Creates an instance of a class. Accepts either no arguments, or an object containing values to initialize the newly instantiated object with.
I don't understand how people figure things like this out:
login: computed(function() {
return Login.create(
getOwner(this).ownerInjection()
);
}),
I don't know what it does, and I'm having a hard time educating myself from the documentation.
I recommend some verbosity on this construct, or if it is in fact explained somewhere, a link in the quoted documentation would benefit the reader.
@Redsandro I see you took this from ember-bootstrap, where this was used in a simple demo to manually create an Ember.Object that has an owner, which is an application instance and gives you access to Ember's Dependency Injection (DI) system, which was needed there (e.g. injecting services into an object requires access to the owner).
But normally you don't need that in a real app, as things like models, controllers, services and components are not manually created, but automatically through Ember's DI. In that case you don't need that ownerInject thing, as they have access to the owner object automatically. This was just needed for this simple demo, where I didn't have a real ember-data store to create models...
And btw, ownerInjection is documented here: https://emberjs.com/api/ember/3.8/classes/ApplicationInstance/methods/ownerInjection?anchor=ownerInjection. Again it's just a rarely needed thing, that's why it's not mentioned a lot.
@simonihmig thanks for the explanation. If it's that obscure, I don't think the docs would need to be updated.
I know this is not within the scope of this issue, but what would be the correct way to recreate that example (i.e. validations on a model bound to a (very simple) form)? Is it correct to just create a 'real' model for the controller and replace the aforementioned login: computed with that model??
E.g.:
login: alias('model')
Yes, if this is an ember-data model fetched or created through the store, you won't need that special injection stuff, it will just work, including the validations from ember-cp-validations!
Thank you!
Thanks to OP for creating this issue.. I was trying to use ember-cp-validations on a normal json object (didn't want to use ember-data) and the addon was throwing an exception due to not having an owner.
Not much relevant info related to it on google until I stumped across this issue. Passing the owner to create() solved my issue :
EmberObject.extend(Validations, myJsonData).create( getOwner(this).ownerInjection(());
Thanks @Redsandro
Most helpful comment
Thanks to OP for creating this issue.. I was trying to use ember-cp-validations on a normal json object (didn't want to use ember-data) and the addon was throwing an exception due to not having an owner.
Not much relevant info related to it on google until I stumped across this issue. Passing the owner to create() solved my issue :
EmberObject.extend(Validations, myJsonData).create( getOwner(this).ownerInjection(());Thanks @Redsandro