Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get reference to view model with compose #90

Closed
olanod opened this issue Aug 3, 2015 · 9 comments
Closed

Get reference to view model with compose #90

olanod opened this issue Aug 3, 2015 · 9 comments

Comments

@olanod
Copy link

olanod commented Aug 3, 2015

I have a composed view <compose view-model="app/some/nice-view"></compose> and I'd like to reference the inner view model form a sibling element of the compose or from the js code.
I'd like to do something like this:

<button click.delegate="foo.jump()">Click Me</button>
<compose view-model.ref="foo" view-model="app/some/nice-view"></compose>
export class NiceView {
    ...
    jump(){...}
}
@EisenbergEffect
Copy link
Contributor

Well, you can get a hold of the compose's view model with compose.ref="foo". From there you can probably get at the composed view model instance.

@olanod
Copy link
Author

olanod commented Aug 3, 2015

Well the view model instance would be foo.currentViewModel, but there's a timing issue. If I want to assign this view model to my current view model there's no clean way to do it because that property(currentViewModel) only exists when the composed element is attached(bound?) and that happens after the callbacks of my current view model are called.

@EisenbergEffect
Copy link
Contributor

Did you try doing <button click.delegate="foo.currentViewModel.jump()">Click Me</button> ?

@olanod
Copy link
Author

olanod commented Aug 3, 2015

That should work, but I wish there was a simpler/prettier way because I have to access that more than once, and that's just an example, I actually have to use it in one place like this this.$parent.$parent.foo.currentViewModel.otherThing.someMethod()(btw it would be nice to get this $parent reference in nested routes automatically).
As you see it's getting ugly. Some other thing that could help is if the view model is injectable somehow.

@EisenbergEffect
Copy link
Contributor

It can't be injectible because it can dynamically change at any time if it's bound. We could add an event to the compose element that fires whenever a composition completes.

@jdanyow
Copy link
Contributor

jdanyow commented Nov 12, 2015

@olanod can this be closed? This week's release eliminates the need to use $parent.

@jdanyow
Copy link
Contributor

jdanyow commented Nov 30, 2015

closing- @olanod please re-open if you're still having trouble with this.

@jdanyow jdanyow closed this as completed Nov 30, 2015
@timfish
Copy link

timfish commented Nov 11, 2016

I'm trying to use compose.ref with typescript but the currentViewModel doesn't exist in any definitions. Does that means it's not a public API or are the definitions incomplete?

@eamodio
Copy link

eamodio commented Jan 11, 2017

@jdanyow I had the same question as @timfish -- can currentViewModel safely be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants