ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

v1.0.0-beta.14: All DelegateService function in the modal under rootScope will always fail $ionicHistory.isActiveScope() check

Open dreamershl opened this issue 11 years ago • 24 comments

For example, create a modal page under rootScope with the slide box. We won't be able to call the slide box functions.

Because the slide box delegate service function will verify whether the current scope (modal scope) is active (not in the cache) by calling the $ionicHistory.isActiveScope(). And this "isActiveScope" will stopped at ionSideMenus scope, which is a sibling scope of the modal scope.

dreamershl avatar Dec 18 '14 03:12 dreamershl

@meticoeus - Thanks for illustrating the use case of same delegate handle being used by multiple views. With that bear in mind, I think the direction you mentioned earlier, with a question, would be the easiest way to patch up the issue without inflicting issues elsewhere in the code.

Maybe modal scope should be treated specially to get around this problem?

I went ahead and made the patch, only for $ionicSlideBox as that's the only affected Ionic module that I am using for the time being, to my own fork of Ionic. The patch is based on the fact that for $ionicModal instances that are initialized without specifying a scope, the instances themselves are assigned to their isolated scopes (see the code below).

// If this wasn't a defined scope, we can assign the viewType to the isolated scope
// we created
if (!options.scope) {
  scope[ options.viewType ] = modal;
}

For those who are interested, here is my change to $ionicSlideBox (Ionic v1.1.1)

Changed from

var deregisterInstance = $ionicSlideBoxDelegate._registerInstance(
  slider, $attrs.delegateHandle, function() {
    return $ionicHistory.isActiveScope($scope);
  }
);

To

var deregisterInstance = $ionicSlideBoxDelegate._registerInstance(
  slider, $attrs.delegateHandle, function() {
    return $ionicHistory.isActiveScope($scope) || isInRootScopeModal($scope);

    function isInRootScopeModal(scope) {
      var s = scope;

      // If `$parent` is `null`, then the root scope has been reached, at which point the loop should stop.
      while (s.$parent !== null) {
        if ('modal' in s) { return true; }
        s = s.$parent;
      }

      return false;
    }
  }
);

If there's enough interest, I will create a PR and apply this solution on all the other affected delegate services, such as $ionicScroll.

tamakisquare avatar Nov 09 '15 18:11 tamakisquare

I couldn't make the dynamically generated ion-scrolls to be referenced by handle, so I borrowed a code snippet from above and mixed in the worst ideas from PHP with stunning results.

angular.module('ionic').config(['$provide', $ionicScrollDelegateDecorator]);

function $ionicScrollDelegateDecorator($provide) {
  $provide.decorator('$ionicScrollDelegate', ['$delegate', decorateService]);

  function decorateService($delegate) {
    $delegate.$getByRealHandle = function $getByRealHandle(name) {
      var instances = this._instances;
      return instances.filter(function (instance) {
        return (angular.element(instance.element).attr('delegate-handle') == name);
      }).shift();
    };

    return $delegate;
  }
};

So now if calling $getByHandle() doesn't work, just use $getByRealHandle() :D

$ionicScrollDelegate.$getByRealHandle(myHandle);

integry avatar Jan 20 '16 14:01 integry

@integry mind that (if I'm not mistaken) handle is not required to be globally unique. It only has to be unique on a single ionView. The implementation you suggested may return incorrect instance in case the same handle has been used in both modal and view. I suppose the following implementation is safer because it will only work different way than $getByHandle if handle is globally unique. Otherwise it will show the error anyway.

$provide.decorator('$ionicScrollDelegate', function ($delegate) {
  return Object.assign($delegate, {
    $getByAbsoluteHandle(delegateHandle) {
      const instancesByHandle = this._instances
        .filter(instance => instance.$$delegateHandle === delegateHandle);

      return instancesByHandle.length === 1 ?
        instancesByHandle[0] :
        this.$getByHandle(delegateHandle);
    },
  });
});

Too bad there's no "official" way of decorating the ionic.DelegateService so all delegates have this method. This decorator has to be applied one-by-one to delegates used on modals.

jrencz avatar Feb 05 '16 10:02 jrencz

As an alternative to filtering, I've modified the ionic-angular.js source to support a dynamic handle by letting the scope bind it. So far its working fine for dynamic-but-set-once-immediately cases. This approach does not support changing the handle later.

My use case is generating a small list of sliders so I'm using the ids/indexes in the list to create handles to each item.

I changed the scope definition to include

scope: {
...,
delegateHandle: '@'
}

and modified the register method from

var deregisterInstance = $ionicSlideBoxDelegate._registerInstance(
  slider, $attrs.delegateHandle, function() {
    return $ionicHistory.isActiveScope($scope);
  }
);

to

var deregisterInstance = $ionicSlideBoxDelegate._registerInstance(
  slider, $scope.delegateHandle, function() {
    return $ionicHistory.isActiveScope($scope);
  }
);

meticoeus avatar Feb 06 '16 14:02 meticoeus