1

I've a Vue.js instance as follows:

new Vue({
  el: '#app',
  data: {
      active: ''
  }
  methods: {
      addActive: function(){
         $(document).on('click', '[prop-name]', function(){
                this.active = {name: $('this').attr('prop-name'), latitude: $('this').attr('prop-lat'), longitude: $('this').attr('prop-long'), icon: $('this').attr('prop-icon')};
            }.bind(this));
      }
  } 
});

I want to set the data active when I trigger the addActive method. But I think I'm unable to set the active like I'm doing it here. How can I do this?

2 Answers 2

4

I'm not convinced this is idiomatic Vue but I'm sure there's a reason behind the way you're trying to do this. Anyway, this solves your problem:

new Vue({
  el: '#app',
  data: {
      active: {
      },
  },
  ready: function() {
      this.addActive();
  },
  methods: {
      addActive: function(){
          $(document).on('click', '[prop-name]', function (e) {
              var target = e.target;
              this.active = {
                  name: $(target).attr('prop-name'), 
                  latitude: $(target).attr('prop-lat'), 
                  longitude: $(target).attr('prop-long'), 
                  icon: $(target).attr('prop-icon'),
              };   
         }.bind(this));
      }
  } 
});

Jsfiddle here: https://jsfiddle.net/3hbr9rza/

Sign up to request clarification or add additional context in comments.

Comments

0

You don't actually need jQuery for that and I think this is a more "Vue way" to do it.

Here is the working fiddle: http://jsfiddle.net/q4ky8g9r/

HTML:

   <div v-for="prop in props"
        v-set-active-prop="active"
        prop-name="test-{{$index}}" 
        prop-lat="1.111" 
        prop-long="2.2222" 
        prop-icon="icon.png"
        class="xpto"
    >
       I have many props (click me)
    </div>

    <div v-if="active">
        <p>{{active.name}}</p>
        <p>{{active.lat}}</p>
        <p>{{active.long}}</p>
        <p>{{active.icon}}</p>
    </div> 

</div>

Vue Directive:

Vue.directive('set-active-prop', {
  twoWay: true,
  bind: function () {
      var el = this.el,
          self = this;

      el.addEventListener("click", function() {
          var active = {
            name: el.getAttribute('prop-name'),
            lat: el.getAttribute('prop-lat'),
            long: el.getAttribute('prop-long'),
            icon: el.getAttribute('prop-icon')
        }
        self.set(active);
      });
  },
  update: function (newValue, oldValue) {


  },
  unbind: function () {}
})

Vue Instance:

new Vue({
  el: '#app',
  data: {
      props: 4,
      active: {
         name: null,
         lat: null,
         long: null,
         icon: null
      }
  },
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.