-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First pass at state directive for auto-generating link URLs #139
Conversation
Thanks @nateabele! |
Ill check it out tonight |
Looks great! I'm so glad you decided to just take the initiative on this, I'm sure @ksperling appreciates the help. Only couple of thoughts:
|
Also just noticed for non-form objects we are adding the associated state url to the href attribute, couple thoughts there:
|
Yeah, that commit actually got carried over from #138, but, IMO those tests would be kind of redundant. The
I don't have a real strong opinion here, but I do love me some terse syntax. :-) Honestly, I don't think it conveys any information that couldn't easily be inferred from context. It might make more sense to me if we had multiple state-related directives. If multiple people feel strongly about this, I can change it.
Bookmarks... general compatibility with the rest of the web.
Currently it would, yeah. I can add a check to prevent attempts to generate a URL for non-navigable states. I can still see a case where people would want to use links to transition to states that aren't associated with URLs. |
Is this PR going to need an update after pulling in #138 ??... (I can see that it includes the same change, so I was wondering how Git handles that, still sort of new to Git after all) |
@jeme It shouldn't matter. |
@nateabele Great work! I'm still not entirely convinced a custom syntax is the way to go, but I guess with scope.eval() doing most of the work it's actually not that much of a problem. I do think ui-state is somewhat generic as a directive name. What about ui-ref or ui-sref? Support for form elements is interesting... how would that actually be used? |
I think for states without a URL href an option might be to generate a link to the nearest navigable ancestor, but have the onlick transition to the state itself. We would need a flag or options parameter on $state.href() for that -- I think the default behavior of $state.href() should remain to fail on non-navigable. In terms of tests it might be nice to have a test for testing that changes in the parmeter value are handled, i.e. that href is updated. |
It was the only way I could think to do a reasonably terse syntax. I'm open to other options.
I'm down with
Agreed with all this. I'll try to spend some time on it later today. |
My initial thought was that it could be used for doing state transitions if the form submit is successful, but that might need to be too tied into how form submits are handled. It would be nice if you could do something like this in an submit: function($event) {
this.item.$save(function() {
// Pseudo-code to trigger the transition in the form's ui-ref:
$event.$success();
});
} That way, the controller code would not have to be tightly coupled to state reference information, which logically belongs with the form. Also, there are generic benefits to generating form |
I like ui-sref better than ui-ref which is too generic still. Needs the 's' to represent state, in my opinion. |
@nateabele presumable in the cmd-enter case you'd have to handle the form submit server side? Or are we talking about GET only? |
@timkindberg I just decided a few minutes ago that I like it better, too. ;-) |
@ksperling Correct, the submit would have to be handled server-side. What? Don't all your apps degrade gracefully, too? ;-) |
http://plnkr.co/edit/pwACBH?p=preview |
After a bit of refactoring, I believe this is ready to roll. It is dependent on #192, which should be merged first. You'll notice the test that verifies the click behavior is a little weird. Unfortunately the methods that are supposed to exist to test events... don't. I have no idea why this is, but I was able to work around it. Let me know if anybody has any questions. |
if (!nav) return; | ||
|
||
try { | ||
element[0][attr] = $state.href(ref.state, params, { lossy: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lossy is already the default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it better to make that explicit, especially since there was discussion around reversing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you wrapping this in a try block because href() may throw an error? I'm wondering if it would be more helpful if href() returned undefined or maybe "#" on invalid states? Devs might use the method in a loop through states that may or may not have urls, might be annoying to always have to wrap it in a try block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I think null
would probably be best then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right ok. Hmmm. I think it would be more convenient as null in use. It
doesn't seem fatal enough to throw an error in my opinion. I wonder what
@ksperling thinks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the consensus is that null
is the way to go, that's fine with me. @ksperling?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exciting. I hope sref can degrade gracefully if it is used in
unexpected places or can't do its job due to bad state definitions (or
whatever) as would fit html norms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess returning null from href() seems OK for a non-navigable state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though I generally also like things to fail at the point where something is actually wrong, instead of silently passing nulls or other invalid data around and having it blow up later at a completely unrelated place. But in this specific case it's probably benign enough.
just put a few comments in the diff |
@ksperling I replied to everything with what I think were adequate explanations. Let me know if you have any follow-ups. |
First pass at state directive for auto-generating link URLs
@ksperling Hey, I was gonna change |
Yeah go ahead... I thought I'd just merge this to get the ball rolling. |
Hey guys,
So I was messing around and just went ahead and took a pass at implementing a
ui-sref
directive. Based on my comments from #16 (comment), and elaborating slightly, valid syntax is any of the following:...where
state
andparams
are any valid parameter pair for$state.transitionTo()
, except that within the context of the directive,state
is a literal, not a quoted string. The value forparams
can be an object literal,scope
property, or the result of ascope
method call. For example:Binding & Events
The directive will auto-update
href
for links andaction
for forms. For links, the default click behavior is suppressed in favor of using$state.transitionTo()
.Notes
$state.href()
.ng-href
with$state.href()
.Interested to hear your thoughts, cc: @timkindberg, @ksperling, @jeme, @ProLoser, etc.