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

ModelSync.Local implementation (final) - passing on A-grade browsers #1218

Merged
merged 17 commits into from
Sep 27, 2013
Merged

ModelSync.Local implementation (final) - passing on A-grade browsers #1218

merged 17 commits into from
Sep 27, 2013

Conversation

clarle
Copy link
Collaborator

@clarle clarle commented Sep 23, 2013

This implements the feature discussed in #1156.

The previous problem with the PR in #385 was that there were failing tests on older browsers (IE6 and IE7) that did not support localStorage. This includes the fixes for those failing tests, which now pass successfully and fallback to the internal cache.

As mentioned before, ModelSync.Local is currently being used on the YUI TodoMVC and has no other problems beyond the fallback in the older browsers.

Error handling is included with ModelSync.Local to handle the case when localStorage passes its maximum allocated space.

} catch (e) {
return false;
}
})(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should advocate the use of feature testing, rather than simply detecting whether localStorage exists or not. For example, we should test if we're actually able to set and remove items from localStorage, like so:

if (localStorage is detected and the following pass):
    localStorage.setItem(..., ...);
    localStorage.removeItem(...);
then localStorage is available and useable

The main reason being there are times when localStorage is detected, but is actually unusable. For example, when browsing in private mode on an iOS device, localStorage is available but cannot be used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This sounds good to me, I'll push this up in just a bit.

* Switched to feature testing, rather than feature detection
* Various fixes to improve code clarity
@clarle
Copy link
Collaborator Author

clarle commented Sep 24, 2013

@ezequiel Thanks for the feedback and attention to detail!

Made the changes like you suggested, let me know what you think now!

this.root = config.root || '';
}

if (this.model && this.model.prototype.root) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a code comment there that this is checking if the sync layer is being applied to a ModelList and if so, trying to look for a root on its model's propto.

var store;

try {
store = Y.config.win.localStorage;
Copy link
Member

Choose a reason for hiding this comment

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

I think this data should be set back after this suite is done running.

@ericf
Copy link
Member

ericf commented Sep 25, 2013

@clarle this is looking good; I added some line comments. What sort of coverage numbers does this have?

@ericf
Copy link
Member

ericf commented Sep 25, 2013

How are you dealing with a model list which wants its root data as an array, and model which wants the root as an object to be able to lookup its data by id?

I think we may need to rethink the storage structure, unless it's acceptable for loading a model list from localStorage to not guarantee order.

@since @VERSION@
**/
_index: function (options) {
return Y.Object.values(LocalSync._data[this.root]);
Copy link
Member

Choose a reason for hiding this comment

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

This is effectively saying that storage order is not significant for a model list's data. If we want to make it significant and consistent, then I don't want us to store it as an object whose key-order is not guaranteed, especially when going to/from JSON.

Clarence Leung added 2 commits September 26, 2013 09:22
This new mechanism now stores data in the folllowing way:

Key   : 'users'
Value : [{id: 'users-1', name: 'clarle'}, {id: 'users-2}, name: 'ericf'}]

This guarantees the order of the values. A in-memory lookup table by `id` is
used to do lookups faster than iterating through the entire array.
* Add try/catch to JSON.parse
* Add a few clarifying comments on `initializer`
* Using clearer conditional assignment style
@clarle
Copy link
Collaborator Author

clarle commented Sep 26, 2013

@ericf Done - I've changed the storage mechanism to the following.

Key : 'users'
Value : [{id: 'users-1', name: 'clarle'}, {id: 'users-2, name: 'ericf'}]

An in-memory lookup table that maps id to the specific object in memory is also used for doing faster loads for a single Model.

I've also made all the changes that you mentioned, so let me know if there are any other pressing issues.

@clarle
Copy link
Collaborator Author

clarle commented Sep 26, 2013

Coverage stats:

--------------------------+-----------+-----------+-----------+-----------+
File                      |   % Stmts |% Branches |   % Funcs |   % Lines |
--------------------------+-----------+-----------+-----------+-----------+
model-sync-local/         |     97.14 |     87.18 |       100 |     97.14 |
      model-sync-local.js |     97.14 |     87.18 |       100 |     97.14 |

Not all branches will be reachable since some are the fallback branches, so I think this should be fine.

@ericf
Copy link
Member

ericf commented Sep 27, 2013

@clarle could you add tests for the serialization order, mainly to make sure there won't be future regression on that. Then merge this!

@clarle clarle merged commit 2797cd8 into yui:dev-3.x Sep 27, 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.

3 participants