Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you say you want to save the data to <code>localStorage</code>, but also that you want to send modified data to the server. </p> <p>I would suggest that you divide this problem up into (Part 1) learning how to save locally to <code>localStorage</code> and rendering that content with templating and then (Part 2) learning how to store on a server. I can help you with Part 1, since quite frankly I'm still learning about Part 2 myself.</p> <p>Okay so, two subtasks:</p> <ol> <li>using localStorage to persist data</li> <li>using jQuery templates to render data</li> </ol> <hr> <p><strong>Using localStorage</strong></p> <p>You haven't specified where your data is coming from, so I'll assume you have some JSON. For simplicity I'll just use this data:</p> <p>(You might be wondering why I added content that isn't plain ASCII -- it's just a habit of mine, I believe in testing with realistic text from the get-go. When we finally render this data, it should look right in your browser.)</p> <pre><code>var philosophers = [ { "phone": "1-800-123-1937", "name": "H\u00e9l\u00e8ne Cixous", "email": "helene@stanford.edu" }, { "phone": "1-800-000-0000", "name": "\u041c\u0438\u0445\u0430\u0438\u0301\u043b \u0411\u0430\u043a\u0443\u0301\u043d\u0438\u043d", "email": "mikhail@ispitondns.com" }, { "phone": "1-800-770-0830", "name": "Jayar\u0101\u015bi Bha\u1e6d\u1e6da", "email": "jay@ancientindia.edu" } ] </code></pre> <p>So we need to get this into <code>localStorage</code>, just to have some data to start with. </p> <p>The trick about <code>localStorage</code> is that you can't just directly store JSON objects. You can only store strings. There are <a href="https://github.com/whitmer/store.js" rel="noreferrer">some libraries out there</a> designed to improve on this situation, but we'll just convert our objects ourselves. To do that we'll use JSON:</p> <pre><code>localStorage.philosophers = JSON.stringify(philosophers) </code></pre> <p>Unsurprisingly, <code>JSON.stringify</code> turns JSON objects into a string, and that can be set directly as an "attribute" of <code>localStorage</code>. </p> <p>(If you're using an old browser, then you might not have the native JSON object -- there's a <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js" rel="noreferrer">library you can include for that too</a>.)</p> <p>Okay, so now we have some contact data stashed in <code>localStorage</code> with the label of <code>philosophers</code>. (Hey, you never know when you might need to call a philosopher!)</p> <p>To get that data back out and into a Javascript object we can do something with, we use another JSON method, <code>JSON.parse</code>.</p> <pre><code>philosophers = JSON.parse(localStorage.philosophers) </code></pre> <p>This is all pretty artificial, since we've got the <code>philosophers</code> data in the first place, then we stringify it, and then we store it, and then we take it right back out, and then we parse it, and then we're back where we started. But in reality such data will come from some other source -- perhaps an external file or a web service or whatever.</p> <hr> <p><strong>Using templates to render objects</strong></p> <p>Since you used what looks like jQuery template syntax in your template, I'm going to assume that's the library you're using. The jQuery docs show us how we can render a variable containing some objects (like what we have in our <code>philosophers</code> variable) with a template, here's the key bit of those docs:</p> <pre><code>// Convert the markup string into a named template $.template( "summaryTemplate", "&lt;li&gt;${Name}&lt;/li&gt;" ); function renderList() { // Render the movies data using the named template: "summaryTemplate" $.tmpl( "summaryTemplate", movies ).appendTo( "#moviesList" ); } </code></pre> <p>Here's one way you can get your template to work (there are other, arguably cleaner methods, but jQuery templates are a topic unto themselves):</p> <pre><code>var myTemplate = "&lt;li&gt;Name: ${name}&lt;br/&gt;Phone: ${phone}&lt;br/&gt;Email: ${email}&lt;/li&gt;"; $.template("contactLi", myTemplate); </code></pre> <p>That creates a template and stores it in a <em>variable</em> named <code>contentLi</code>. (Note that <code>$.template</code> wants that given variable name given as a string, which strikes me as weird. I find the way jQuery templates names and defines these methods confusing, which is one of the reasons I prefer Mustache for templating. Shrug.) </p> <p>Also, note that we don't have the <code>ul</code> included in the template, because that's not going to be repeated for each rendered object. Rather, we're going to add the <code>ul</code> as a hook in the markup, and render the assembled template repeatedly as a child of that. Which just takes a single line with jQuery templates, rather nice:</p> <pre><code>$.tmpl( "contactLi", philosophers ).appendTo( "#guests" ); </code></pre> <p>So there you go, a rendered list. </p> <p>I know this doesn't answer your whole question but there's a lot here to start with. </p> <p>Here's an <a href="http://jsfiddle.net/TKSeU/" rel="noreferrer">example you can try out</a>, it ends up rendering something like:</p> <pre><code> Name: Hélène Cixous Phone: 1-800-123-1937 Email: helene@stanford.edu Name: Михаи́л Баку́нин Phone: 1-800-000-0000 Email: mikhail@ispitondns.com Name: Jayarāśi Bhaṭṭa Phone: 1-800-770-0830 Email: jay@ancientindia.edu </code></pre> <p>(Hehe, boy, SO's syntax highlighting doesn't handle that Unicode text very well!) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload