/// <summary> /// Return a javascript block containing the contents of <paramref name="script" /> /// if <paramref name="condition" /> is true /// with an optional execution deferment. /// </summary> /// <param name="helper">The HTML helper instance that this method extends.</param> /// <param name="condition">The condition that must be met to display the script block.</param> /// <param name="script">The script to write to the script block.</param> /// <param name="defer">Whether or not to defer script execution until after page load completion.</param> /// <returns></returns> public static MvcHtmlString JSBlock(this HtmlHelper helper, bool condition, string script, bool defer) { if (condition) { return(MvcHtmlString.Create(JavaScriptHelper.BuildJavaScriptFragment(null, script, defer))); } else { return(MvcHtmlString.Empty); } }
/// <summary> /// Returns an MVC string with all the required JS script block fragments. /// Clears the requires list to prevent duplicate emission. /// </summary> /// <returns></returns> public static MvcHtmlString WriteRequiredJsFragments(this HtmlHelper helper) { var requires = GetRequires <JsFragmentResource>(KEY_JSFRAGMENTS); if (requires == null) { return(MvcHtmlString.Empty); } var result = new StringBuilder(); foreach (var fragment in requires) { result.AppendLine(JavaScriptHelper.BuildJavaScriptFragment(null, fragment.JsFragment, fragment.Defer, fragment.Type)); } requires.Clear(); return(MvcHtmlString.Create(result.ToString())); }