Ionic Beta: Changing Controllers/Delegates .. again!
Old Delegate System (0.9.x)
Was very easy-to-use, but not powerful.
If you wanted to target a specific instance of something, for example only make one specific ion-content scroll to top, it was not possible.
New Controller System (nightly)
Very powerful, but hard to use for some common cases. For example, if I have a "MainCtrl" on the body, then two scopes down an ion-content, the MainCtrl's scope can't reach the $ionicScrollController on the ion-content. Also, the new solution is very hard to write tests for.
Fixing both problems: Delegates plus Unique IDs (idea for 1.0)
The easiest way to explain is within the example in this short gist: https://gist.github.com/ajoslin/c1bed34eb83aeffdba4b
What do you guys think?
/cc @calendee @rvanbaalen @mhartington @mlynch @adamdbradley and everyone else who wants to input
/cc @D3CK3R
I think it's a best of both worlds solution, plus it will be backwards compatible with alpha, right? Since it's so close, we should ensure that it works out of the box with what we had before.
I would say let's do something else besides delegate-handle, maybe control-id so it's more generic? I know the delegate system will use it, but this way it's more flexible for other things to use it in the future.
Thoughts?
I did delegate-handle because it's the most intuitive thing I could think of. Using delegate keyword makes the user automatically think of 'delegate' in his mind, and using handle instead of id doesn't make it remind the user of the DOM's id attribute.
So would this be backwards compatible then?
We could make the raw instance of $ionicScrollDelegate control everything, then a method for specific ones.
$ionicScrollDelegate.scrollTop();
$ionicScrollDelegate.byHandle('h').scrollTop();
I like that. Maybe byHandle change to withInstance or fromHandle or fromId?
withInstance!
@mlynch I'd say let's not create some magic 'we can use this for anything' attribute like control-id.
delegate-handle is far easier to understand and to read. It makes the code more self-explanatory.
About the two comments above mine; mix 'em up and go for withHandle ? Assuming the use of delegate-handle, this makes the most sense to me.
I actually like withHandle, @mlynch ?
A little late on this, just saw the email notice, but I agree with @rvanbaalen. delegate-handle and withHandle makes much more sense. It keeps things more logical when going back and forth between the markup and javascript.
I'm a little late too, but thought a bit about the ideas. I also think that delegate-handlepaired with withHandle (better getHandle?)plus a general handler for all is a good solution. As a result it is not too complicated for newbies and flexible for some deeper logic. The only thing i don't like so much is the use of strings, but i have no really good alternative in mind :-) . I just don't like juggling around with a bunch of 'IDs'.
What i also think about is what should $ionicScrollDelegate.byHandle('h') return if the id is wrong or the delegate-handle just doesn't exist? We are currently using some dynamic templates to render data from a database. In the dataset you can check some properties to enable a custom behavior (scrolling for example) (ok, we can also check it by ourself before calling $ionicScrollDelegate) ... just some thoughts :-)
@D3CK3R great questions! Thanks!
You can try out the current implementation for scroll soon (I'll be committing within an hour I think, and without breaking changes with the $ionicScrollController for now).
This is how it works so far:
You can create a delegate instance even if a directive with that handle doesn't exist yet.
var someInstance = $ionicScrollDelegate.withHandle('someHandle');
However, if you try to call a method on someInstance before the directive with handle 'someHandle' is instantiated, it will simply do nothing. It will console.error() and be no operation. For example, if I call someInstance.scrollTop() and there is no directive yet, it will put this in the console (but continue the app):
'Delegate with handle "someInstance" could not find a corresponding element with delegate-handle="someInstance"! scrollTop was not called!'
What do you think?
http://ajoslin.github.io/docs/nightly/api/service/$ionicScrollDelegate
I'm not sure about the with prefix. If it's returning an instance or null, I vote getInstance or anything that starts with get. It seems to make more sense of what I'd expect the method to return, whereas with is something I'd have to question.
var someInstance = $ionicScrollDelegate.getInstance('someHandle');
@adamdbradley +1
It doesn't actually return null, I changed it a bit from what we discussed in chat (https://github.com/driftyco/ionic/issues/877#issuecomment-38505973)
Still think getInstance? Maybe byHandle or something?
$ionicSideMenuDelegate landed via c2765451808003438f5820d15a80cacb4c831533.
http://ajoslin.github.io/docs/nightly/api/service/$ionicSideMenuDelegate/
What about forHandle? Because we want to get a delegate for a handle?
var mommy = $ionicScrollDelegate.forHandle('mommy');
I like it!
A step further, is a given handle valid for multiple items? Seems like if you had a common delegate for two or more stacked scrolling areas (with different content sizes) you could implement parallax scrolling very simply. Completely untested or thought-out, of course...