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

Ionic Beta: Changing Controllers/Delegates .. again!

Open ajoslin opened this issue 12 years ago • 38 comments

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

ajoslin avatar Mar 24 '14 16:03 ajoslin

/cc @D3CK3R

ajoslin avatar Mar 24 '14 16:03 ajoslin

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?

mlynch avatar Mar 24 '14 17:03 mlynch

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.

ajoslin avatar Mar 24 '14 17:03 ajoslin

So would this be backwards compatible then?

mlynch avatar Mar 24 '14 17:03 mlynch

We could make the raw instance of $ionicScrollDelegate control everything, then a method for specific ones.

$ionicScrollDelegate.scrollTop();
$ionicScrollDelegate.byHandle('h').scrollTop();

ajoslin avatar Mar 24 '14 17:03 ajoslin

I like that. Maybe byHandle change to withInstance or fromHandle or fromId?

mlynch avatar Mar 24 '14 17:03 mlynch

withInstance!

ajoslin avatar Mar 24 '14 17:03 ajoslin

@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.

rvanbaalen avatar Mar 24 '14 18:03 rvanbaalen

I actually like withHandle, @mlynch ?

ajoslin avatar Mar 24 '14 18:03 ajoslin

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.

mhartington avatar Mar 24 '14 18:03 mhartington

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 avatar Mar 24 '14 21:03 D3CK3R

@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?

ajoslin avatar Mar 24 '14 21:03 ajoslin

http://ajoslin.github.io/docs/nightly/api/service/$ionicScrollDelegate

ajoslin avatar Mar 24 '14 21:03 ajoslin

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 avatar Mar 24 '14 22:03 adamdbradley

@adamdbradley +1

mlynch avatar Mar 24 '14 22:03 mlynch

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?

ajoslin avatar Mar 24 '14 22:03 ajoslin

$ionicSideMenuDelegate landed via c2765451808003438f5820d15a80cacb4c831533.

http://ajoslin.github.io/docs/nightly/api/service/$ionicSideMenuDelegate/

ajoslin avatar Mar 24 '14 22:03 ajoslin

What about forHandle? Because we want to get a delegate for a handle? var mommy = $ionicScrollDelegate.forHandle('mommy');

mbuster avatar Mar 24 '14 22:03 mbuster

I like it!

ajoslin avatar Mar 24 '14 22:03 ajoslin

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...

mbuster avatar Mar 24 '14 22:03 mbuster