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

localStorage persistance #10

Merged
merged 1 commit into from
Apr 6, 2017
Merged

Conversation

Martodox
Copy link
Contributor

@Martodox Martodox commented Apr 5, 2017

I had to abstract Todo class away from main file and added a service to delegate the responsibility.

@@ -0,0 +1,21 @@
import Todo from '../models/todo';
Copy link
Member

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

Copy link
Member

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.

Copy link
Contributor Author

@Martodox Martodox Apr 5, 2017

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?

Copy link
Member

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.

Copy link
Member

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

Copy link
Contributor Author

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?

@@ -0,0 +1,26 @@
import { tracked } from "@glimmer/component";
Copy link
Member

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

@Martodox
Copy link
Contributor Author

Martodox commented Apr 5, 2017

@rwjblue I tried to move the files around, but cli throws this error:
Error: The collection 'components' is not configured to contain a collection 'services'
Any advise?

@@ -1,30 +1,26 @@
import Component, { tracked } from "@glimmer/component";
import Navigo from 'navigo';
import StorageService from '../../../utils/storage';
Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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

import { tracked } from "@glimmer/component";

export default class Todo {
id: number;
Copy link
Member

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.

@@ -0,0 +1,21 @@
import Todo from '../ui/components/-utils/todo';
Copy link
Member

@mmun mmun Apr 5, 2017

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.

this.completed = !this.completed;
}

serialize() {
Copy link
Member

@mmun mmun Apr 5, 2017

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.

Copy link
Contributor Author

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);
Copy link
Member

@mmun mmun Apr 5, 2017

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.

@mmun mmun mentioned this pull request Apr 6, 2017
22 tasks
@@ -0,0 +1,21 @@
import Todo from '../ui/components/-utils/todo';

export default class StorageService {
Copy link
Member

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.


export default class StorageService {

private storageKey = 'todos-glimmer';
Copy link
Member

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

Copy link
Contributor Author

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

@mmun
Copy link
Member

mmun commented Apr 6, 2017

@Martodox Sorry for the tedious review. I'm happy to make these changes myself if you don't have the time! Thanks for contributing! :)

@mmun
Copy link
Member

mmun commented Apr 6, 2017

Thanks. I will follow up with some code style tweaks later. Nothing personal ;)

@mmun mmun merged commit d807ebf into glimmerjs:master Apr 6, 2017
@Martodox
Copy link
Contributor Author

Martodox commented Apr 6, 2017

No problem. Happy to help to workout code guidelines for glimmer apps :)

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