Summer Sale! All accounts are 50% off this week.

ashleywnj's avatar

Vue simple JSON example

Hi - appreciate any feedback - thanks in advance.

Playing with Vue and looking to populate a simple list from json data source. I have modified the example provided by vue - it works in jsfiddle: https://jsfiddle.net/ashleywnj/f84o1fxt/ but I cannot return any data once I pull it into a laravel blade page:

Script ....

var apiURL = 'https://api.github.com/repositories/11730342/commits?per_page=5&sha=';

var demoList = new Vue({ el: '#demoList', data: { currentBranch: 'dev', items: null }, created: function () { this.fetchData(); }, methods: { fetchData: function () { var self = this; $.get( apiURL, function( data ) { self.items = data; }); } } });

....

html ....

Latest Vue.js Commits

  • - @{{commit.message}}
    by @{{commit.author.name}} at @{{commit.author.date}}
....

And of course referencing the cdn's .... Scripts -->

....

I have a blade view within the same project that is working well with a modified vue grid - so I know the references to the libraries are correct.

both the page and console have zero output

0 likes
6 replies
vitorarjol's avatar
Level 10

Hi @ashleywnj. I saw that in your fiddle you're using the 0.11 version. Is that right?

This version is very old, and there are a lot of changes on the 1.0.* releases.

I made a fiddle with the newest version (1.0.7), that works. Here is the code:

https://jsfiddle.net/kkpLnv6k/

Some points that you can take a look:

First, this is the cheatsheet for the syntax of the 1.0.* version: https://github.com/vuejs/vue/wiki/1.0.0-binding-syntax-cheatsheet

Other point is that the v-repeat directive is deprecated. Now, you have to use the v-for directive.

The last one is that in order to make it work with the Blade templates, you have to put the @ symbol before the mustache tags, as you have made in your fiddle.

Hope it helps!!

1 like
Devmaurice's avatar

Which Vue version are you using??

Then use the vue-resource to get data.

 fetchData: function () {
    var self = this;
    self .$http.get( apiURL, function( data ) {
        self.items = data;
    });

    } 

check this

1 like
ashleywnj's avatar

Thank you @vitorarjol - with the updated cdn and your script it worked. Still puzzled why i could get it to work in a fiddle but not in my page.

Thanks @Devmaurice - will try the vue-resource to get the data rather than jquery to parse.

1 like
vitorarjol's avatar

@ashleywnj Maybe it's because the list of items need an identifier in order to work. Something like this:

<li v-repeat="item: items">
      - <span class="message">{{item.commit.message}}</span><br>
      by <span class="author">{{item.commit.author.name}}</span>
      at <span class="date">{{item.commit.author.date}}</span>
    </li>

These are the old docs: http://011.vuejs.org/guide/list.html#Using_an_identifier

=]

1 like
erikverbeek's avatar

@vitorarjol "The last one is that in order to make it work with the Blade templates, you have to put the @ symbol before the mustache tags, as you have made in your fiddle."

If you use Vue and Blade together a lot you can also choose to change the delimiters of Vue. You can do this by adding:

Vue.config.delimiters = ["[[", "]]"];

This way you can use {{ }} for Blade and [[ ]] for Vue.

3 likes

Please or to participate in this conversation.