0

I am using the togglable tabs component in Bootstrap. I would like a table in each tab. Each one is displaying a table of email logs (Click Event, Open Event, etc). I would also like each table to be loaded dynamically with Vue-resource only once the user clicks on that tab, and for the resource/component to only be loaded once (once we have the data from AJAX, don't refresh it).

How can I set this up? I currently have an email-table component and an email-table-template template that renders the table, but I'm not sure how to set those up to render themselves when the user clicks the tab, and to only call the AJAX once.

An illustration of the task

Here is my current code for detecting the tab switch and newing up a Vue component:

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var email_event = $(e.target).data('email-event');
switch(email_event) {
  case 'click':
    createClick();
    break;
  // rest of the cases
}

function createClick() {
var click_events = Vue.resource('/api/email_logs/click_events');

click_events.get().then((response) => {
  new Vue({
    el: '#table-click',
    data: {
      searchQuery: '',
      gridColumns: ['campaign_id', 'target_link_name', 'target_link_url', 'created_at'],
      gridData: response.body
    }
  })
});

Any insight is appreciated. Thanks very much!

2
  • Can you create a jsfiddle of what you have tried and where you are getting error. Commented Nov 22, 2016 at 6:21
  • Though what you want to achieve is clearly shown in the gif, I feel your question may be too abroad. I'd like to suggest you spend a few hours to go through the official guide and come back with any specific problem you feel hard to solve. Commented Nov 22, 2016 at 7:48

2 Answers 2

1

If you want to call a method only once you can use listen and emit events.

vm.$once and vm.$emit should do the trick.

Official documentation https://v2.vuejs.org/v2/api/#vm-once

Here is a quick example https://jsfiddle.net/leocoder/s1nfsao7/4/

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

1 Comment

This is what I was looking for, and it also pointed out to me what I need to change fundamentally; I needed to create Vue wrappers for my Bootstrap tabs. So instead of using jQuery and listening for those click normally, I made a Vue wrapper around the tab and used the Vue v-on:click directive, and tied that to a call to the AJAX. Your example was super helpful in pointing me in the right direction, thanks very much!
0

From official documentation

If you want to keep the switched-out components in memory so that you can preserve their state or avoid re-rendering, you can wrap a dynamic component in a <keep-alive> element

Sample:

<keep-alive> <component :is="currentView"> <!-- inactive components will be cached! --> </component> </keep-alive>

In the above example "currentView" is to be set to a component name you want to load/display

Docs: https://v2.vuejs.org/v2/api/#keep-alive

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.