# What's the best way to pass a model to a component?

**URL:** https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191
**Category:** CanJS
**Created:** [March 18, 2016, 1:56am UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191 "2016-03-18T01:56:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![marshallswain](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/marshallswain/32/22_2.png) [@marshallswain](https://forums.bitovi.com/u/marshallswain)
#### Post date: [March 18, 2016, 1:56am UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/1 "2016-03-18T01:56:38Z")

</div>

When developing components that interact with a model, in order to pass the model into the component, I’m currently setting up the model on the parent component’s viewModel and passing it in as an attribute on the child component. Here’s a pseudo-code/abbreviated example:

```auto
// parent-component viewModel
import TxnModel from 'app-name/models/transaction';
viewModel: {
  TxnModel
};

```

```auto
<parent-component>
  <child-component {model}="TxnModel"></child-component>
</parent-component>

```

This is working just fine, but I’d like to see a way to import the model directly in the template, since in most cases I don’t really need it in the parent component. **Is there an existing way to do this?**

```auto
<parent-component>
  <can-import from="app-name/models/transaction" as="TxnModel">
  <child-component {model}="TxnModel"></child-component>
</parent-component>

```

Also, I’d prefer to use the adjacent (non-nested) structure, to avoid the async “pyramid of doom” in the html markup:

```auto
<parent-component>
    <can-import from="app-name/models/transaction">
        <child-component {model}="value"></child-component>
    </can-import>
</parent-component>

```

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [March 18, 2016, 3:00am UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/2 "2016-03-18T03:00:45Z")

</div>

Yes. I think @matthewp made another forum post about this. I’m on my cell so can’t easily search.

Basically you can export from an import with the normal view binding syntaxes to a reference scope.

---

<div class="post-metadata">

### Author: ![marshallswain](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/marshallswain/32/22_2.png) [@marshallswain](https://forums.bitovi.com/u/marshallswain)
#### Post date: [March 18, 2016, 3:26am UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/3 "2016-03-18T03:26:47Z")

</div>

Cool. Thanks. I’ll look for it.

---

<div class="post-metadata">

### Author: ![marshallswain](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/marshallswain/32/22_2.png) [@marshallswain](https://forums.bitovi.com/u/marshallswain)
#### Post date: [March 18, 2016, 3:36am UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/4 "2016-03-18T03:36:18Z")

</div>

I think it might be this one: [Using 2.3 binding syntax to import and use partials](http://forums.bitovi.com/t/using-2-3-binding-syntax-to-import-and-use-partials/22)

Does this mean that I should be able to do this:

```auto
<parent-component>
  <can-import from="app-name/models/transaction" {^value}="*TxnModel">
  <child-component {model}="TxnModel"></child-component>
</parent-component>

```

Or would I need to use the \* to pass it into the child-component? Like this:

```auto
<child-component {model}="*TxnModel"></child-component>

```

---

<div class="post-metadata">

### Author: ![marshallswain](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/marshallswain/32/22_2.png) [@marshallswain](https://forums.bitovi.com/u/marshallswain)
#### Post date: [March 18, 2016, 3:40am UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/5 "2016-03-18T03:40:54Z")

</div>

Also, help me understand this better. Should the asterisk in `*TxnModel` make the `TxnModel` variable available to a “distant cousin” component, like in this example:

```auto
<parent-component>
  <can-import from="app-name/models/transaction" {^value}="*TxnModel">
  <child-component {model}="TxnModel"></child-component>
</parent-component>
<distant-cousin {model}="TxnModel"></distant-cousin>

```

Or would I need to use `../` in some way to get it out to that level: `{^value}="../*TxnModel"` to get it up another level?

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [March 18, 2016, 12:12pm UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/6 "2016-03-18T12:12:14Z")

</div>

@marshallswain `*REF` keys are references to a template variables:

[https://canjs.com/docs/can.stache.key.html#section\_templatevariableoperator\_](https://canjs.com/docs/can.stache.key.html#section_templatevariableoperator_)

[https://canjs.com/docs/can.view.bindings.reference.html](https://canjs.com/docs/can.view.bindings.reference.html)

There’s one context for variables for the entire template. This means there’s only one `*TxnModel` allowed for everything.

Because you are exporting a function, you might need the `@` symbol to keep stache from running the function and getting its return value. Making your template look like:

```auto
<can-import from="app-name/models/transaction" {^@value}="*TxnModel">
<parent-component>
  <child-component {model}="@*TxnModel"></child-component>
</parent-component>
<distant-cousin {model}="@*TxnModel"></distant-cousin>

```

---

<div class="post-metadata">

### Author: ![marshallswain](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/marshallswain/32/22_2.png) [@marshallswain](https://forums.bitovi.com/u/marshallswain)
#### Post date: [March 20, 2016, 8:48pm UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/7 "2016-03-20T20:48:09Z")

</div>

It looks like with a generated SuperModel, since it exports both a default export and `connection`, this is the syntax you have to use. It’s pretty much the same as what you posted. Thank for your help.

```auto
<can-import from="app-name/models/transaction" {^@value}="*TxnModel">
<parent-component>
  {{#if *TxnModel}}
    <child-component {model}="@*TxnModel.default"></child-component>
  {{/if}}
</parent-component>

{{#if *TxnModel}}
  <distant-cousin {model}="@*TxnModel.default"></distant-cousin>
{{/if}}

```

---

<div class="post-metadata">

### Author: ![ilyavf](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/ilyavf/32/122_2.png) [@ilyavf](https://forums.bitovi.com/u/ilyavf)
#### Post date: [April 28, 2017, 2:03pm UTC](https://forums.bitovi.com/t/whats-the-best-way-to-pass-a-model-to-a-component/191/8 "2017-04-28T14:03:35Z")

</div>

Here is what worked for me:

```auto
<can-import from="~/models/biggest-movers" {^value.default}="*BiggestMovers" />

<biggest-movers-grid {model}="*BiggestMovers"/>

```

testing:

```auto
> canViewModel('biggest-movers-grid').model
function BiggestMovers(){return __.apply(this,arguments)}

> new canViewModel('biggest-movers-grid').model( { } )
BiggestMovers

```
