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

Could you put together a codepen so that this is easier to replicate?

drewrygh avatar Dec 18 '14 22:12 drewrygh

Please refer to the below page.
http://codepen.io/dreamershl/pen/emzVKZ?editors=001

In home page there 2 buttons to create the modal box under the different scope. When you try to click the header button to slide the slider, the modal under the root scope will fail with the following error message.

  console:Delegate for handle "ruleSlides" could not find a corresponding element with delegate-handle="ruleSlides"! next() was not called!

Possible cause: If you are calling next() immediately, and your element with delegate-handle="ruleSlides" is a child of your controller, then your element may not be compiled yet. Put a $timeout around your call to next() and try again.

image

dreamershl avatar Dec 19 '14 06:12 dreamershl

This is messing me up too. Debugging also tracked back to $ionicHistory.isActiveScope which always returns false

chris-carrington avatar Dec 23 '14 20:12 chris-carrington

I have no idea if this is a fix, i swapped the return statement in isActiveScope around like so:

// return currentHistoryId ? currentHistoryId == 'root' : true;
return currentHistoryId ? true : currentHistoryId == 'root';

So far, this appears to be working in my app and has fixed the slidebox issue, but the internal workings of the framework is beyond me so a yay or nay from the dev team would be needed.

billymedia avatar Dec 28 '14 11:12 billymedia

The issue for me is if an ionContent is on the screen that is not apart of the activeScope you no longer allow us to grab it via the delegate-handle with $ionicScrollDelegate.

This really screws me up b/c I have custom modals that use ionContent but are not apart of the activeScope within your $ionicHistory b/c I am dynamically adding them to the DOM and giving them their own scope.

Within ionic.js on line 96 this if statement will allow you to grab the ionContent under the conditions it describes.

if ((!handle || handle == instance.$$delegateHandle) && instance.$$filterFn(instance)) {
   ...
}

The second piece of the if statement triggers this function which can be found on line 33 within scrollController.js which checks that the handle is apart of the active scope.

var deregisterInstance = $ionicScrollDelegate._registerInstance(
   self, scrollViewOptions.delegateHandle, function() {
      return $ionicHistory.isActiveScope($scope);
   }
);

The above lines of code are copied and pasted within several files in ionic. My issue is specifically with the line return $ionicHistory.isActiveScope($scope);

There are certain cases where an ionContent won't be apart of the activeScope so this triggers false which disallows me to grab it with something like.

$ionicScrollDelegate.$getByHandle('mainScroll').scrollTop();

@drewrygh Is there any good reason for the isActiveScope() check?

chris-carrington avatar Dec 29 '14 16:12 chris-carrington

@nextbigleap @drewrygh

Work around function I've built. Grabs the view no matter what the history stack or historyId is and lets you run view functions on it. If you need more functions you can just add it to the service. I like this way a little better @nextbigleap b/c we don't have to touch their source code.

angular.module('app.service.inContent', ['ionic'])



.service('inContent', ['$ionicScrollDelegate', '$timeout', 

function($ionicScrollDelegate, $timeout)
{
   function getView(name)
   {
      var instances = $ionicScrollDelegate.$getByHandle(name)._instances;
      var view = _.where(instances, { '$$delegateHandle': name })[0];

      return view;
   }

   return {
      top: function(name) 
      {
         $timeout(function()
         {
            var view = getView(name);
            view.scrollTo(0, 0, false);
         });
      },
      resize: function()
      {
         $timeout(function()
         {
            var view = getView(name);
            view.resize();
         });
      }
   };
}]);

Example HTML:

<ion-content delegate-handle="tags"></ion-content>

Example JS

inContent.top('tags');

chris-carrington avatar Dec 29 '14 18:12 chris-carrington

Actually I like this way better. Now you can just use the existing getByHandle api that has documentation! http://ionicframework.com/docs/api/service/$ionicScrollDelegate/

angular.module('app.service.inContent', ['ionic'])


.service('inContent', ['$ionicScrollDelegate', function($ionicScrollDelegate)
{
   return {
      get: function(name)
      {
         var instances = $ionicScrollDelegate.$getByHandle(name)._instances;
         return _.where(instances, { '$$delegateHandle': name })[0];
      }
   };
}]);

Example JS 1

$timeout(function()
{
   inContent.get('tags').scrollTop();
});

Example JS 2

$timeout(function()
{
   inContent.get('tags').resize();
});

chris-carrington avatar Dec 29 '14 18:12 chris-carrington

I'm having this same issue - trying to get a scroll delegate by handle for ion-content inside a modal in beta 14 and it returns the following:

"Delegate for handle "composer1" could not find a corresponding element with delegate-handle="composer1"! scrollTop() was not called! Possible cause: If you are calling scrollTop() immediately, and your element with delegate-handle="composer1" is a child of your controller, then your element may not be compiled yet. Put a $timeout around your call to scrollTop() and try again."

Using @chris-carrington 's solution from directly above makes everything work. Thanks Chris!

btw - I'm not using underscore, so changed the code to the following.

    .service('inContent', function($ionicScrollDelegate)
    {
       return {
          get: function(name)
          {
             var instances = $ionicScrollDelegate.$getByHandle(name)._instances;
             return instances.filter(function(element) {
                return (element['$$delegateHandle'] == name);
             })[0];
             //return _.where(instances, { '$$delegateHandle': name })[0];
          }
       };
    })

rajatrocks avatar Jan 05 '15 00:01 rajatrocks

Based on the code from @chris-carrington, I ended up creating a proxy scroll delegate that I can use in my code instead of $ionicScrollDelegate. When this problem is eventually fixed, I can just return "standard" instead of "custom" in the service and not have to change code throughout my app.

    // $ionicScrollDelegate not working for content in modals
    // as described here: https://github.com/driftyco/ionic/issues/2754
    // So using an intermediary service until it's fixed. 
    // Change to return standard and it should use native functionality. 
    .service('myScrollDelegate', function($ionicScrollDelegate)
    {
        var custom = {
            $getByHandle: function(name) {
                var instances = $ionicScrollDelegate.$getByHandle(name)._instances;
                return instances.filter(function(element) {
                    return (element['$$delegateHandle'] == name);
                })[0];
            }
        };

        var standard = {
            $getByHandle: function(name) {
                return $ionicScrollDelegate.$getByHandle(name);
            }
        };

        //return standard;
        return custom;
    })

Calling it looks like this:

myScrollDelegate.$getByHandle("composer1").resize();

rajatrocks avatar Jan 05 '15 00:01 rajatrocks

@rajatrocks Cheers mate!

chris-carrington avatar Jan 08 '15 09:01 chris-carrington

Ok so the main problem is that there can be many instances of the same delegate handle because of cached views. So let's say we have 10 views, each with the "myHandle" delegate handle. When you go to get "myHandle" it's going to find 10 of them, rather than just one. The active scope could makes it possible to actually find the correct modal/scroll.

In this example you show where it does and doesn't work by passing in the correct scope or root scope: http://codepen.io/ionic/pen/emyvmM?editors=101

Will the solution of passing the scope not work?

adamdbradley avatar Feb 10 '15 18:02 adamdbradley

  • Makes sense.
  • But unfortunately my custom modal solution is outside the realm of your cached view solution.
  • I could see an ionic upgrade where you check cached view handles first before resorting to the pool, or an ionic upgrade where you throw an error if you go to check the pool and find multiple with the same name.
  • However, as long as you guys keep a pool of all handles within the $ionicScrollDelegate I'm a happy camper, and really don't need anything else!

chris-carrington avatar Feb 11 '15 04:02 chris-carrington

I am still having this problem with modals created from root scope in 1.0.1.

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

I'm using the modified get function from @chris-carrington but using a provider decorator to replace the method so I don't need to change any code if/when this is fixed.

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

function $ionicSlideBoxDelegateDecorator($provide) {
  $provide.decorator('$ionicSlideBoxDelegate', ['$delegate', decorateService]);

  function decorateService($delegate) {
    $delegate.$getByHandle = function $getByHandle(name) {
      var instances = this._instances;
      return instances.filter(function (element) {
        return (element['$$delegateHandle'] == name);
      })[0];
    };

    return $delegate;
  }
}

meticoeus avatar Sep 13 '15 12:09 meticoeus

I have spent several hours struggling to get $ionicScrollDelegate.$getByHandle working inside an $ionicModal and finally stumbled upon this thread.

The patch @rajatrocks posted on Jan 4 fixed the issue — thank you! Used it as a drop in replacement for $ionicScrollDelegate and it works perfectly. :+1:

Running ionic info shows:

Cordova CLI: 5.3.3
Gulp version:  CLI version 3.9.0
Gulp local:   Local version 3.9.0
Ionic Version: 1.1.0
Ionic CLI Version: 1.6.5
Ionic App Lib Version: 0.3.9
ios-deploy version: Not installed
ios-sim version: 3.1.1 
OS: Mac OS X Yosemite
Node Version: v4.1.1
Xcode version: Xcode 7.0.1 Build version 7A1001 

kylefox avatar Oct 05 '15 23:10 kylefox

Quick note — ionic info appears to show I'm on "Mac OS X Yosemite", but I'm definitely on El Capitan:

image

kylefox avatar Oct 05 '15 23:10 kylefox

@kylefox could you open an issue for this on https://github.com/driftyco/ionic-app-lib

mhartington avatar Oct 05 '15 23:10 mhartington

@mhartington You bet, simple fix — https://github.com/driftyco/ionic-app-lib/pull/31

kylefox avatar Oct 06 '15 01:10 kylefox

Possible cause: If you are calling slide() immediately, and your element with delegate-handle="propertySlide" is a child of your controller, then your element may not be compiled yet

@meticoeus I have the same problem with the version 1.1 .Could you tell me how to fix it?thanks.

Mr-Luo avatar Oct 15 '15 06:10 Mr-Luo

@chris-carrington, @rajatrocks, @meticoeus: Great way to work around the issue. Thanks.

@adamdbradley: I see your point. But leaving the filter function (ie. $ionicHistory.isActiveScope) as it is, means we wouldn't be able to use $getByHandle with a couple essential ionic delegate services (namely $ionicListDelegate, $ionicScrollDelegate, $ionicSideMenuDelegate, and $ionicSlideBoxDelegate) in modals whose scopes are children of $rootScope, which is actually the default behaviour of ionicModal. It doesn't make much sense to me that a default behaviour would lead to such pitfall. I believe this issue worths a second look.

So let's say we have 10 views, each with the "myHandle" delegate handle.

Admittedly, I was a little surprised when I saw this line. The reason was that I always had the impression that each delegate handle should be unique across all the view of the app. I guess that impression was influenced by the use of the word "handle" in other contexts, such as file handle. Anyways, my question to you is what is the use case of having one delegate handle being used across multiple views? As someone who always keep his delegate handles unique across the app, I am curious to learn from you. Cheers.

tamakisquare avatar Nov 03 '15 05:11 tamakisquare

someone correct me if I'm misunderstanding the way $ionicHistory works but @tamakisquare I believe the typical use case that you run into is when you have a view defined as follows and multiple versions cached with different id parameters:

// in your ui-router config file
$stateProvider.state('app', {
  url: "/channels/:id",
  views: {
    'app-view': {
      templateUrl: "feeds.html",
      controller: "FeedController"
    }
  },
  resolve: {
    feedList: ['feedService', '$stateParams', function (feedService, $stateParams) {
      return feedService.getFeedList($stateParams.id);
    }]
  }
});

//feeds.html
<ion-scroll delegate-handle="feedScrollHandle">
  <ion-list>
    <ion-item ng-repeat="feedEntry in feedList">
      {{feedEntry.name}}
    </ion-item>
  </ion-list>
</ion-list>

//FeedController.js
angular.module('app')
  .controller('FeedController', ['$scope', '$ionScrollDelegate', 'feedList', function FeedController($scope, ionScrollDelegate, feedList) {
    $scope.feedList = feedList;

    // do something with $ionScrollDelegate.getHandle('feedScrollHandle')
  }])

meticoeus avatar Nov 03 '15 12:11 meticoeus