-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
with directive #5269
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
Comments
@rhyek, you could actually do a computed property for that. |
Actually I do like the idea, but I would call it |
It's better to create a component for that and compute there only once the heavy operation you need |
@posva this is actually intended for one off computations you won't need anywhere else. It's simpler, quicker, and offers more readability. For more complex and reusable computations and functionality, I always use the approach you mention. |
It's quicker to add once you know the syntax, but it's one more thing to know and adds another way of doing the same thing. That said, I see the benefits and simplicity of it, I simply think it's better to use a component, even if it's just declared on one component, because if you know that way you know a more powerful way of abstracting things, and will probably understand better how to use components |
@posva, for computing one property such as I do in my example, I would probably use @nickmessing's suggestion, currently. If I need more complexity/functionality, I tend to use components for that. I agree with you that components are the most powerful option (in fact, I like them a lot), but for some cases it might feel a bit overkill. IMO:
|
Ok, still simpler to use a component for all cases
…On Sat, 25 Mar 2017, 15:59 Carlos Gonzales, ***@***.***> wrote:
@posva <https://github.com/posva>, for computing one property such as I
do in my example, I would probably use @nickmessing
<https://github.com/nickmessing>'s suggestion, *currently*. If I need
more complexity/functionality, I tend to use components for that. I agree
with you that components are the most powerful option (in fact, I like
them a lot <http://codepen.io/rhyek/pen/vyxOdQ>), but for some cases it
might feel a bit overkill.
IMO:
- One or maybe two computations: with or v-scope
- Two or more: @nickmessing <https://github.com/nickmessing>'s
suggestion
- More than a few properties, use of any other view model
functionality (watchers, methods, etc), own template, etc: Components
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5269 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAoicSLXk8SIsvYTBLuf53FvY7wINbfuks5rpSvPgaJpZM4MniyD>
.
|
I think that it is not difficult to achieve this issue, but the template syntax check is a problem. |
I think It could instead be something like: <tr
v-for="meeting in meetings"
v-scope="{ person: people.find(person => person.id === meeting.personId) }"
>
<td>{{ meeting.date }}</td>
<td>{{ scope.person.firstName }}</td>
<td>{{ scope.person.lastName }}</td>
</tr> |
Another (maybe dumb) example: <tbody>
<template
v-for="(meeting, index) of meetings"
v-scope="{ class: [index % 2 === 0 ? 'even' : 'odd'] }"
>
<tr class="summary" :class="scope.class">
...
</tr>
<tr class="detail" :class="scope.class">
...
</tr>
</template>
</tbody> You get the idea. @posva, you can't use components here because they can only have one root element. 😄 |
Actually, you can, the tbody can be a component |
Using your method, the component would be per meeting.
Edit: Nvm, I see what you mean.
…On Mar 25, 2017 3:14 PM, "Eduardo San Martin Morote" < ***@***.***> wrote:
Actually, you can, the tbody can be a component
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5269 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAq5ype0LqlKiALnYvcSTXsc-tJD6327ks5rpYOxgaJpZM4MniyD>
.
|
@posva I think that in some cases he is right, everyone wants to write a little code to achieve more features :) |
@rhyek p(
v-for="(meeting, i, person=people.find(person => person.id === meeting.personId)) in meetings",
)
| {{ meeting.date }} |
| {{ person.firstName }} |
| {{ person.lastName }}
|
That's kinda funny. It definitely works and I think I actually like this syntax better. This is something that should maybe be mentioned in the guides? Anyways, thank you @pbastowski. Feel free to close the issue if so desired. |
Eh, apparently the expression in <tr
v-for="(
meeting,
index,
person=people.find(person => person.id === meeting.personId)
) in meetings"
> Bummer. |
@rhyek I submitted a PR, hoping to help you |
@pbastowski In fact the above method does not solve all the situation, I think add |
@gebilaoxiong My method solves the reported problem. I use this method myself. So, just wondering, what problems does it not solve that a "with" directive will solve? |
@pbastowski There are a lot of ways to solve the problem, you also want to be able to maintain a good code, rather than write a line, is not it? |
@gebilaoxiong Your statement and the following leading question is so broad that it can apply to any aspect of programming and perhaps even life itself :) But seriously, I agree with you. A |
@pbastowski Actually inside the https://vuejs.org/v2/api/#v-for <div v-for="(val, key, index) in object"></div> Your method finally generates the code that's it like this: function (meeting, index, person = people.find(person => person.id === meeting.personId)) {
// ...
} and also this method only supports one parameter, if there are multiple?
My English is not very good, no intention to offend :) |
@gebilaoxiong Yes, that is correct. My method only works for iterating arrays, like in the question asked in this issue. Iterating objects would not work at all. |
I feel it may over complexify the template syntax. Any of computed properties, methods and components can solve these problems easily. |
@CodinCat As I said, all roads lead to Rome. But we want to choose a good way :)
<tr v-for="(meeting, index) in meetings">
<td>{{ meeting.date }}</td>
<td>{{ findPerson(meeting.personId).firstName }}</td>
<td>{{ findPerson(meeting.personId).lastName }}</td>
</tr>
You can imagine, each field needs to be calculated once
So I think the you can use it like this <tr v-for="meeting in meetings"
with="{ person: people.find(person => person.id === meeting.personId) }">
<td>{{ meeting.date }}</td>
<td>{{ person.firstName }}</td>
<td>{{ person.lastName }}</td>
</tr>
|
@gebilaoxiong computedMeetings () {
return this.meetings.map(meeting => Object.assign({}, meeting, {person: this.people.find(person => person.id === meeting.personId)}))
} <tr v-for="meeting in computedMeetings">
<td>{{ meeting.date }}</td>
<td>{{ meeting.person.firstName }}</td>
<td>{{ meeting.person.lastName }}</td>
</tr> |
@Kingwl Well, maybe I'm too horn, I just make a suggestion, use |
I'm afraid Consider the trivial example above, it already has been quite verbose to specify A larger component will probably worsen it. For example, you also want to show the attendant's meeting note in the component. More logic is crammed to A larger team will also worsen it. A programmer wants to skip a computed property, so does another programmer. In team work a |
@HerringtonDarkholme thx for your reply, I agree very much :) |
Thank you all for your interest. I feel I agree with @HerringtonDarkholme and can see the issues this feature might develop for larger teams/projects. Using a computed property that maps my array or @pbastowski's solution are both good ways to provide the functionality I need. |
What problem does this feature solve?
This would provide something akin to a
with
block. It would be useful to me especially in cases like the one shown here. During iterations where your context is essentially a superset of the current component's, you can't have a computed property that definesperson
since it would be different for everymeeting
.There are, of course, ways around this. The most straightforward would be to set
person
on themeeting
object during creation. The issue is that creating the object might be done in different ways (locally, initial data load, server broadcast, etc) and you would need to handle all those cases, preferably in a DRY manner.It could be made to only work with
v-for
to avoid this.What does the proposed API look like?
The text was updated successfully, but these errors were encountered: