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

Expose enum constants #10

Merged
merged 1 commit into from
Apr 29, 2019
Merged

Expose enum constants #10

merged 1 commit into from
Apr 29, 2019

Conversation

jlsjonas
Copy link
Contributor

Allows exposing the constants for usage in frontend (where $data is being used for more descriptive labels)

@jlsjonas jlsjonas requested a review from aocneanu April 10, 2019 14:00
@jlsjonas jlsjonas added this to Backlog in Laravel Enso via automation Apr 10, 2019
@jlsjonas jlsjonas moved this from Backlog to In Review in Laravel Enso Apr 10, 2019
@aocneanu
Copy link
Member

Do you know about the frontend Enum class?

@jlsjonas
Copy link
Contributor Author

I did not, but I also can't find any usages nor documentations surrounding it.

I'm currently adding my enums to AppState's enums(), where I needed a certain type as their constant (comparing a custom select field to it's value)

Any example or reference to doing it properly would of course be welcome.

@aocneanu
Copy link
Member

aocneanu commented Apr 29, 2019

You should add local enums to a LocalStateBuilder and reuse the bootEnums helper available in the FE.

The you can do

computed: {
   ...mapState('local', ['enums']),
}

....

$this.enums.myEnum._get(key)`

usage example

full api

BTW if you have the time a PR in the docs with this would be great ;)

@jlsjonas
Copy link
Contributor Author

Thanks, I'll look into using & documenting it!

I do believe certain use-cases could benefit from this simpler exposure.
Feel free to disagree and close this PR though 😁

p.s.: I hope you had a nice holiday :-)

@aocneanu
Copy link
Member

I hope you had a nice holiday :-)

Yes I did, finally leveraged 3.2.0 :))))

The same for you :)

@aocneanu aocneanu merged commit 384d410 into master Apr 29, 2019
Laravel Enso automation moved this from In Review to Done Apr 29, 2019
@jlsjonas
Copy link
Contributor Author

jlsjonas commented Apr 29, 2019

Thanks, I did recently :)

Just as a small follow-up (reason why I exposed constants() in the first place):

If you have an enum (LaravelEnso\Helpers\app\Classes\Enum declared with both constants & a $data object; only the numerical keys & strings contained in the data object are exposed to the frontend, right?

ex.:

class MyEnumClass extends Enum
{
    const onEnter = 0;
    const onExit = 1;
    const manual = 2;

    protected static $data = [
        self::onEnter => 'Automatically on enter',
        self::onExit => 'Automatically on exit',
        self::manual => 'Manually',
    ];
}

In the frontend, I want to check something === enums.myEnumClass.manual; the Enum class doesn't provide a method for it,does it? (it's methods always seem to return the full string declared in $data; which makes sense as that's the only data it received from the api)

If confirmed, I'd document exposing via ::constants() too, for this kind of usecase; unless you have an idea to modify Enum.js + the way it's receive the classes to allow for both sets to be defined simultaneously (even then load-wise it's probably interesting to document the constants() method too.

@aocneanu
Copy link
Member

aocneanu commented May 2, 2019

It has a little magic but only if you are using self::MyConstantName' => 'My Constant Name'

@jlsjonas
Copy link
Contributor Author

jlsjonas commented May 2, 2019

@aocneanu commented on May 2, 2019, 9:06 AM GMT+2:

It has a little magic but only if you are using `self::MyConstantName' => 'My Constant Name'

Do you mean declaring self::MyConstantName as a string instead of a direct reference, and thus passing it along back to back-end as a string instead of integer? 🤔 If not, could you provide a more complete example?

Thanks!

@aocneanu
Copy link
Member

aocneanu commented May 2, 2019

Basically the constant name should match the label without spaces

@jlsjonas
Copy link
Contributor Author

jlsjonas commented May 2, 2019

Ah, yes; because spaces get trimmed in the flip.

I'll note it alongside the constant method 👍

@aocneanu aocneanu deleted the jlsjonas-expose-constants branch June 23, 2020 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Laravel Enso
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants