-
Notifications
You must be signed in to change notification settings - Fork 643
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
introduce utility methods? #84
Comments
To elaborate on this one: const Store = types.model({
todos: types.array(Todo),
get completedTodos() { return this.todos.filter }, // OK -> computed
addTodo(todo) { this.todos.add(todo) }, // OK -> action
searchTodos(text) { // Not ok, will become an actionwhich is tracked
return this.todos.filter(t => t.title.indexOf(text) !== -1)
}
}) Solutions:
What dou you think @mattiamanzati @RainerAtSpirit? |
@mweststrate I like mostly the third one, would also allow clearer typings because we can make the first parameter accept only factories and immutable values, and the second actions. About static ones, imho they are not needed. They can be wrote as functions with object as first parameter. And do we really want untracked ones in models? |
Me too, so that changes the example to: const Store = types.model({
todos: types.array(Todo),
get completedTodos() { return this.todos.filter }, // OK -> computed
searchTodos(text) { // OK -> a view. Not allowed to have side effects
return this.todos.filter(t => t.title.indexOf(text) !== -1)
}
}, {
addTodo(todo) { this.todos.add(todo) }, // OK -> action
// hooks also go in here
}) |
Version 3 looks good to me. |
Eh problem with that, the An alternative could be:
but that get's quite verbose. Small upside is that this introduces a 'postCreate' for free :) |
@mweststrate have you checked out ThisType of TS2.3? microsoft/TypeScript#14141 |
Freaking genious :) |
I'm digging this implementation. Post create is really useful.
…On Wed, Apr 26, 2017, 6:06 AM Michel Weststrate ***@***.***> wrote:
Eh problem with that, the this for actions is hard to type.
An alternative could be:
const Store = types.model({
todos: types.array(Todo),
get completedTodos() { return this.todos.filter }, // OK -> computed
searchTodos(text) { // OK -> a view. Not allowed to have side effects
return this.todos.filter(t => t.title.indexOf(text) !== -1)
}
}, (instance) => {
function addTodo(todo) {
instance.todos.add(todo)
}
return { addTodo }
})
but that get's quite verbose. Small upside is that this introduces a
'postCreate' for free :)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#84 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAIrcrLRUS6coIAZB74g03Ullw0JEbIaks5rzyUZgaJpZM4M8UTv>
.
|
@mattruby @mweststrate yeah, lifecycle events are awesome. Looking forward to preprocessSnapshot and postprocessSnapshot :P |
@mattiamanzati specific use cases already? |
|
@mweststrate mostly to polyfill missing array of references, and because I have some rest api built up with Laravel (PHP Framework) which includes snapshots also for related data (e.g. Comment author, category data, etc...). In my store that data is normalized, so I would like to do creation of missing entities at /entities/[name]/[id] directly on the snapshot logic instead of preprocessing the snapshot and then apply it :) |
@mattiamanzati can you open seperate issue? |
currently only computed values and actions can be defined on a model. However utility method (that can be tracked, unlike actions) or static methods now need to be defined outside models. Find a place for them inside models?
The text was updated successfully, but these errors were encountered: