Skip to content

RFC: State toggle (ui-sref-toggle) #1890

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
elgerlambert opened this issue Apr 17, 2015 · 2 comments
Closed

RFC: State toggle (ui-sref-toggle) #1890

elgerlambert opened this issue Apr 17, 2015 · 2 comments

Comments

@elgerlambert
Copy link

Hi, I have a use-case whereby a button effectively opens and closes a form when clicked. The form that is opened is defined in a child state and is targeted through a relative ui-sref=".new". I've currently hacked this behaviour by creating the following directive.

  angular.module('sw.util')
    .directive('uiSrefToggle', function ($timeout, $state) {
      return function (scope, element) {
        element.on('click', function (e) {
          var stateChanged = false
          var unbindListener = scope.$on('$stateChangeStart', function () {
            stateChanged = true
            unbindListener()
          })

          $timeout(function () {
            if (stateChanged) return
            unbindListener()
            $state.go('^')
          }, 200)

          e.preventDefault()
        })
      }
    })

I was wondering whether you would consider/be interested in a PR that would add this type of behaviour to ui-router?

API Options

<!-- option 1 -->
<a class="btn btn-default" ui-sref-toggle=".new"></a>
<!-- option 2 -->
<a class="btn btn-default" ui-sref=".new" ui-sref-toggle="true"></a>

I feel option 1 is the cleaner of the two from a user's perspective. I suspect option 2 is easier to realise/implement; I had a 2 line fix in ui-router for quite a while that realised this state toggle behaviour by checking whether the attribute 'ui-sref-toggle' was present.

Alternative

An alternative route to take would be to consider whether it's possible to make the ui-sref directive more extensible so that developers might be able to hook these type of use-cases into ui-router..? This would perhaps fit with nateabele's remark in issue #1863.

If you want to resubmit a separate PR that just factors out the uiSref link function into a controller, that'd be fine, too. Then you'll have a more flexible API for making directives that goes beyond what's available in the core if you need to do really specific stuff.

But I'm not sure whether that would work for this use-case - haven't gotten my head around it yet. Wanted to hear your thoughts first!

@nateabele
Copy link
Contributor

Hey, sorry for the late feedback. Interesting concept, don't love the implementation. Again, as with other proposals elsewhere (including #1863), you're munging together two unrelated concerns. We could theoretically have thousands of these.

If you're interested in taking up the call for refactoring per my notes in the other issue, I'd be happy to guide you, and it'd probably get you a cleaner, more general solution in your own code.

@eddiemonge
Copy link
Contributor

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

3 participants