开发者

How can I populate a jQuery template (tmpl) using HTML5 Local Storage data

开发者 https://www.devze.com 2023-03-01 13:38 出处:网络
I am trying to build a simple jQuery UI template and populate it with data stored in the localStorage.

I am trying to build a simple jQuery UI template and populate it with data stored in the localStorage.

I need to set the local storage with a list of guests and have the user edit the list. When clicking update, the changes are sent back to the server.

<ul id="guests">
 <li>
  Name: ${name} <br />
  Phone: ${phone} <br />
  Email: ${email}
 </li>
</ul>
开发者_开发知识库

I am really new at this and have no idea what to do. I am just interested in setting the local storage when the page loads and populating the template.

Can someone please provide a short tutorial?

I thought this is a simple question... Can someone please let me know in case it is not possible at all? Thanks!


you say you want to save the data to localStorage, but also that you want to send modified data to the server.

I would suggest that you divide this problem up into (Part 1) learning how to save locally to localStorage 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.

Okay so, two subtasks:

  1. using localStorage to persist data
  2. using jQuery templates to render data

Using localStorage

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:

(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.)

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"
  }
]

So we need to get this into localStorage, just to have some data to start with.

The trick about localStorage is that you can't just directly store JSON objects. You can only store strings. There are some libraries out there designed to improve on this situation, but we'll just convert our objects ourselves. To do that we'll use JSON:

localStorage.philosophers = JSON.stringify(philosophers)

Unsurprisingly, JSON.stringify turns JSON objects into a string, and that can be set directly as an "attribute" of localStorage.

(If you're using an old browser, then you might not have the native JSON object -- there's a library you can include for that too.)

Okay, so now we have some contact data stashed in localStorage with the label of philosophers. (Hey, you never know when you might need to call a philosopher!)

To get that data back out and into a Javascript object we can do something with, we use another JSON method, JSON.parse.

philosophers = JSON.parse(localStorage.philosophers)

This is all pretty artificial, since we've got the philosophers 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.


Using templates to render objects

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 philosophers variable) with a template, here's the key bit of those docs:

// Convert the markup string into a named template
$.template( "summaryTemplate", "<li>${Name}</li>" );

function renderList() {
   // Render the movies data using the named template: "summaryTemplate"
   $.tmpl( "summaryTemplate", movies ).appendTo( "#moviesList" );
}

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):

var myTemplate = "<li>Name: ${name}<br/>Phone: ${phone}<br/>Email: ${email}</li>";
$.template("contactLi", myTemplate);

That creates a template and stores it in a variable named contentLi. (Note that $.template 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.)

Also, note that we don't have the ul included in the template, because that's not going to be repeated for each rendered object. Rather, we're going to add the ul 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:

$.tmpl( "contactLi", philosophers ).appendTo( "#guests" );

So there you go, a rendered list.

I know this doesn't answer your whole question but there's a lot here to start with.

Here's an example you can try out, it ends up rendering something like:

 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

(Hehe, boy, SO's syntax highlighting doesn't handle that Unicode text very well!)


try AmplifyJS -- can extract your data as json the same way you would as $.getJSON

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号