Partial MVC views and unobtrusive jQuery / Javascript

I'm struggling to figure out how I should create my partial views that use jQuery.

For example, in a partial view below, I show a text box that connects to the jquery autocomplete plugin.

In the end, I want the results to appear next to them next to an “X”, which the user can delete, but for this example, the selected text is simply added to the div div.

Is my jQuery OK code sitting here in a partial view, or should I put it in an external file? If an external file is used, another developer using a partial view should know that the external js file should be included - is this normal?

I suppose my question is actually that although I'm kind of OK with jQuery (finding elements in the DOM, calling asynchronously, etc.), I need some guidance on where to put all this code That will stop me in a mess.

Does what I just printed make sense ????

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script language="javascript" type="text/javascript">
$(document).ready(function() {

    $(".acEmployee").autocomplete({
        source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"],
        select: function(event, ui) {

            $(this).siblings().append(ui.item.value);
        }
    });

});

</script>
<div><div></div><input class="acEmployee" /></div>
+3
source share
2 answers

If JavaScript is a "connection code", then I do not mind that it be partial - after all, in the end, everything that applies to it is.

If JavaScript is a "framework" of code and can be reused everywhere and does not depend on anything in partial, I put it in my own file.

This is what I do, but I would be interested to hear other methods of people.

0

, js- js . , jquery .

.net mvc - :

       <!--try fetching jquery from CDN, if CDN is down, fallback to local-->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='<%:Url.Content("~/Scripts/jquery-1.5.1.min.js")%>' type='text/javascript'%3E%3C/script%3E"));
}
    </script>
    <script type="text/javascript" language="javascript">
    var MVC_baseurl = "<%:Url.Content("~/") %>";
    </script>
    <script type="text/javascript" language="javascript" src="<%:Url.Content("~/Scripts/jquery-ui-1.8.10.custom.min.js")%>"></script>
    <script type="text/javascript" language="javascript" src="<%:Url.Content("~/Scripts/plugins.js")%>"></script>
    <asp:ContentPlaceHolder ID="BottomJavascript" runat="server">

    </asp:ContentPlaceHolder>

., , , plugins.js,

var path = MVC_baseurl + "Controller/Action"

ajax ..

0

All Articles