Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"toString" the value of an object #6

Closed
Mattes1000 opened this issue Oct 23, 2013 · 5 comments
Closed

"toString" the value of an object #6

Mattes1000 opened this issue Oct 23, 2013 · 5 comments
Assignees

Comments

@Mattes1000
Copy link

Is it possible to "render" the value of an object in a textarea?
Something like a toString

I made an example at http://jsfiddle.net/Mattes/AbwGC/

@corinis
Copy link
Owner

corinis commented Oct 23, 2013

I am not sure what you mean, in json ther eis nothing like "toString" or a value of an object.

if you have an object like
´´´
{
"field": "test"
"field2": true
}
´´´

then "what" would be the result of "toString" (if something like that exists).

In addition, "toString" is not reversible.

so if even if you would have something like toString and would write an object containing multiple fields into a textara (which can hold only strings) - what would be the reverse?

However, there is a "workaround" that I use form time to time.

For example i have an object like this:
´´´
{
"name": "tim"
"lastname": "taylor"
"fullname": null
}
´´´

and i want to display the "displayname" (which is either the fullname or the name + lastname), then i create a preprocess function (i use it mostly to display infos)

´´´
var obj = ABOVE;

if(obj.fullname && obj.fullname !== '') obj.displayname = obj.fullname;
else obj.displayname = obj.name + " " + obj.lastname;

$("#myform").jsForm("fill", obj);
´´´

this would a field that is filled
´´´

´´´

I hope this informatio helps

@Mattes1000
Copy link
Author

Sorry, my example is at http://jsfiddle.net/Mattes/AbwGC/1/

What I mean is JSON.stringify ... of course ..

I need a field where the user can insert "anything" (of course valid json)

....but I will try the "preprocessing"

Thanks

@corinis
Copy link
Owner

corinis commented Oct 23, 2013

Hmm.. now i see what you mean. Following suggestion:
I will change the code so if you specify class="object" it will put the subobject on the field. When filling, it will trigger a "fill" event which you can bind and use stringify to process anything you want.

When "get"ting the form, it will look for the bound object.

Let me update the script real quick and i can add a smaple for you

@ghost ghost assigned corinis Oct 23, 2013
@Mattes1000
Copy link
Author

You are fantastic! (I told you already 👍 )
I did a example at http://jsfiddle.net/Mattes/AbwGC/4/

@corinis
Copy link
Owner

corinis commented Oct 23, 2013

so.. in theory the last commit should do it. I made sure that the object is correctly bound on the input (if you specify class="object") -> it uses $(input).data("pojo"). it will also read from that when constructing the json for "get"!

the change event was already triggered, but i also added a "fill" event ( i will deprecate the change event in the future). I also commited your jsfiddle sample - and extended it a little to show writing as well.

Hope this works for you.

@corinis corinis closed this as completed Oct 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants