0

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;
                    }
                });
        }
    }
});

2 Answers 2

2

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;
                }
            });
    }
}

});

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

Comments

0

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

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.