2011-12-08 26 views
0

在我的_MainLayout(parentlayout)中,我引用了幾個JS文件。Asp.Net MVC 3 - 用於PartialView的重複JS文件(Ajax)

Partialviews是使用以下的javascript/jquery的功能加入到一個div:

$.ajax(link, { 
      dataType: "html", 
      cache: false, 
      success: function(data, textStatus, jqXHR) { 
       return pageDownloaded(data, a); 
      }, 
      complete: function (jqXHR, textStatus) { 
      //add .on event 
      } 
     }); 

function pageDownloaded(data, anchor){ 
var link = anchor.attr("href").replace(/^\#/, ""), 
title = anchor.attr("title"), 
id = link.replace(/[\/\.]/,"-"), 
target = "#main-content", 
div = $('<div style="left: 100%" id="'+id+'">'+data+'</div>').appendTo($(target)); 
$("#wrapper > section > section > header h1").html(title); 

if ($('#wrapper > section > section').css('position')=='absolute') { 
    $("> div:last", target).css({left: 0, position: 'relative'}).siblings().remove(); 
    $('#wrapper > section > section').show().animate({left: 0}, 300, "easeInOutQuart",          function(){$(this).css('left',0);}); 
} else { 
/* $("> div", target).animate({left: "-=100%"}, "slow", "easeInOutQuart", function(){ 
     $(this).css('left',0); 
     $("> div:last", target).css({position: 'relative'}).siblings().remove(); 
    });*/ 
    $("> div", target).animate({ left: "-=100%" },500, function() { 
     $(this).css('left', 0); 
     $("> div:last", target).css({ position: 'relative' }).siblings().remove(); 
    }); 

} 

$(window).trigger('drilldown'); 
} 

問題是,如果我設置在一個JS文件通過我的母版 (加載默認動作被添加到每個頁面(dateinput,...))

(function($){ 
    $.fn.placeholder = function(options) { 
     return this.each(function() { 
     if (!("placeholder" in document.createElement(this.tagName.toLowerCase()))) { 
      var $this = $(this); 
      var placeholder = $this.attr('placeholder'); 
      $this.val(placeholder).data('color', $this.css('color')).css('color', '#aaa'); 
      $this 
      .focus(function(){ if ($.trim($this.val())===placeholder){  $this.val('').css('color', $this.data('color'))} }) 
      .blur(function(){ if (!$.trim($this.val())){  $this.val(placeholder).data('color', $this.css('color')).css('color', '#aaa'); } }); 
     } 
     }); 
    }; 
    })(jQuery); 

    $(document).ready(function() { 
     $('input[placeholder]', target).placeholder(); 

     $("input[type=date]", target).dateinput(); 

     $("input:checkbox,input:radio,select,input:file", target).uniform(); 

    }); 

我必須在PartialView中再次引用它。 我還必須再次參考jquery.min.js文件,...

我該如何解決這個重複問題?

回答

0

這是一個令人困惑和相當混亂的問題,也許這會幫助你理順你的代碼。

隨後,內容頁面可以加載應該包含ON_LOAD類型的活動或其他全局腳本引用。

  1. 負載MasterPage(腳本引用,引用CSS等)
  2. 負載Content(局部圖,動態數據,具體腳本等)
  3. 內容加載完畢?運行Content.Init()類型的函數。 (例如:MyView.GetData()
  4. 用戶互動開始。
+0

這是不可能的,因爲我使用的是hashcange jquery插件。這使我可以在文檔準備好的情況下加載partialview,並允許我的客人將url加入書籤並被重定向到正確的頁面:)。 其他建議? – NicoJuicy

相關問題