Пример #1
0
        /// <summary>
        /// Grabs all the values and options specified and creates the proper script to ouput to the page. If you called ton OnReady it outputs the script
        /// within a jQuery on document ready function, if you flagged it to AddScriptTag it sorrounds the whole script with the propery <script></script> tag
        /// </summary>
        /// <returns>Properly formatted javascript to include in the page</returns>
        public override string ToString()
        {
            var options = new JSObject();
            options.AddString("url", _url);
            options.AddString("type", _type);
            options.Merge(_otherOptions);
            if (!string.IsNullOrEmpty(_dataType))
            {
                options.AddString("dataType", _dataType);
            }
            if (!string.IsNullOrEmpty(_target))
            {
                options.AddString("target", _target);
                if (_replaceTarget)
                {
                    options.Add("replaceTarget", "true");
                }
            }

            // If the user does not specify a validation function, then assign a default
            if (string.IsNullOrEmpty(_validateFunction))
                _validateFunction = "jQuery('#" + _formName + "').validate().form()";

            if (!string.IsNullOrEmpty(_beforeSerialize) & _validate == false)
            {
                options.Add("beforeSerialize", _beforeSerialize);
            }
            else if (!string.IsNullOrEmpty(_beforeSerialize) & _validate)
            {
                options.Add("beforeSerialize", "function(){ " + _beforeSerialize + "(); if (!" + _validateFunction + ") return false; }");
            }
            else if (_validate)
            {
                options.Add("beforeSerialize", "function() { if (!" + _validateFunction + ") return false; }");
            }
            if (!string.IsNullOrEmpty(_beforeSubmit))
            {
                options.Add("beforeSubmit", _beforeSubmit);
            }
            if (!string.IsNullOrEmpty(_success) || options.HasMember("error"))
            {
                // Need to check that the response is actually a success before executing it
                var ssb = new StringBuilder();
                ssb.Append("function(responseText, statusText, xhr, $form) { ");

                // Sometimes the response is 0 or undefined but there is not an error in the response 
                //   (this happens sometimes when uploading a form with a file input)
                ssb.Append("    var undefinedWithError = false; ");
                ssb.Append("    try {");
                ssb.Append("         if ((xhr.status == undefined || xhr.status == 0) && responseText.indexOf('<span><h1>Server Error in') != -1) { ");
                ssb.Append("              undefinedWithError = true;");
                ssb.Append("         }");
                ssb.Append("    } catch(e){log('###ERROR: ' + e);}");

                //ssb.Append("    alert('statusIs0OrUndefined: ' + (xhr.status == undefined || xhr.status == 0));")
                //ssb.Append("    alert('statusText: ' + statusText + ' \n status: ' + xhr.status + '\n undefinedWithError: ' + undefinedWithError + '\n\n' + responseText);")
                //ssb.Append("if (undefinedWithError) alert('***ERROR***: StatusText: ' + statusText + ' status: ' + xhr.status);")

                // Trigger an error handler if any
                if (options.HasMember("error"))
                    ssb.Append("if (undefinedWithError)(" + options.GetValue("error") + ")(responseText, statusText, xhr, $form);");

                // Call success only after checking that there were no undefined with errors
                if (!string.IsNullOrEmpty(_success))
                    ssb.Append("if (!undefinedWithError) (" + _success + ")(responseText, statusText, xhr, $form);");

                ssb.Append("}");
                options.Add("success", ssb.ToString());
            }

            if (_resetForm)
            {
                options.Add("resetForm", "true");
            }
            if (_clearForm)
            {
                options.Add("clearForm", "true");
            }

            var script = "";

            // Add the script tag if requested
            if (_wrapScriptTag)
                script += "<script type='text/javascript'>";
            // Add the jquery on ready tag
            if (_wrapOnReady)
                script += "jQuery(function(){";
            script += "jQuery('#" + _formName + "').ajaxForm(" + options + ");";
            // Close the jquery on ready tag
            if (_wrapOnReady)
                script += "});";
            // Close the script onready tag
            if (_wrapScriptTag)
                script += "</script>";

            return script;
        }