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:
enyo.kind({
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']
"{"model":false,
"collection":true,
"data":[
"7830e888-1229-74b5cb16-1a649d850eb8",
"3e246aad-e848-15b18c8b-e8812ec2cf90",
"c299389e-ee68-cc924146-77fe56a69391",
"cc27dce0-26b2-05e96d4b-9834e4a29e23"
]
}"
(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.

Comments

  • 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:
    <br />			commit && this.commit(opts);<br />			fetch && this.fetch(opts);<br />
    (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.