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
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I can't figure out how to execute a function in Vue.js after an $http.get request has finished. In the following example I want to automatically select an element of foobar right after page load, but this works only when the alert is there.

new Vue({

    el: '#foo',

    data: {
        foobar: [],
        foobar_selected: []
    },

    ready: function() {
        this.fetchFoobar();
        alert('silly');
        this.selectFoobar(2);
    },

    methods: {
        fetchFoobar: function () {
            this.$http.get('foobar_to_fetch', function (foobar_fetched) {
                this.foobar = foobar_fetched;
            })
        },
        selectFoobar: function (index) {
            this.foobar_selected.push(this.foobar[index]);
        }
    }

});

-

<div id="foo">

    <h2>Foobars</h2>
    <table>
        <tr v-repeat="foobar">
            <td>{{ id }}</td>
            <td>{{ name }}</td>
            <td><button v-on="click: selectFoobar($index)">select</button></td>
        </tr>
    </table>

    <h2>Selected Foobars</h2>
    <table>
        <tr v-repeat="foobar_selected">
            <td>{{ id }}</td>
            <td>{{ name }}</td>
        </tr>
    </table>

</div>

It says here https://github.com/vuejs/vue-resource that the third argument of get is success, but I don't understand how I am supposed to use it in this case. At least this works:

this.$http.get('foobar_to_fetch', function (foobar_fetched) {
    this.foobar = foobar_fetched;
}, alert('useless'));

But this does not:

this.$http.get('foobar_to_fetch', function (foobar_fetched) {
    this.foobar = foobar_fetched;
}, this.selectFoobar(2));

What is the correct way to do this in Vue.js?

I am using Vue 0.11.10 and Vue-resource 0.1.16.

share|improve this question
up vote 2 down vote accepted

get has the following signature:

get(url, [data], [success], [options])

data is in square brackets, which means that parameter is optional. Since data is defined as:

Object|string - Data to be sent as the request message data

You are not passing in an object/string as the second parameter to the function. That means that the second argument you are passing in is interpreted as the success callback function! It doesn't have to be the third argument!

If you do something like this:

this.$http.get('foobar_to_fetch', function(data, status, request) {
  this.foobar = data;
  this.selectFoobar(2);
});

It should theoretically work. This is just a guess though based on my glance at the Vue documentation that you linked in your question.

share|improve this answer
    
Thank you. status and request are not even necesseary. The this.selectFoobar(2) just has to be inside the get function and not after it. Can't believe I had not tried that... – Watchduck Nov 24 '15 at 0:56
    
@mate2code Yep they may not be necessary. I was just trying to convey that the function is the success callback. Also even if it wasn't, you would have to wrap the command in a function like this: function() { this.selectFoobar(2) } so the function you're passing it to could execute that code at a later time. That's essentially what callbacks are all about; wrapping up code in functions so you can pass them to other functions that will execute your functions when its the right time. – Shashank Nov 24 '15 at 0:58
    
Thanks for the clarification. "The function is the success callback. That is what I missed, because I thought in terms of "the n-th argument of get(url, [data], [success], [options])". – Watchduck Nov 24 '15 at 1:09

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.