LocalStorageSource not actually saving collections?

I'm trying to use the LocalStorageSource to save a collection of messages. The messages are fairly simple models, basically a message body and a timestamp. I've registered the source:
new enyo.LocalStorageSource({name: "localstorage"})
And set the source and url in the collection:
name: "ConversationCollection",
kind: "enyo.Collection",
model: "MessageModel",
source: "localstorage",
// Because LocalStorageSource doesn't support dynamic getUrl()
constructor: enyo.inherit(function(sup) {
return function() {
sup.apply(this, arguments);
this.set('url', 'conversation:' + this.name);
The collection is created like so:
var collection = new ConversationCollection({name: "foo"});
I add a few records, then commit() it. When I fetch() it later, though, I get an empty collection. I can see that data is being saved in localStorage, but it's just a list of UUIDs.
>> localStorage['enyo-app-conversation:foo']
(Formatted for readability) I dug into the code, and it looks like there's a mistake in LocalStorageSource.commit(). I suspect that model.map was supposed to be model.forEach, or else the function was supposed to return the whole model instead of just the primary key.

Or am I misunderstanding this whole thing and I'm not using it correctly? I'm happy to submit a PR, I just want to make sure I'm not completely off base first.


  • I had this same issue the first time I used Collections and Models. The issue is that you need to add the source to the Model used as well. The collection and its models are saved separately. The UUID is used to fetch the model from LocalStorage. In short, add the source to MessageModel.
  • Ah, I see. So each model has to also be committed, then commiting the collection saves the references. That seems cumbersome, but I guess I can make it easier by setting commit: true in the model and collection options.

    Thanks, Roy.
  • edited June 2016
    Shouldn't lines 732/733 in Model.js constructor read:

    commit && this.commit(opts);
    fetch && this.fetch(opts);
    (Currently, it is without opts, e.g., commit && this.commit();)

    Otherwise, the model commit function does not respect the options sent in...
  • Any chance you can put together a sample that shows the problem and a pull request that fixes it?
Sign In or Register to comment.