Is there any way to reference a model in a component without explicitly passing the model?


#1

Hello everyone,

I’m relatively new to ember and have a question regarding building components. I am trying to build a library to quickly create inputs using ember components. I am aware libraries like this exist already but I figured I’d build it as a means of acquiring ember experience.

Currently my interface looks something like this,

#sometemplate.hbs
{{quick-input input-model=model attribute="name"}}

and my component looks something like this

#quick-input.js
export default Ember.Component.extend({
  value: function() {
    return this.get('input-model')[this.get('attribute')]
  }.property('input-model.name')
})

#quick-input.hbs
<label>{{attribute}}</label>
<input value={{value}}>

Is there any way to reference the model without explicitly passing it into the component?

My ideal component API would be something like this,

#sometemplate.hbs
{{quick-input attribute="name"}}

Sorry if this is a dumb question or one that is answered explicitly in the docs somewhere, I couldn’t find anything.

Thanks!


#2

The short answer is no and don’t.

The long answer is you could get it from a singleton via a dependency injection or from the parentView.


#3

Cool, thanks! I noticed this after posting. “There is no access to the surrounding context or outer controller; all contextual information must be passed in.” Sorry for the noob question. :disappointed: