开发者

Trying to get Telerik ScriptRegistrar to load Telerik scripts and custom script

I am using the latest version of Telerik MVC extensions with my ASP.NET MVC 3 razor application. I have also downloaded the latest version of jQuery.

I have my jQuery file in a different directory as to what comes default with Visual Studio. The reason for this is because I have downloaded the latest version of jQuery jquery-1.6.2.min.js. I have my Telerik MVC content and scripts directories in a different directory. As to what I have seen the Telerik scripts needs the jQuery file to be loaded first. I have deleted the content and scripts directories that come by default with an ASP.NET MVC application.

Telerik scripts directory:

~/Assets/telerikaspnetmvc/2011.2.712/Scripts/

My jQuery directory:

~/Assets/JavaScripts/jQuery/

I changed the ScriptRegistrar to that of below:

@(Html.Telerik().ScriptRegistrar()
   .DefaultGroup(group => group
      .DefaultPath("~/Assets/telerikaspnetmvc/2011.2.712/Scripts/")
      .Compress(true)
   )
   .Scripts(scripts => scripts
      .AddGroup("JavaScriptAssetLocation", group => group
         .DefaultPath("~/Assets/JavaScripts/jQuery/")
         .Add("jquery-1.6.2.min.js")
         .Compress(true)
      )
   )
   .jQuery(false)
)

This gave me an error. I swapped the 2 around so that it can read jQuery first as below but it still seems to load the default Telerik JavaScript files first and results in errors. This is the changed code:

@(Html.Telerik().ScriptRegistrar()
   .Scripts(scripts => scripts
      .AddGroup("JavaScriptAssetLocation", group => group
         .DefaultPath("~/Assets/JavaScripts/jQuery/")
         .Add("jquery-1.6.2.min.js")
         .Compress(true)
      )
   )
   .DefaultGroup(group => group
      .DefaultPath("~/Assets/telerikaspnetmvc/2011.2.712/Scripts/")
      .Compress(true)
   )
   .jQuery(false)
)

What am I doing wrong here? Please provide code samples as to how it m开发者_如何学运维ust be done.

Just another question. Do I need to specify Compress(true) per group or is once enough? And jQuery(false) needs to be specified where? Only after DefaultGroup? Or anywhere?


I think the easiest thing to do here would be the following:

@(Html.Telerik().ScriptRegistrar().DefaultGroup(group => group
  .DefaultPath("~/Assets/telerikaspnetmvc/2011.2.712/Scripts/").Combined(true)
  .Compress(true)).jQuery(false))

Sorry if this ends up looking a bit compressed - the code snippet style refused to be applied with any other setting. Anyways, once you have done that register the following in the head section of your page:

<script src="@Url.Content("~/Assets/JavaScripts/jQuery/jquery-1.6.2.min.js")" type="text/javascript"></script>

This will link properly to your own jQuery version while ensuring that the default group's DefaultPath is changed.

As far as using compression here I don't think it would do much. This is really only good for when you use .Combined(true) in a group that has several scripts that are being added (the default group for the Telerik components for example). If you're just using a single file (jquery-1.6.2.min.js) there's really no need. So, just make sure you use .Combined(true) (I had to add it above) whenever you want to compress a combined group. This would need to be added for every group.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜