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

Control widget on controller initialization #27

Closed
slavafomin opened this issue Oct 21, 2015 · 2 comments
Closed

Control widget on controller initialization #27

slavafomin opened this issue Oct 21, 2015 · 2 comments

Comments

@slavafomin
Copy link

Hello!

Thank you for this great module.

I have a use-case where I need to control initial state of the accordion according to the hash in the URL. I have several panes and each pane has a unique ID. If URL contains this ID I want to expand this tab on view initialization.

After some fiddling, I've came up with this solution:

var paneId = $location.hash();
if (paneId) {
  $scope.$on('$viewContentLoaded', function () {
    var paneIndex = $('#' + paneId).closest('v-pane').index();
    if (paneIndex > 0) {
      var cancelWatch = $scope.$watch('accordion', function (accordion) {
        $timeout(function () {
          accordion.expand(paneIndex);
        }, 0);
        cancelWatch();
      });
    }
  });
}

I have to use $scope.$on('$viewContentLoaded') and $scope.$watch('accordion') because I can't access accordion on scope directly, looks like it takes time to initialize it. This works, but it's very cumbersome approach.

Do you see a better way to approach this problem? Maybe we could somehow improve the design of the directive to address this issue?

I think, if we could give unique names to panes, e.g. my-super-pane than we could tell component to initially expand a pane for us, e.g. <v-accordion expand="my-super-pane">.

What do you think? Thank you!

@LukaszWatroba
Copy link
Owner

Hi!

Please download the latest release (1.4.0) and change your code to something like this:

Template:

<v-accordion id="my-accordion" control="myAccordion">
  <v-pane id="{{ pane.id }}" ng-repeat="pane in panes">
    <v-pane-header>
      {{ ::pane.header }}
    </v-pane-header>

    <v-pane-content>
      {{ ::pane.content }}
    </v-pane-content>
  </v-pane>
</v-accordion>

Controller:

$scope.$on('my-accordion:onReady', function () {
  myAccordion.toggle(yourSuperPaneId);
});

Basically now you can listen for yourAccordionId:onReady event and toggle the pane by it's id.
What do you think?

@slavafomin
Copy link
Author

Oh, thank you Lukasz! This is much better than my current implementation for sure ))
However, I think that we shouldn't move this logic to controller itself. The more Angularish approach would be to implement it on a directive level in my opinion, e.g. add something like I've mentioned earlier: <v-accordion expand="my-super-pane-name">.

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