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. 开发者_如何学GoFor 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 });
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!
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.
I may be Waaaay off base here but have you wrapped your script within $(document).ready()?
精彩评论