Comments (6)
You can't.
It would be an interesting exercise to write a function that can deserialize back into the form from the returned object... but I think you are trying to do something that is out of scope for this plugin.
I think you need something like a template binding. Maybe you should look into something like Rivets.js ?
from jquery.serializejson.
No. When you created entity with your plugin, the next thing you must think is how to edit your new entity.
Yes you can use plain methods such as server-side script to do this , but it's amazing to do this all in your plugin.
So I write a plugin below to do this, hope this helpful and can be included into your codes.
(function($){
$.fn.deserializeJSON = function(s){
$(this).find("input, select, textarea").each(function(){
var o = s;
var match, arrays;
var name = this.name;
if (match = name.match(/^(.*):([^:]+)$/)){
name = match[1]
}
var names = []
if(name.indexOf("[") > -1){
names.push(name.substring(0, name.indexOf("[")));
if(match=name.match(/\[([^\]]+)\]/g)){
for(var i=0;i<match.length;i++){
names.push(match[i].substring(1, match[i].length-1));
}
}
}else{
names.push(name);
}
for(var i=0;i<names.length;i++){
o = o[names[i]];
if(o == null) return;
}
if(names.length>0 && o!=null){
if($(this).is("[type=checkbox]")){
if($(this).attr("data-unchecked-value")){
if(o.toString() == $(this).attr("data-unchecked-value")){
if($(this).is(":checked")){
$(this).click();
}
}else{
if(!$(this).is(":checked")){
$(this).click();
}
}
}
}else{
$(this).val(o);
}
}
});
};
})(jQuery);
from jquery.serializejson.
This is a good start, but in order to fully support deserialization, it should accept all the same options as when serialized (i.e. checkboxUncheckedValue
or useIntKeysAsArrayIndex
change the format of the serialized object), and also take into account the ":type" of each input if any. And what if the serialization was made with a custom parseWithFunction
function? The user would have to create a reverse-function as well ...
I would also require a good test coverage to accept this in the plugin codebase... if you think about it, the solution is actually pretty complicated, since this plugin can serialize in many different ways.
For that reason, it's usually better to just write your own code to de-serialize, or just manually put back the values into the form with jQuery:
// Set the values of the object returned by serializeJSON back into the form
function deserializeJSON(obj, $form) {
$form.find("input[name='my[nested][value]']").val(obj['my']['nested']['value']);
$form.find("input[name='my[other][value]']").val(obj['my']['other']['value']);
}
// Serialize
var $form = $('form');
var obj = $form.serializeJSON();
obj.my.nested.value = "newstuff"; // modify your object
deserializeJSON(obj, $form); // set back into the HTML form
But again, if what you want is a full 2-way binding, Rivets.js is a great solution. Take a look at this example: http://jsfiddle.net/nsisodiya/y3gDP/
from jquery.serializejson.
I agree with giter that it would be great to be able to deserialize the object.
Personally I am not at all looking for something like template-binding (as in Rivet.js), but being able to deserialize seems quite useful.
from jquery.serializejson.
@marioizquierdo Yes this is an early handy source, I may continue to complete this deserialize plugin when I finish current job ~ 2015.05.
Major reason that I need this deserialize plugin is that your plugin break the nature HTML elements' name attribute and add some suffix on name like "weight:number", maybe you can use some other friendly method like provide some conversion class such as "number","datetime", etc.
from jquery.serializejson.
I still think that this type of functionality should be implemented in a separate repository. Maybe named jquery.deserializeJSON
?
Setting the values back into the form is something that most users don't need, and it would break the promise of keeping this plugin light. And it's not easy at all to support all the possible options and types.
Closing the issue for now.
from jquery.serializejson.
Related Issues (20)
- checkboxUncheckedValue not working for me HOT 1
- Option for include parsed 0 values HOT 1
- Raise ERROR: Invalid type in value HOT 5
- Mixup between colon and semicolon HOT 2
- .serializeJSON returns undefined HOT 4
- Provide dom element into custom type function HOT 5
- Is there something wrong about array? HOT 1
- Make an es6 module? HOT 1
- Ability to use attributes other than name for building the json HOT 3
- Consider passing element name to custom type function. HOT 4
- Versioning scheme? HOT 1
- Empty SELECT multiple HOT 3
- AMAZING, THANK YOU
- It doesn't serialize type=file HOT 3
- Initialize form values from object HOT 3
- Radio Buttons are not working in >3.2.0 HOT 2
- Doesn't work with ASP.NET MVC naming conventions HOT 5
- name多层级数组无效
- checkboxCheckedValue option HOT 1
- Disabled elements are ignored? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery.serializejson.