Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  61] [ 2]  / answers: 1 / hits: 29748  / 7 Years ago, thu, august 3, 2017, 12:00:00

Why does the method set runs twice? You can see the console when you click the star.
If I remove @click=set(rating) nothing happens so not like it is again called elsewhere.



http://jsfiddle.net/q22tqoLu/



HTML



<div id=star-app v-cloak>
<star-rating value=0></star-rating>
</div>

<template id=template-star-rating>
<div class=star-rating>
<label
class=star-rating__star
v-for=rating in ratings
:class={'is-selected': ((value >= rating) && value != null), 'is-disabled': disabled}
@mouseover=star_over(rating)
@mouseout=star_out
@click=set(rating)>
<input
class=star-rating star-rating__checkbox
type=radio
:name=name
:disabled=disabled
:id=id
:required=required
v-model=value>

</label>
</div>
</template>


JS



  'use strict';

Vue.component('star-rating', {
template: '#template-star-rating',
data: function data() {
return {
value: null,
temp_value: null,
ratings: [1, 2, 3, 4, 5]
};
},
props: {
'name': String,
'value': null,
'id': String,
'disabled': Boolean,
'required': Boolean
},
methods: {
star_over: function star_over(index) {
if (this.disabled) {
return;
}

this.temp_value = this.value;
this.value = index;
},
star_out: function star_out() {
if (this.disabled) {
return;
}

this.value = this.temp_value;
},
set: function set(value) {
if (this.disabled) {
return;
}

// This runs twice
console.log(value);

this.temp_value = value;
this.value = value;
}
}
});

new Vue({
el: '#star-app'
});


The code is based on older version from someone, here it also doubles https://codepen.io/sktwentysix/pen/oZwXjN


More From » vue.js

 Answers
17

If you move @click=set(rating) to <input/> instead of <label/>, it will run once.


[#56881] Monday, July 31, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;