Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am very new to vue. I am trying to execute a simple for loop, but for some reason it is not working. Any help will be appreciated. My code:

var Vue = require('vue');
Vue.use(require('vue-resource'));
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');
new Vue({

    el: '#adresponse',

    ready: function() {
        this.fetchMessages();
    },

    data: {
        classified_bids: {},
        accept_qty: {},
        submitted: false
    },

    methods: {
        fetchMessages: function () {
            this.$http.get('/api/getbids')
                .success(function (bids) {
                    this.classified_bids = bids;
                    for (i = 0; i < this.classified_bids.length; i++) {
                        this.accept_qty[i] = 0;
                    }
                });
        }
    }
});
share|improve this question

Your problem is the value of this inside the .success() callback.

You could try something like this:

this.$http.get('/api/getbids')
  .success(function (bids) {
    this.classified_bids = bids;
    for (i = 0; i < this.classified_bids.length; i++) {
      this.accept_qty[i] = 0;
    }
  }.bind(this));

Similar problem

share|improve this answer

By changing the for loop like so, this worked:

el: '#adresponse',

ready: function() {
    this.fetchMessages();
},

data: {
    classified_bids: {},
    accept_qty: {},
    submitted: false
},

methods: {
    fetchMessages: function () {
        this.$http.get('/api/getbids')
            .success(function (bids) {
                this.classified_bids = bids;
                for (var key in this.classified_bids) {
                    this.accept_qty[key] = 0;
                }
            });
    }
}

});

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.