Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model Sync Local Storage - sync layer for Model and ModelList #190

Closed
wants to merge 7 commits into from
Closed

Model Sync Local Storage - sync layer for Model and ModelList #190

wants to merge 7 commits into from

Conversation

clarle
Copy link
Collaborator

@clarle clarle commented Jul 13, 2012

Looks like I'm probably too late for this release, but here goes:

This provides Y.ModelSync.Local, an extension which provides a sync implementation through locally stored
key value pairs, either through the HTML localStorage API or falling back onto an in-memory cache, that can be mixed into a Model or ModelList subclass.

A group of Models/ModelLists is serialized in localStorage by either its class name, or a specified 'root' that is provided.

Example Usage

var User = Y.Base.create('user', Y.Model, [Y.ModelSync.Local], {
    root: 'users'
});

var Users = Y.Base.create('users', Y.ModelList, [Y.ModelSync.Local], {
    model: User,
    root  : 'users'
});

var myUser = new User({id: '123'});

// Will load the data from the in-memory object
// or HTML5 localStorage
myUser.load(); 

// Will update the in-memory object 
// and HTML5 localStorage
myUser.set('name', 'Eric F').save();

// Will delete the item in the in-memory object 
// and HTML5 localStorage
myUser.destroy({remove: true});

// Will create a new item in the in-memory object 
// and localStorage under key 'users' with a generated GUID
var newUser = new User({name: 'Clarence L'});
newUser.save(); 

To-do

Has API docs, high test coverage, and is only missing user documentation. However, there is an example writen for TodoMVC that can be found here:

https://github.com/clarle/todomvc/tree/yui-3.6.0/architecture-examples/yuilibrary

@ghost ghost assigned ericf Jul 13, 2012
@ericf
Copy link
Member

ericf commented Jul 13, 2012

(Keeping a record of what we decided in IRC)

@clarle Looks good on first glance! But let's keep iterating on this in Gallery and put it through its paces some more. We'll plan on merging this in for 3.7.0pr1.


component=model-sync-local
component.jsfiles=model-extensions/model-sync-local.js
component.requires=model, model-list, json-stringify
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to explicitly require model-list. I left it out of ModelSync.REST's requirements too.

@juandopazo
Copy link
Member

Looks great!

@clarle
Copy link
Collaborator Author

clarle commented Jul 24, 2012

@juandopazo and anyone else interested in testing it out, ModelSync.Local is available in the Gallery here:

http://yuilibrary.com/gallery/show/model-sync-local

@juandopazo
Copy link
Member

I'm totally using this for a new project :)

this.storage = Y.config.win.localStorage;
store = this.storage.getItem(this.root);
} catch (e) {
Y.log("Could not access localStorage.", "warn");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you propose to use the extension? Like this?

var exts = [];
if (Y.config.win.localStorage) {
  exts.push(Y.ModelSync.Local);
}
var Foo = Y.Base.create('foo', Y.Model, exts)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should fall back to an in-memory store if Y.config.win.localStorage is not available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to use Y.CacheOffline?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericf and I discussed this before, and we decided that Y.CacheOffline had too much overhead and not all of the parts in it would be needed for ModelSync.Local, so just a quick re-implementation of the core portion of it would be fine. Any thoughts on your side?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I was thinking exactly the same. Let me know if I you need help with the implementation.

@ericf
Copy link
Member

ericf commented Sep 6, 2012

A quick update:

I am going to need to wait until after this sprint to pull this in (code freeze is this Friday, 9/7).

That said, I've been thinking about using localStorage in conjunction with ModelSync.REST, which could enable a couple of things:

  1. Offline support: This seem great for mobile devices which may have a spotty connection. This is probably a huge rabbit hole, since master-master replication is a PITA and app-specific.
  2. Multi-tab support: One thing I really hate is having an app open in two tabs and each one displaying a different state. By passing all changes through localStorage, the sync layer can listen to the storage event and to get updates which are happening in other tabs/windows of that browser.

@juandopazo
Copy link
Member

\o/ Thanks for tackling my problems!

@clarle
Copy link
Collaborator Author

clarle commented Sep 6, 2012

Yeah, I understand, it's for the better. I've made some changes to the Gallery version of LocalStorage that I haven't synced up to this PR yet, my apologies.

I still need to do more testing on it myself in the meantime and write user documentation for it. There might be a fairly major API change for this module (I forgot about the use case where people can have multiple ModelLists that share the same Model), so that's one thing I need to work on.

@triptych
Copy link
Contributor

triptych commented Jan 7, 2013

@clarle would it make sense to close this one out in favor of your new one #385 ?

@clarle
Copy link
Collaborator Author

clarle commented Jan 7, 2013

@triptych Yep, sounds good to me.

@clarle clarle closed this Jan 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants