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
Use microstates library i.e. a new API #69
Conversation
```js import { Service } from 'ember-microstates'; class User { name = String; email = String; superuser = Boolean; } export default Service.extend({ typeClass: User, defaultValue() { return { superuser: false }; } }); ``` ```js import Controller from '@ember/controller'; import { inject as service } from '@ember/service'; import { alias } from '@ember/object/computed'; export default Controller.extend({ currentUserService: service('user'), user: alias('currentUserService.microstate'), actions: { save() { let userMicrostate = this.user; let value = userMicrostate.valueOf(); alert(JSON.stringify(value)); } } }); ``` ```hbs <form {{action 'save' on='submit'}}> <input type='text' placeholder='Name' oninput={{invoke user.name 'set' value='target.value'}}> <input type='text' placeholder='Email' oninput={{invoke user.email 'set' value='target.value'}}> <label> Is an admin? <input type='checkbox' checked={{user.state.superuser}} onchange={{invoke user.superuser 'toggle' value='target.checked'}}> </label> <button>Submit</button> </form> ```
Also remove a debugger
With this kind of service, for now it might be better to use service actions instead of the invoke function. This was the approach that Charles and I planned for until the Applicatives wrapper is ready. What do you think? |
I'm for that as well, it's a little used pattern that should be used more. // services/current-user.js
export default Service.extend({
typeClass: User,
defaultValue() {
return {
superuser: false
};
},
actions: {
setName(name) {
this.microstate.name.set(name);
},
setEmail(email) {
this.microstate.email.set(email);
},
toggleSuperuser() {
this.microstate.superuser.toggle();
}
}
}); <form {{action 'save' on='submit'}}>
<input type='text' placeholder='Name' oninput={{action 'setName' value='target.value' target=currentUserService}}>
<input type='text' placeholder='Email' oninput={{action 'setEmail' value='target.value' target=currentUserService}}>
<label>
Is an admin?
<input type='checkbox' checked={{user.superuser.state}} onchange={{action 'toggleSuperuser' value='target.checked' target=currentUserService}}>
</label>
<button>Submit</button>
</form> |
The only issue is that it might be repetitive especially if you are nesting many classes for the "schema". |
Why not create an implicit action for each custom transition of the microstate? |
@cowboyd would they be recursive like the microstate? I don't think it's possible for actions hash to be a computed property. Right? |
Unsure, probably not supported. But you can use functions outside of the actions hash as actions, e.g. // services/current-user.js
export default Service.extend({
typeClass: User,
defaultValue() {
return {
superuser: false
};
},
init() {
this._super(...arguments);
this.toggleSuperuser = this.microstate.superuser.toggle.bind(this.microstate.superuser);
}
}); <button type='button' onclick={{action userService.toggleSuperuser}}>
Toggle Super User
</button> The above would work if the method was bound before hand (example twiddle). |
👍 |
@knownasilya |
<label> | ||
Is an admin? | ||
<input type='checkbox' checked={{user.superuser.state}} onchange={{invoke user.superuser 'toggle' value='target.checked'}}> |
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.
You'd still want this value='target.checked'
since otherwise you get an event and that is set. I think..
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.
toggle
doesn't take any arguments. Event will be ignored.
Remove custom function for setting up a service since microstates are much simpler to setup now.
Never mind, I updated and the error seems to have gone. Adding some basic acceptance tests. |
Closing in favor of #72 |
WIP, don't merge yet!
Resolves #68
This change removes all of the helpers for a (currently) single HoC
state-for
component which takes a type class and a value to create a microstate using the default middleware.I removed all of the tests and update to the latest cli and ember versions. This change will require a major version bump because it fundamentally changes this addon.
New API
Component API
Service API
Using
Template
TODO
action
helper, currently we write our owninvoke
helper Auto binding transitions thefrontside/microstates#135Trying it out
npm install
node_modules/microstates
andnpm install
.ember-microstates
main dir runnpm start
.