10

Spoiler alert: this is NOW a question, so apologies to anyone that read it purely as a discursive topic :)

Anyway, I was doing a little research today re adding routes via javascript when i thought that a bit of google research wouldn't hurt. Basically, my aim was to do away with the following type of construct within my views:

alt text

and replace it with something akin to:

alt text

well, i lucked out a little today after finding this fantastic article (which isn't mine nor do i have any affiliation other than respect for the piece of work):

http://weblogs.asp.net/zowens/archive/2010/12/20/asp-net-mvc-javascript-routing.aspx

this really has been a missing link (or so i thought) for me when dealing with routes via javascript. However, the 2nd code example is misleading and actually won't produce what the example leads on. Can anyone suggest a fix for this and/or an alternative solution to allow this fluent convention of js routes within mvc views??

cheers...

[edit] - question edited 22:16 GMT to explore deeper options on this topic, plus changed title (removed OT portion).

9
  • Spoiler Alert: Stack Overflow is for questions. Jan 4, 2011 at 21:20
  • Nathan -yup, appreciated. really just wanted to add this little OT nugget to the mix. tail between legs - ouch/touche etc :) !!
    – jim tollan
    Jan 4, 2011 at 21:24
  • Oh look someone's plugging my little pet project :D
    – zowens
    Jan 4, 2011 at 21:28
  • @zowens - yes, i couldn't find anywhere else on here to 'plug' it (short of asking a question and saying -'maybe this might work'??). it's fantastic. i really appreciate your efforts on that one - kudos
    – jim tollan
    Jan 4, 2011 at 21:29
  • @jim I do have a suggestion to form this into a question... Edit the question to ask why the 2nd code example is misleading and actually won't produce what the example leads on (just realized I screwed up the code on my blog... oops)
    – zowens
    Jan 4, 2011 at 21:38

1 Answer 1

6

So the question is why the second code example won't work as expected. Here's the answer, post currently doesn't return anything. This is an example of a certain developer not looking at the details of the code. When you use homePageUrl, the value will be undefined.

To actually get the home page URL, you'd do the following:

$.routeManager.action({controller:'Home', action:'Index'}).toUrl()

So, the moral of the story is that the code is a bit broken. The post action SHOULD return an object where you can put "toUrl()" right after the post is performed, like this:

$.routeManager.action({controller:'Home', action:'Index'})
              .post(function(data){ alert(data); })
              .toUrl();

I'll be fixing this bug in a bit!

3
  • 2
    +1 - for a humble response ;). plus a wee tick as it's the answer - yeah... thanks!!
    – jim tollan
    Jan 5, 2011 at 7:57
  • 1
    It's fixed now... I can't believe I put that in my blog post. I need to be a bit more careful!
    – zowens
    Jan 5, 2011 at 17:37
  • 1
    zack - even the 'best' of us can fall on our swords :-). will check out the update later and thanks again.
    – jim tollan
    Jan 7, 2011 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.