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

Proposal: make it possible to create behaviors using functions #122

Open
phillipskevin opened this issue Aug 7, 2019 · 0 comments
Open
Labels
discussion Open ended discussion about the library and its design

Comments

@phillipskevin
Copy link
Contributor

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

Related to canjs/can-stache-element#47 (comment).

@phillipskevin phillipskevin added the discussion Open ended discussion about the library and its design label Aug 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Open ended discussion about the library and its design
Projects
None yet
Development

No branches or pull requests

1 participant