Skip to content

API to access to mounting element for state (uiView element in html) #3573

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

Closed
5 of 6 tasks
falsyvalues opened this issue Dec 7, 2017 · 2 comments
Closed
5 of 6 tasks

Comments

@falsyvalues
Copy link

This is a:

  • Bug Report
  • Feature Request
  • General Query

My version of UI-Router is: 1.0.11 ng1

Feature Request

Would be nice to have an access to list of mounting elements (uiViews) corresponding to state, possibly through existing views() API.

Code below is an example what can be achieved right now but contains some downsides (but illustrate the idea behind this):

  • Search is done through all document (its not state context aware like views())
  • Search is done on every invoke
  • Supports only one view (How to query for a proper element using views() data?)
$transitionsProvider.onStart({
    from: 'a.*',
    to: 'a.*'
}, (transition) => {
    const fromState = transition.from();

    if (typeof fromState.template === 'string') {
        // Returns as an exmaple "my-directive" it will looks different with components case
        const elementName = getElementNameFromTemplate(fromState.template);
       // There is no way to find proper uiView element as a root
        const elementInsideSomeUiView = angular.element(document).find(elementName);
        // Do anything with element
    }
});

General Query

Described in Feature Request.

@christopherthielen
Copy link
Contributor

Here's what I recommend:

  • Create a ui-view directive and track when the directive is created and destroyed. Keep a reference to the directive's element. (the directive will share the same element as ui-router's ui-view)
  • Register a transition hook which checks all the elements' angular.element().data('$uiView') data.

Plunker example: http://plnkr.co/edit/vPSZL8X78v8IUh4acorv?p=preview

I've also added an onSync callback to the view plugin API. I will release the new onSync api in a future release of ui-router. Plugin APIs are lower level than the rest of ui-router APIs and is more likely to change in the future. The onSync API will informs listeners of each $view.sync() event and provides the list of all active ui-view and the matching view configs (from state views block). This API may never provide an actual element as part of the api because it does not always make sense in all frameworks and environments (such as server-side rendering)

@falsyvalues
Copy link
Author

Hi @christopherthielen thanks for looking into it.
I guess example could be extended with some injectable registry of ui-views (managed via directive) which could be useful with onSync 🎉.
Looking forward for onSync release.

wawyed pushed a commit to wawyed/core that referenced this issue Mar 3, 2018
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

2 participants