views:

50

answers:

3

In all my years of .NET programming I have not run across a bug as weird as this one. I discovered the problem because some elements on the page were getting double-bound by jQuery. After some (ridiculous) debugging, I finally discovered that once the view is completely done rendering itself and all its children partial views, it goes back to an arbitrary yet consistent location and re-renders itself.

I have been pulling my hair out about this for two days now and I simply cannot get it to render itself only once!

For lack of any better debugging idea, I've painstakingly added logging tracers throughout the HTML just so I can pin down what may be causing this. For instance, this code ($log just logs to the console):

...
<script type="text/javascript">var x = 0; $log('1');</script>
<div id="new-ad-form">
<script type="text/javascript">x++;$log('1.5', x);</script>
...

will yield

... <--- this happens before this snippet
1  
1.5 1 
...
10 <--- bottom of my form, after snippet
1.5 2 <--- beginning of part that runs again!
...
9 <--- this happens after this snippet

I've searched my codebase high and low, but there is NOTHING that says that it should re-render part of a page. I'm wondering if the jQueryUI has anything to do with it, as #new-ad-form is the container for a jQueryUI dialog box.

If this is potentially the case, here's my init code for that:

$('#new-ad-form').dialog({

autoOpen: false, modal: true, width: 470, title: 'Create A New Ad', position: ['center', 35], close: AdEditor.reset });

+1  A: 

Check the JavaScript to insure no requests are accidentally being made back to the same controller. Use Firebug or Fiddler to watch the HTTP requests.

Jason Rowe
so in firebug, there are no requests back to the controller, but after the page loads, the JS files for the two partial views are requested again via GET (after being loaded already by being referenced in the partial view)
Jason
Oh, are the JS files included twice in two different places?
Jason Rowe
no, i've searched the entire project. they're only referenced in one spot... :\
Jason
Wow, tough one. Maybe start removing things until it stops. Also, try posting the view code in your question as people suggested. Maybe someone will notice something.
Jason Rowe
found the culprit, but don't know how to solve the bug... apparently the jQuery `.dialog()` is the offending code. After the page runs, the div assigned to the dialog function runs itself again. I'd really like to keep the dialog, but this just sucks.
Jason
A: 

I may be Waaaay off base here but have you wrapped your script within $(document).ready()?

griegs
haha... yeah of course. see my comment on jason rowe's answer.
Jason
+2  A: 

As I suspected in my original question, the issue is with jQueryUI's dialog() method. After the page renders, the element assigned as the dialog box gets re-rendered, meaning any Javascript contained or loaded in that element is re-run. In my case I had two partial views that had their own JS files.

I wrote the ugliest hack to get around it, but I got it working by creating a global variable and checking it before calling anything in the $(function()). I died a bit writing that hack, but heck, now we can ship!

Thanks for anyone who tried to help!

Jason