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

Closed
wants to merge 7 commits into
from

Conversation

Projects
None yet
4 participants
@clarle
Contributor

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

This comment has been minimized.

Show comment Hide comment
@ericf

ericf Jul 13, 2012

Member

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

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.

@ericf

View changes

src/app/build-model-sync-local.properties
+
+component=model-sync-local
+component.jsfiles=model-extensions/model-sync-local.js
+component.requires=model, model-list, json-stringify

This comment has been minimized.

Show comment Hide comment
@ericf

ericf Jul 13, 2012

Member

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

@ericf

ericf Jul 13, 2012

Member

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

@ericf

View changes

src/app/meta/app.json
+ "model-sync-local": {
+ "requires": [
+ "model",
+ "model-list",

This comment has been minimized.

Show comment Hide comment
@ericf

ericf Jul 13, 2012

Member

Remove model-list from here too.

@ericf

ericf Jul 13, 2012

Member

Remove model-list from here too.

@ericf

View changes

src/app/js/model-extensions/model-sync-local.js
+A group of Models/ModelLists is serialized in localStorage by either its
+class name, or a specified 'root' that is provided.
+
+ var User = Y.Base.create('user', Y.Model, [Y.ModelSync.REST], {

This comment has been minimized.

Show comment Hide comment
@ericf

ericf Jul 13, 2012

Member

s/REST/Local/

@ericf

ericf Jul 13, 2012

Member

s/REST/Local/

@ericf

View changes

src/app/js/model-extensions/model-sync-local.js
+ root: 'user'
+ });
+
+ var Users = Y.Base.create('users', Y.ModelList, [Y.ModelSync.REST], {

This comment has been minimized.

Show comment Hide comment
@ericf

ericf Jul 13, 2012

Member

s/REST/Local/

@ericf

ericf Jul 13, 2012

Member

s/REST/Local/

This comment has been minimized.

Show comment Hide comment
@clarle

clarle Jul 13, 2012

Contributor

Oops, that's a little embarassing...

@clarle

clarle Jul 13, 2012

Contributor

Oops, that's a little embarassing...

@juandopazo

This comment has been minimized.

Show comment Hide comment
@juandopazo

juandopazo Jul 14, 2012

Member

Looks great!

Member

juandopazo commented Jul 14, 2012

Looks great!

@clarle

This comment has been minimized.

Show comment Hide comment
@clarle

clarle Jul 24, 2012

Contributor

@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

Contributor

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

This comment has been minimized.

Show comment Hide comment
@juandopazo

juandopazo Jul 30, 2012

Member

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

Member

juandopazo commented Jul 30, 2012

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");

This comment has been minimized.

Show comment Hide comment
@juandopazo

juandopazo Aug 10, 2012

Member

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)?
@juandopazo

juandopazo Aug 10, 2012

Member

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

This comment has been minimized.

Show comment Hide comment
@clarle

clarle Aug 10, 2012

Contributor

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

@clarle

clarle Aug 10, 2012

Contributor

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

This comment has been minimized.

Show comment Hide comment
@juandopazo

juandopazo Aug 25, 2012

Member

Does it make sense to use Y.CacheOffline?

@juandopazo

juandopazo Aug 25, 2012

Member

Does it make sense to use Y.CacheOffline?

This comment has been minimized.

Show comment Hide comment
@clarle

clarle Aug 26, 2012

Contributor

@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?

@clarle

clarle Aug 26, 2012

Contributor

@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?

This comment has been minimized.

Show comment Hide comment
@juandopazo

juandopazo Aug 26, 2012

Member

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

@juandopazo

juandopazo Aug 26, 2012

Member

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

@ericf

This comment has been minimized.

Show comment Hide comment
@ericf

ericf Sep 6, 2012

Member

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

This comment has been minimized.

Show comment Hide comment
@juandopazo

juandopazo Sep 6, 2012

Member

\o/ Thanks for tackling my problems!

Member

juandopazo commented Sep 6, 2012

\o/ Thanks for tackling my problems!

@clarle

This comment has been minimized.

Show comment Hide comment
@clarle

clarle Sep 6, 2012

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment Hide comment
@triptych

triptych Jan 7, 2013

Contributor

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

Contributor

triptych commented Jan 7, 2013

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

@clarle

This comment has been minimized.

Show comment Hide comment
@clarle

clarle Jan 7, 2013

Contributor

@triptych Yep, sounds good to me.

Contributor

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