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

Function on :text #49

Closed
MasterPuffin opened this issue Oct 7, 2024 · 5 comments
Closed

Function on :text #49

MasterPuffin opened this issue Oct 7, 2024 · 5 comments

Comments

@MasterPuffin
Copy link

I want to use a function to format a text, for example

<p :text="()=>{formatVal(object[field.name])}"></p>

however, this renders ()=>{formatVal(object[field.name])} and not the return value of the function.

@dy
Copy link
Owner

dy commented Oct 7, 2024

I can see a few ways

  1. <p :text="formatVal(object[field.name])"></p>
  2. <p :with="{formatted: formatVal(object[field.name})}" :text="formatted"></p>
  3. sprae(el, { object:..., get formatted(){ return formatVal(this.object[field.name]) } })
  4. let formatted = computed(()=>formatVal(object[field.name])); sprae(el, { object, formatted })
  5. <p :text="computed(() => formatVal(object[field.name]))"></p>, import {computed} from 'sprae'; sprae(el, {..., computed})

But - possibly that's good idea to autowrap function props into computed.
Please let me know if you find other situations when you need it, if there's 2-3 use-cases I can try to add it.

@MasterPuffin
Copy link
Author

It would be very useful for inline edits, eg. a display of values which can be toggled to inputs.
One scenario would be rendering of numbers. Numbers are stored without trailing zeroes which makes displaying for example prices not pretty.
Also to display the label of a select field. The value would be a storable value, but if I want to display the corresponding label I could access it via a function call.

@dy
Copy link
Owner

dy commented Oct 7, 2024

@MasterPuffin which of this cases cannot be solved with just :text="formatVal(object[field.name])"? I wonder what's the purpose of specifically function here :text="() => formatVal(object[field.name])"

@MasterPuffin
Copy link
Author

Sorry I was confused, I thought that passing parameters this way is not possible, like described in my issue here #46

@dy
Copy link
Owner

dy commented Oct 7, 2024

Yes, essentially attribute receives exactly the value you pass: for :onclick that must be a function, for :attr that must be a string/number/etc.

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

No branches or pull requests

2 participants