0

I am building an Angular application that utilizes the https://github.com/bespokejs/bespoke presentation plugin to create dynamic slides. My slides come from an ng-repeat which generates the HTML. My problem is that depending on the length of the array from which the ng-repeat reads, my slide-deck is instantiated expecting more slides then there are.

For example, if I instantiate the deck on an array that has 12 items for ng-repeat, and then hit another call to pinActiveCategory() with an array of 6 items, the deck still thinks there are 12 slides, even though it will only populate 6, creating an ugly scenario where my user is cycling through empty slides.

My view:

<div id="left" ng-class="butterfly">
<div id="carousel">
<article>
<section ng-repeat="carousel in theminiBreadsList">
<img ng-src="img/{{carousel.type|lowercase|nospace}}big.jpg">
<p class="breadDescribe"><span class="buttons"><i ng-click="pinActiveBread($event,carousel.text)" class="fa fa-arrows-alt"></i><i ng-click="initiateCart()" class="fa fa-cart-plus"></i><i class="fa fa-question"></i></span><span class="carousel-text"><u>{{carousel.text}}</u></span></p></section>
</article>
</div>
</div>

My controller:

    $scope.pinActiveCategory = function($event, category) {
  $scope.show.push('show');
  for (var i = 0; i < $scope.theBreadsList.length; i++) {
    if ($scope.theBreadsList[i].category == category) {
      $scope.theminiBreadsList = angular.copy($scope.theBreadsList[i].breads);
      $scope.butterfly.push("show");
      $scope.$watch("theminiBreadsList", function(newValue, oldValue) {
        var butterfly = function() {
          $timeout(function() {
            $(".bespoke-active img").css("transform", "scale(1.0)");
            $(".bespoke-before img").css("transform", "scale(1.1)");
            $(".bespoke-after img").css("transform", "scale(1.1)");
            $scope.theActiveLoaf = $(".bespoke-active .carousel-text").text();

            function isDaily(nameKey, myArray) {
              for (var i = 0; i < myArray.length; i++) {
                for (var z = 0; z < myArray[i].breads.length; z++) {
                  if (myArray[i].breads[z].text === nameKey) {
                    $scope.isTheActiveBreadDaily = myArray[i].breads[z].isDaily;
                  }
                }
              }
            }
            isDaily($scope.theActiveLoaf, $scope.theBreadsList);
          }, 999);
        };
        var deck = bespoke.horizontal.from('#carousel article', {
          fx: {
            direction: "horizontal",
            transition: "carousel"
          }
        });
        butterfly();
        deck.on('activate', function(event) {
          butterfly();
        });
      });
    }
  }
},

I know that the problem has to do with when the bespoke command runs relative to when the scope is updated with the new theminiBreadsList I'm just not sure what to do to solve it. I tried moving around the code-block that starts the slide-deck, but that didn't do anything useful, I just ended up breaking more of it than fixing anything, so I'm looking for another set of eyes for some help, thank you!

Edit: Basically I'm looking for an answer that helps me call the deck variable (starting the slideshow) at the right time so that it knows how many section elements and therefore how many slides there should be.

Summer Developer
  • 2,056
  • 7
  • 31
  • 68
  • The lack of proper indentation makes the code hard to understand. Is it setting `$watch` inside of a `ng-click` function? What's with that? – georgeawg Dec 26 '15 at 03:34
  • @georgeawg Yes, this was the only way to get it working to were it would update the slides each time `theminiBreadsList` changed, but I guess it is still a bit wonky. I was just playing around with `$watch` in an attempt to get it to work, and I am close. – Summer Developer Dec 26 '15 at 03:45
  • 1
    What is `pinActiveCategory` supposed to do? Seems there is too much going on. Can you replicate it in a plunker perhaps? – tasseKATT Dec 26 '15 at 09:09
  • @tasseKATT The main thing is that it updates `theminiBreadsList` and initiates the slide-deck through `bespoke.horizontal.from('#carousel article',`. The `theminiBreadsList` is what is used in the ng-repeat, and this determines how many sections there are. My problem is that the slide-deck doesn't know how many sections there actually when it is initiated, which I don't understand because it is in the `$watch` function. – Summer Developer Dec 26 '15 at 12:40

0 Answers0