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

Update docs to mobx 3 #725

Merged
merged 2 commits into from
Jan 9, 2017
Merged

Update docs to mobx 3 #725

merged 2 commits into from
Jan 9, 2017

Conversation

mweststrate
Copy link
Member

nuf said


@action.bound
increment() {
this.tick++ // 'this' will always be correct

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain why this might not be correct using plain action

## Bound actions

The `action` decorator / function follows the normal rules for binding in javascript.
However, Mobx 3 introduces `action.bound` to automatically bind actions to the targeted object.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please shortly explain the difference as in:

The action decorator / function follows the normal rules for binding in javascript... That is it [only] binds this and that
... action.bound to automatically bind actions to the targeted object. That is in this and that situation

The following conversion rules are applied, but can be fine-tuned by using *modifiers*. See below.

1. If *value* is wrapped in the *modifier* `asMap`: a new [Observable Map](map.md) will be returned. Observable maps are very useful if you don't want to react just to the change of a specific entry, but also to the addition or removal of entries.
1. If *value* is an wrapped is an instance of an [ES6 Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map): a new [Observable Map](map.md) will be returned. Observable maps are very useful if you don't want to react just to the change of a specific entry, but also to the addition or removal of entries.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is an wrapped is an instance

should probably be

is a wrapped instance


`@computed` can be parameterized. `@computed({asStructure: true})` makes sure that the result of a derivation is compared structurally instead of referentially with its preview value. This makes sure that observers of the computation don't re-evaluate if new structures are returned that are structurally equal to the original ones. This is very useful when working with point, vector or color structures for example. It behaves the same as the `asStructure` modifier for observable values.
If your environment doesn't support decorators, use the `computed(expression)` modifier incombination with `extendObservable` / `observable` to introduce new computed properties.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incombination

## Computed values

Usage:
* `computed(() => expression)`
* `computed(() => expression, (newValue) => void)`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

computed(() => expression, (newValue) => void) isn't explained in the computed docs, only computed(() => expression) and computed(() => expression, options) are explained

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, does the setter behave as an action?

## Modifiers

[Modifiers](modifiers.md) can be used to define special behavior for certain properties.
For example `observable.ref` creates an observable reference which doesn't automatically convert it's values into observables, and `computed` introduces a derived property:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's -> its

`observable(asMap(values?, modifier?))` (and `map(values?, modifier?)`) creates a dynamic keyed observable map.
## `observable.map(values)`

`observable.map(values?)` creates a dynamic keyed observable map.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably makes sense to add

To create dynamically keyed objects always use maps!

here as well. You can never have too few warnings :)

In general, this should just do what you need, but if you want you can override the default behavior using _modifiers_.
Note that modifiers are 'sticky', they are interpreted as being annotations.
They do not only apply to the current value, but also to all values that are assigned in the future to the same attribute.
Modifiers can be used decorator or in combination with `extendObservable` and `observable.object` to change the autoconversion rules for specific properties.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be used decorator -> can be used as decorators

If this modifier is used, `observable` will not attempt to make the value observable.
Use this for example if you want to store a reference to a function, instead of creating a view based on that function.
You can also use it to prevent that plain objects or arrays are made observable automatically.
// ficitonal example, if author is immutable, we just need to store a reference and shouldn't turn it into an mutable, observable object

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ficitonal -> fictional


To use the `asStructure` modifier in combination with the `@computed` decorator, use the following:
`action`, `action.bound`, `computed` and `computed.struct` can be used as modifiers as well.
See [`computed`](computed-decorator.md) respectively [`action`](action.md).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See computed and action respectively
(word order)

setInterval(ticker.increment, 1000)
```

_Note: don't use *action.bind* with arrow functions; arrow functions are already bound and cannot be rebound._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: should sayaction.bound

## Computed values

Usage:
* `computed(() => expression)`
* `computed(() => expression, (newValue) => void)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, does the setter behave as an action?

So they will not be invoked when their input parameters didn't modifiy or if they are not observed by some other computed value or autorun.
This automatic suspension is very convenient; it means that a computed value that is not observed anymore (for example because the UI in which it was used has disappeared)
can automatically be garbabged collected by MobX, and unlike `autorun`'s you don't have to dispose them yourself.
This sometimes confuses people new to MobX; if you create a computed property but don't use it anywhere in a reaction, it will not cache it's value and recompute more often than seems necesarray.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: it's -> its
Typo: necesarray -> necessary

```

After these assignments:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great example!

If a plain JavaScript object is passed to `observable` all properties inside that object will be made observable.
(A plain object is an object that wasn't created using a constructor function)
If a plain JavaScript object is passed to `observable` all properties inside will be copied into a clone and made observable.
(A plain object is an object that wasn't created using a constructor function / but has `Object` as it's prototype, or no prototype at all.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its

The side effect can be debounced, just like `autorunAsync`.
`reaction` returns a disposer function.
If a string is passed as first argument to `reaction`, it will be used as debug name.
The functions passed to `when` will receive one argument when invoked, the current reaction, which can be used to dispose the when during execution.
The functions passed to `reaction` will receive one argument when invoked, the current reaction, which can be used to dispose the when during execution.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dispose the when?

@mweststrate mweststrate merged commit 7c917aa into gh-pages Jan 9, 2017
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