Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>My feeling is that backbone-localstorage was just a quick implementation that the author used for demo purposes.</p> </blockquote> <p>Exactly right. I'd say that the implementation is fine for most things, but what support you can get for it is probably minimal.</p> <blockquote> <p>I also noticed that the keys are randomly generated, whereas I would like something that would allow me to name my own key values. </p> </blockquote> <p>This isn't really correct. I assume you are referring to this:</p> <pre><code>// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already // have an id of it's own. create: function(model) { if (!model.id) model.id = model.attributes.id = guid(); this.data[model.id] = model; this.save(); return model; } </code></pre> <p>Here, what's happening is that when a create is called on a model, it tries to determine if an <code>id</code> has been set on the model, and if no such <code>id</code> is provided, then the <code>guid</code> function is used to build one. This isn't setting a key randomly, it's fulfilling the requirement that every saved model should have an id (by assigining a random one).</p> <p>Modifying the <code>backbone-localstorage.js</code> code should be fairly trivial. Let's look at the store constructor as an example</p> <pre><code>Before: var Store = function(name) { this.name = name; var store = localStorage.getItem(this.name); this.data = (store &amp;&amp; JSON.parse(store)) || {}; }; </code></pre> <p>The only thing we need to update here is the call to localStorage:</p> <pre><code>After: var Store = function(name) { this.name = name; //Notice here, that jStorage gives us the option to define a default. var store = $.jStorage.get(this.name, {}); this.data = store; }; </code></pre> <p>Similarly simple is the save function:</p> <pre><code>Before: save: function() { localStorage.setItem(this.name, JSON.stringify(this.data)); } </code></pre> <p>again, simply update the call to localStorage:</p> <pre><code>After: save: function() { $.jStorage.set(this.name, this.data); } </code></pre> <p><strong>Update</strong>: jStorage handles all the JSON.stringify, and JSON.parse for you, so I've updated the above code to reflect that.</p> <p>Easy peasy, right!</p> <p>Anyways, this is a great exercise, and I wish you the best of luck. I hope that I've been able to help you out.</p> <p>P.s. One thing that bothers me about the localStorage plugin is that it uses <code>model.attributes.xxx</code>, where it's better to use <code>model.get("xxx")</code>. This way, if they ever change the internals of model change, your code won't break.</p> <p>P.p.s. as far as how to generate guids, and weather a random guid is appropriate for you, depends upon your domain. With the TODO example, there is no backing database to sync with, so any old guid would do the trick, but in many other cases, you may be using html5 storage to create an offline version of your app, so id's need to be compatable with the db. In these cases, using a random guid is probably not that great an idea, but the one provided by the backbone-localstorage plugin is designed so that it isn't likely to collide with your db's actual id's, so it's not _<em>that bad</em>. There isn't really a right answer here, so do what makes sense for your domain.</p>
    singulars
    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