Generating a form dynamically in JQuery - javascript

I have my form information in a JSON object(Custom Format). Now i have to populate the From and display in predefined placeholder. I came across this post http://neyeon.com/2011/01/creating-forms-with-json-and-jquery/, which is very helpful.
But the problem is, i have to parse my JSON and then i have to create the new JSON in the required format so that the form will be created. Is this the right way to do it? or is there any other options available for me to do this?

If the author's code expects a certain structure for it's API and it differs from what you're creating, then yes, you'll have to translate. There's no "JSON Form" standard here to really dictate whether you should format your data a certain way.
If there's a well known, popular, jQuery form plugin (I've never heard of this one), it might make sense for you to simply format your data accordingly from the get go. OTOH, you might have better ideas and specialized needs anyway so that might not make sense either.
Either way, it shouldn't be too much work. Just write up a neat conversion function so that you can do your translation consistently.

Related

Is there any way to do a non-Ajax form submission with pure JSON data, with jQuery?

I was able to find this old question, which offered a consolation prize that I may end up using: if you want to feed JSON to the server on a POST whole page update, you are able to assign the JSON content to a (perhaps hidden) input.
However, I wanted to check in as far as reality checks go. jQuery has, as a basic and straightforward feature to offer, the ability to call jQuery.ajax() with, as one major option, a JSON string sent as the query string. And it's almost a non-option to fail to provide this functionality; people have come to expect to be able to provide pure-JSON data either way including contexts unrelated to the web and between two non-JavaScript endpoints.
Is there a non-workaround way now to do a page submit with the query string being neither more nor less than whatever JSON string encodes what the developer is sending?

Parse different sources differently

Let's say I wanted to parse information from different radio stations websites (the songs that were just played) and store them in a database. The websites differ (obviously), so I need to parse them differently. My way to do that is to create a super class "RadioStation" with the common functions and derive subclasses for each website in which I define the special parse function. However I don't think that's the right way to go because i would have to write 100+ subclasses. What is the correct solution here?
Thank you!
You could try and write an intelligent parser or you could write the 100+ subclasses, there is no simple solution to trying to parse data from different sources in different formats.
Though I would not be surprised if webradios would provide data in some kind of standard format (SOAP, XML, something...) as I suppose there are already quite a few applications that use it.

What is the best way to append data from AJAX?

At the moment i am using JSON as a result of AJAX and dynamically create all new DOM elements which i need. But here I was thinking - is it right? For example if i need to create a <table> of data is is better to build it with JS or generate it on the server and just append result to the page?
UPDATE
Now i do like this:
jQuery.getJSON(url, function(result){var table = jQuery('<table/>').append([jQuery('<td/>.....
But also can simply like this:
jQuery.get(URL, function(result){jQuery('body').append(result);}.......
there is really no one answer to your question - it all depends on your software type, expected loads, etc.
clearly, moving "rendering" to client side, would reduce the amount of code transmitted between server and client and also less server resources would be used on generate html code (clearly, we are talking about miniature difference). However, it might become really important, if you are talking about thousands of online visitors.
But if you were having thousands of visitors, you would not have asked such question :) So, I guess, you should format all stuff on Server side and void bothering about this question.
It depends on the size of the result. With small results it would make no difference.
But if you are sending a lot of rows for a table a JSON response would be much more efficient than the actual table html code.
Using a template engine like #Arun P Johny said in his comment is a good way to process a JSON result set.
Check underscore templates http://underscorejs.org/#template
There is no right or wrong. It all depends on what suits you best. I personally find JSON to be a good all-purpose format. What you should never do is return the data as HTML to be appended. Try to keep your data free of any structuring that belongs to the presentation layer.
For more information, have a read on Model-View-Controller

Namespaces in JSON

Is there such a thing as JSON namespaces, just like XML namespaces? Has anyone created a spec or libraries for this? Is this a good or a terrible idea?
I want to make a data spec that can be represented in XML as well as JSON. However I also need the namespace concept, that the data can be extended by annotations in different vocabularies.
To be more specific, this is about representing events. My schema will describe the event in basic terms (time and location), though if you think about it, events can be annotated with different information e.g. attendees or image URLs which I don't want to specify in my schema.
JSON-LD might help :
"JSON-LD (JavaScript Object Notation for Linking Data) is a lightweight Linked Data format that gives your data context."
JSON Schema might be the right thing for this:
http://json-schema.org/
Althought I don't know how well it's implemented.
This is quite an old thread, but there are JSON prefixes, which are almost like namespaces. If you are using Java server-side with Jettison, you can easily meet them.

How to Obtain Data to Pre-Populate Forms

The objective is to have a form reflect user's defined constraints on a search.
At first, I relied entirely upon server-side scripting to achieve this; recently I tried to shift the functionality to JavaScript.
On the server side, the search parameters are stored in a ColdFusion struct which makes it particularly convenient to have the data JSON'ed and sent to the client.
Then it's just a matter of separately iterating over 'checkable' and text fields to reflect the user's search parameters; jQuery proved to be exceptionally effective in simplifying the workload.
One observable difference lies in performance. The second method appeared to be somewhat slower and didn't work in IE8.
Evidently, the returned JSON'ed struct was seen as an empty object. I'm sure it can be fixed, though before spending any more time with it, I'm curious to hear how others would approach the task. I'd gladly appreciate any suggestions.
--Stan
Why would you want to do this with JavaScript, if you already have a server-side solution that works with all browsers?
I'm curious to hear how others would approach the task.
I would just do it on the server.

Categories

Resources