this lifecycle hook is called whenever arguments to the resource change. This can be useful for calling functions, comparing previous values, etc.
Static fromFor use in the body of a class.
from is what allows resources to be used in JS, they hide the reactivity APIs
from the consumer so that the surface API is smaller.
import { Resource, use } from 'ember-resources';
class SomeResource extends Resource {}
class MyClass {
@use data = SomeResource.from(() => [ ... ]);
}
For use in the body of a class.
from is what allows resources to be used in JS, they hide the reactivity APIs
from the consumer so that the surface API is smaller.
Though it may be more convenient to not wrap your resource abstraction in a helper function.
import { Resource } from 'ember-resources';
class SomeResource extends Resource {}
class MyClass {
data = SomeResource.from(this, () => [ ... ]);
}
However, if you have argument defaults or need to change the shape of arguments depending on what ergonomics you want your users to have, a wrapper function may be better.
export function someResource(context, { foo, bar }) {
return SomeResource.from(context, () => ... );
}
usage:
import { someResource } from 'your-library';
class SomeResource extends Resource {}
class MyClass {
@tracked foo;
@tracked bar;
data = someResource(this, {
foo: () => this.foo,
bar: () => this.bar
});
}
Generated using TypeDoc
The 'Resource' base class has only one lifecycle hook,
modify, which is called during instantiation of the resource as well as on every update of any of any consumed args.Typically, a
Resourcewill be used to build higher-level APIs that you'd then use in your apps. For example, maybe you want to build a reactive-wrapper around a non-reactive wrapper, XState which requires that the "State machine interpreter" is stopped when you are discarding the parent context (such as a component).An example
Once defined, there are two ways to use
MyResourceIn the template, the Resource can be imported (or re-exported from the helpers directory)
When imported (using RFC 779),
When using in javascript, you'll need the
fromutilityHowever, when authoring a Resource, it's useful to co-locate an export of a helper function:
This way, consumers only need one import.