-
Notifications
You must be signed in to change notification settings - Fork 10
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
localStorage persistance #10
Conversation
@@ -0,0 +1,21 @@ | |||
import Todo from '../models/todo'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this should be in either src/ui/components/todo-item/-services/storage.ts
(if it is "private" to this component).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, sorry about this. We cannot nest services under components. I should have said src/services/storage.ts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run into next issue
Error: The type of module 'services/storage' could not be identified
Is there a way to do imports from the root of the project, like we have in ember?
import StorageService from 'todomvc-demo/services/storage'
Or something similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, looks like https://github.com/glimmerjs/glimmer-application-pipeline/blob/master/lib/broccoli/default-module-configuration.ts does not define services as a type. We should chat with @dgeb to see if that was intentional, but for now you can put it in src/utils/storage.ts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(hoping third time is a charm here 😝 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, that bit of code I was missing.
We could override that in config/environment.js
, couldn't we?
src/ui/components/models/todo.ts
Outdated
@@ -0,0 +1,26 @@ | |||
import { tracked } from "@glimmer/component"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would generally go in src/data/models/todo.ts
or src/ui/components/todo-item/-utils/todo.ts
(I personally prefer the former)...
@rwjblue I tried to move the files around, but cli throws this error: |
fec6428
to
df7dabd
Compare
@@ -1,30 +1,26 @@ | |||
import Component, { tracked } from "@glimmer/component"; | |||
import Navigo from 'navigo'; | |||
import StorageService from '../../../utils/storage'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwjblue Can we do something about those import paths?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use a fully qualified path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fully qualified path like what?
All I could find based on my research was this reference microsoft/TypeScript#5039 with baseUrl prop on tsconf.json
src/ui/components/-utils/todo.ts
Outdated
import { tracked } from "@glimmer/component"; | ||
|
||
export default class Todo { | ||
id: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The id
should be private. It's just a temporary hack because we don't have a good alternative to key="id"
at the moment.
src/utils/storage.ts
Outdated
@@ -0,0 +1,21 @@ | |||
import Todo from '../ui/components/-utils/todo'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't be importing private modules. I'd prefer to make the Todo class public like the store.
src/ui/components/-utils/todo.ts
Outdated
this.completed = !this.completed; | ||
} | ||
|
||
serialize() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it a bit confusing that the deserialization logic is in the store, but the serialization logic is in the model. I think the serialization belongs in the store too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, but conflicts with Todo.id being private.
@@ -73,29 +69,35 @@ export default class TodoMVCApp extends Component { | |||
createTodo(title) { | |||
this.todos.push(new Todo(title)); | |||
this.todos = this.todos; | |||
this.storage.store(this.todos); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the pairs of
this.todos = ...;
this.storage.store(this.todos);
should be extracted to a single method e.g. setTodos
.
src/utils/storage.ts
Outdated
@@ -0,0 +1,21 @@ | |||
import Todo from '../ui/components/-utils/todo'; | |||
|
|||
export default class StorageService { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest a more evocative name like TodoStore
and the file to todo-store.ts
.
src/utils/storage.ts
Outdated
|
||
export default class StorageService { | ||
|
||
private storageKey = 'todos-glimmer'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize this is a major nitpick but can you change this to 'glimmer-todomvc:todos'
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It comes from todomvc specs todos-frameworkName
@Martodox Sorry for the tedious review. I'm happy to make these changes myself if you don't have the time! Thanks for contributing! :) |
df7dabd
to
148b453
Compare
Thanks. I will follow up with some code style tweaks later. Nothing personal ;) |
No problem. Happy to help to workout code guidelines for glimmer apps :) |
I had to abstract Todo class away from main file and added a service to delegate the responsibility.