@willvincent. Good to know, thanks. I'm still very new to JS and VUE, so some seemingly obvious things are not quite obvious to me yet. So thanks for the heads up!
@syaiful6. Yes! I found this out a little while after reading @jimmck's comment. Definitely useful to know that I can relabel the key and value variable names.
@Slothlike. I feel your pain buddy! Trying to get nested sets working has been a real uphill challenge, but I'm nearly there. My data is being provided via a View Composer, not AJAX, but that may change in the future. However, I DO grab the details of a page that the menu is linked too via AJAX (so each menu item loads a new page). The way I've got my code is as follows (I've removed a couple of bits that aren't relevant):
Root VUE Instance - Listens for the dispatch message from the component and stores the data the message contains.
// Root VUE instance.
var rootVUE = new Vue ({
el: 'body',
data: {
currentpage: ''
},
events: {
'currentpagedetails': function(currentpagedetails){
this.currentpage = currentpagedetails;
}
}
});
Vue Component - Grabs the data from the API URL by passing the menu ID. This then dispatches a message with the data up to the root VUE instance.
Vue.component ('menutree', {
template: '#menutree-template',
props: {
menutreedata: ''
},
methods: {
fetchPage: function($usermenuID) {
// Define the endpoint for API resource.
var pageresource = this.$resource('api/v1/usermenu/{id}/page');
// Get current page resource with ID.
var vm = this;
pageresource.get({ id: $usermenuID }, function(retrievedpage) {
this.$dispatch('currentpagedetails', retrievedpage);
});
}
}
});
View HTML - Has an @click event handler that activates the AJAX method with the corresponding menu ID field passed to it.
<menutree :menutreedata="{{ $menuJSON }}"></menutree>
<template id="menutree-template">
<ul class="menu">
<li class="menuitem" v-for="treenode in menutreedata">
<span @click="fetchPage(treenode.id)">@{{ treenode.title }}</span>
<menutree :menutreedata="treenode.children"></menutree>
</li>
</ul>
</template>
API routes - These listen for AJAX requests with the menu ID and will send the relevant data back. This part is probably not correct REST format and I'll be looking back at this to redo.
Route::group(['prefix' => 'api/v1'], function(){
Route::resource('usermenu','ApiUsermenuController');
Route::get('usermenu/{id}/page','ApiUsermenuController@showpage');
});
ApiUsermenuController. The Route then runs @showpage, passing in the menu $id.
public function showpage($id)
{
return Usermenu::find($id)->page()->get();
}
Hopefully this helps a little and shows how AJAX retrieval works! Good luck!