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

introduce utility methods? #84

Closed
mweststrate opened this issue Apr 13, 2017 · 13 comments
Closed

introduce utility methods? #84

mweststrate opened this issue Apr 13, 2017 · 13 comments

Comments

@mweststrate
Copy link
Member

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?

@mweststrate
Copy link
Member Author

mweststrate commented Apr 26, 2017

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:

  1. Introduce util
    searchTodos: util(function (text) { })

  2. Or restore explicit actions again
    addTodo: action(...)

  3. Or define actions on models seperately
    or to create action, pass a second argument to types.model which defines all the actions?

What dou you think @mattiamanzati @RainerAtSpirit?

@mattiamanzati
Copy link
Contributor

mattiamanzati commented Apr 26, 2017

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

@mweststrate
Copy link
Member Author

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

@RainerAtSpirit
Copy link
Contributor

Version 3 looks good to me.

@mweststrate
Copy link
Member Author

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

@mattiamanzati
Copy link
Contributor

@mweststrate have you checked out ThisType of TS2.3? microsoft/TypeScript#14141

@mweststrate
Copy link
Member Author

Freaking genious :)

@mattruby
Copy link
Member

mattruby commented Apr 26, 2017 via email

@mattiamanzati
Copy link
Contributor

@mattruby @mweststrate yeah, lifecycle events are awesome. Looking forward to preprocessSnapshot and postprocessSnapshot :P

@mweststrate
Copy link
Member Author

@mattiamanzati specific use cases already?

@mweststrate
Copy link
Member Author

ThisType is genious btw. Really solves this issue

@mattiamanzati
Copy link
Contributor

@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 :)

@mweststrate
Copy link
Member Author

@mattiamanzati can you open seperate issue?

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

No branches or pull requests

4 participants