I think it would be useful to make it possible to handle all of the different behaviors using functions.
To do this, we would need to create a standard format for what these return. Creating the behavior functions would be handled by another package.
For example, here is what an async and a value might look like:
import { ObservableObject, asyncGet, resolver } from "can/everything";
class AppViewModel extends ObservableObject {
static define = {
customerId: Number,
// this uses an `asyncGet` function that creates a DefinitionObject type that has
// specific Symbols that ObservableObject understands
customer: asyncGet((resolve) => {
Customer.get({ id: this.customerId })
.then((customer) => {
resolve(customer);
});
}),
name: String,
// this uses a `resolver` function that creates a DefinitionObject type that has
// specific Symbols that ObservableObject understands
nameChangeCount: resolver(({ listenTo, resolve }) => {
let count = 0;
listenTo( "name", () => resolve( ++count ) );
resolve( count );
})
};
}
The benefits of this are:
users don't have to remember specific property names (like value, async, etc)
we could eliminate the multiple ways of handling the same thing (prop: Number vs prop: { type: Number }
if this is the only way of doing things, it would enable tree-shaking modules you aren't using (for example ResolverObservable adds 3.37 kB)
The downsides are:
we would have to figure out how you compose two behaviors, if we want that to be possible
I think it would be useful to make it possible to handle all of the different behaviors using functions.
To do this, we would need to create a standard format for what these return. Creating the behavior functions would be handled by another package.
For example, here is what an
async
and avalue
might look like:The benefits of this are:
value
,async
, etc)prop: Number
vsprop: { type: Number }
ResolverObservable
adds 3.37 kB)The downsides are:
Related to https://github.com/canjs/can-stache-element/issues/47#issuecomment-509819918.