Last active
December 17, 2015 02:59
-
-
Save AlexShkor/5539994 to your computer and use it in GitHub Desktop.
Parsing KnockoutJS model into flat key-value form data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parseValues = function (data, callback, prefix, postfix) { | |
postfix = postfix || ""; | |
prefix = prefix || ""; | |
for (var key in data) { | |
if (data[key] == null) { | |
continue; | |
} | |
if ( Object.prototype.toString.call( data[key] ) === '[object Array]') { | |
parseValues(data[key], callback, prefix + key + postfix + "[", "]"); | |
} else if (typeof data[key] == "object") { | |
parseValues(data[key], callback, prefix + key + postfix + "."); | |
} else { | |
callback(prefix + key, data[key]); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.utils.submitForm = function (urlOrForm, data, options) { | |
options = options || {}; | |
var params = options['params'] || {}; | |
var includeFields = options['includeFields'] || this.fieldsIncludedWithJsonPost; | |
var url = urlOrForm; | |
// If we were given a form, use its 'action' URL and pick out any requested field values | |
if ((typeof urlOrForm == 'object') && (ko.utils.tagNameLower(urlOrForm) === "form")) { | |
var originalForm = urlOrForm; | |
if (originalForm && originalForm.action) | |
url = originalForm.action; | |
else { | |
url = window.location.href; | |
} | |
for (var i = includeFields.length - 1; i >= 0; i--) { | |
var fields = ko.utils.getFormFields(originalForm, includeFields[i]); | |
for (var j = fields.length - 1; j >= 0; j--) | |
params[fields[j].name] = fields[j].value; | |
} | |
} | |
data = ko.utils.unwrapObservable(data); | |
var form = document.createElement("form"); | |
form.style.display = "none"; | |
form.action = url; | |
form.method = options["method"] || "post"; | |
parseValues(data, function(key,value){ | |
var input = document.createElement("input"); | |
input.name = key; | |
input.value = value; | |
form.appendChild(input); | |
}); | |
for (var key in params) { | |
var input = document.createElement("input"); | |
input.name = key; | |
input.value = params[key]; | |
form.appendChild(input); | |
} | |
document.body.appendChild(form); | |
options['submitter'] ? options['submitter'](form) : form.submit(); | |
setTimeout(function () { form.parentNode.removeChild(form); }, 0); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.utils.buildQueryUrl = function (data, excludeKeysList, includeDefaultValues) { | |
var query = ""; | |
parseValues(data, function (key, value) { | |
if ((value || includeDefaultValues) && !isKeyExcluded(key, excludeKeysList)) { | |
query += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&"; | |
} | |
}); | |
return query; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Coverting from this
to this