Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you structure your web applications to make AJAX and DHTML easier?
    primarykey
    data
    text
    <p>Given this structure for a websinte</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;!-- CSS at the beginning--&gt; &lt;link/&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="mainContainer"&gt; &lt;div id="side"&gt;&lt;/div&gt; &lt;div id="content"&gt; &lt;!-- DHTML awesomeness happens here --&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- Scripts at the end --&gt; &lt;script/&gt; &lt;script&gt; /* code that attach the JS logic to the HTML items */ &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Using normal web navigation, the page renders totally in HTML, and following the progressive enhancement approach, at the end I look for some specific ids or classes, and I give them dynamic behavior using javascript and specially jQuery. This enhancement code happens at the very end of the body, after the external scripts has been downloaded.</p> <p>In <code>#content</code>, lot of jQuery AJAX interactions happens, some of them get other partial views from the server and insert them in the page, but then I have to look for those ids and classes again and attach javascript objects to this new elements.</p> <p>It could be very cumbersome, since you don't want to reapply controllers, event handlers or whatever to objects that already have them.</p> <p>So far, the only solution that I found is put in my partial views:</p> <pre><code>@if(Request.IsAjaxRequest()) { &lt;script&gt; /* code that attach the JS controllers to the HTML items of this view */ &lt;/script&gt; } </code></pre> <p>I think that a similar problem happens for example when you want <code>$('input.date').datepicker()</code> , and you add new <code>&lt;input type="text" class="date"/&gt;</code> elements dynamically, the new ones have no date picker unless you rexecute the jQuery sentence.</p> <p>For example, considering that in <code>#content</code> I have an <code>&lt;input type="text" class="date"/&gt;</code>: </p> <ol> <li><p>In order to make the jQuery datepicer work the first time, I have to call <code>$('input.date').datepicker()</code> at the end of the <code>&lt;body&gt;</code>, after the external <code>&lt;script&gt;</code> declarations. </p></li> <li><p>If the page download partial views where are new <code>&lt;input type="text" class="date"/&gt;</code> elements, I have to put the initialization call in the view for ajax calls.</p></li> </ol> <p>So I end with repeated code, something that I don't want specially in JS where I cannot refactor the code as easily as in C#.</p> <p>This is something that is driving me nuts in the last week, wondering if there is a better way to accomplish this? A better technique, or other whole approach?</p> <p>How do you structure your web applications?</p> <p>Kind regards.</p> <p>PS: Would be nice to have something like <code>.live()</code> or <code>.delegate()</code> but not only related with events, wouldn't it? Is not there any event that jQuery/browser raises everytime that something is added to the DOM?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload