Home
Man
Search
Today's Posts
Register

Javascript, Javascript libs and toolkits, LAMP, Apache, MySQL, HTML, and other web coding posts.

Turning jQuery Code into Vue.js

Tags
code, jquery, navbar, setinterval, setinterval(), vue, vue.js, web programming

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Turning jQuery Code into Vue.js

The following is some code I am working on the replace our navbar (someday) with a Vue component.

Code:
Vue.component("unix-navbar", {
  template: `<div class="neo-table-border vuenavbar"><div class="flex-item" style="margin-bottom:10px;padding-top:13px;"><a class="vuenavbarhome" :href="url">{{name}}</a></div>
	            <div id="avatar" class="flex-item" v-html="theavatar" v-on:click="OpenMemberPanel()" style="cursor:pointer;" align="center"></div></div>`,
  data() {
    return {
      unixtime: "",
      name: "The UNIX and Linux Forums",
      url: "https://www.unix.com/",
      theavatar:
        '<span  class="alt1" style="font-size:3em;cursor:pointer;float:right;background-color:transparent;margin-right:5px;margin-left:10px;top:0;padding:20px;" ><i style="color:#170072;" class="fas fa-user"></i></span>'
    };
  },
  methods: {
    GetAvatar() {
      if (vbuserId > 0)
        this.theavatar = '<img src="' + vbtheURL + '"  width="60px"></img>';
      else
        this.theavatar =
          '<span  class="alt1" style="font-size:3em;cursor:pointer;float:right;background-color:transparent;margin-right:5px;margin-left:10px;top:0;padding:20px;" ><i style="color:#170072;" class="fas fa-user"></i></span>';
    },
    GetUrl() {
      this.theurl = vbtheURL;
    },
    UpdateTime() {
      this.unixtime = Math.round(new Date().getTime() / 1000);
    },
    OpenMemberPanel() {
      if (vbuserId > 0) openMemberPanel();
      else openLogin();
    }
  },
  created() {
    GetAvatar();
    setInterval(() => {
      this.UpdateTime();
    }, 1000);
  }
});

var navbar =
  new Vue({
    el: "#vue-navbar"
  }) 


  $(function() {
    if (vbtheURL.length > 10)
      $("#avatar").html('<img src="' + vbtheURL + '"  width="60px"></img>');
    else
      $("#avatar").html(
        '<span  class="alt1" style="font-size:3em;cursor:pointer;float:right;background-color:transparent;margin-right:5px;margin-left:10px;top:0;padding:20px;" ><i style="color:#170072;" class="fas fa-user"></i></span>'
      );
  });

Basically, this is working OK; but I used jQuery to make it work.

Does anyone have any suggestions on how to do this using only Vue.js and no jQuery?


OBTW... this methods in the Vue.js code is not being used (or working). That's why I wrote the jQuery code as I could not get the code below to work right in Vue.js:

Code:
GetAvatar() {
      if (vbuserId > 0)
        this.theavatar = '<img src="' + vbtheURL + '"  width="60px"></img>';
      else
        this.theavatar =
          '<span  class="alt1" style="font-size:3em;cursor:pointer;float:right;background-color:transparent;margin-right:5px;margin-left:10px;top:0;padding:20px;" ><i style="color:#170072;" class="fas fa-user"></i></span>';
    },

# 2  
Old 1 Week Ago
Here is my latest try.... works with jQuery, so I deleted my failed code and only post the working Vue.js and jQuery code:

Code:
Vue.component("unix-navbar", {
  template: `<div class="neo-table-border vuenavbar"><div style="margin-bottom:10px;padding-top:13px;"><a class="vuenavbarhome" :href="url">{{name}}</a></div>
              <div id="avatar"  :click="OpenMemberPanel()" style="cursor:pointer;" align="center"></div></div>`,
  data() {
    return {
      unixtime: "",
      name: "The UNIX and Linux Forums",
      url: "https://www.unix.com/"
    };
  },
  methods: {
    OpenMemberPanel() {
      if (vbuserId > 0) openMemberPanel();
      else openLogin();
    }
  }
});

var navbar = new Vue({
  el: "#vue-navbar"
});

$(function() {
  if (vbtheURL.length > 10)
    $("#avatar").html('<img src="' + vbtheURL + '"  width="60px"></img>');
  else
    $("#avatar").html(
      '<span  class="alt1 nav-avatar"><i style="color:#170072;" class="fas fa-user"></i></span>'
    );
});

# 3  
Old 1 Week Ago
I see from the source in the test page link you sent that vbuserId and vbtheURL are both already in scope as Javascript variables, which means you can add them to your data function:

Code:
  data() {
    return {
      vbuserId: vbuserId,
      vbtheURL: vbtheURL,
      unixtime: '',
      name: "The UNIX and Linux Forums",
      url: "https://www.unix.com/",
      theavatar: ''
    };
  },

And then you can use them in your GetAvatar function:
Code:
    GetAvatar() {
      if (this.vbuserId > 0)
        this.theavatar = '<img src="' + this.vbtheURL + '" width="60px"></img>';
      else
        this.theavatar =
          '<span class="alt1" ...></span>';
    },

I guess you need for modify this...
Code:
  created() {
    GetAvatar();

to:
Code:
  created() {
    this.GetAvatar();

The Following User Says Thank You to Scott For This Useful Post:
Neo (1 Week Ago)
# 4  
Old 1 Week Ago
Thanks Scott... Sorry, I cannot get your suggestions to work.

This "almost" works:

Code:
Vue.component("unix-navbar", {
  template: `<div class="neo-table-border vuenavbar"> <div style="margin-bottom:10px;padding-top:13px;"> <a class="vuenavbarhome" :href="url">{{name}}</a></div> <div id="avatar"  v-on:click="OpenPanel()" style="cursor:pointer;" v-html="theicon" align="center"></div> </div>`,
  data() {
    return {
      thehtml:
        '<span  class="alt1 nav-avatar"><i style="color:#170072;" class="fas fa-user"></i></span>',
      name: "The UNIX and Linux Forums",
      url: "https://www.unix.com/"
    };
  },
  computed: {
    id() {
      return vbuserId;
    },
    theURL() {
      return vbtheURL;
    },
    theavatar() {
      return '<img src="' + this.theURL + '" width="60px"></img>';
    },
    theicon() {
      //if (this.id > 0)
      // return '<img src="' + this.theURL + '" width="60px"></img>';
      return this.thehtml;
    }
  },
  methods: {
    OpenPanel() {
      if (this.userId > 0) openMemberPanel();
      else openLogin();
    }
  }
});

var navbar = new Vue({
  el: "#vue-navbar"
});

But the above code only works for the "theicon" text case in the v-html bindings .... if I change it to "theavatar" in the v-html bindings, it breaks.

In this code, the conditional works based on the vbuserId computed to id in vue.... but the problem that remains is that the "theavatar" case, breaks vue when it does the v-html binding.
# 5  
Old 1 Week Ago
The problem in the end was in the vB Template, where $jsGlobalsfromvB was given after the Vue components were loaded, meaning that vbuserId and vbtheURL were not set at the time the components were rendered.

Here's the final, working version:
Code:
Vue.component("unix-navbar", {
  template: `<div class="neo-table-border vuenavbar">
              <div class="flex-item" style="margin-bottom:10px;padding-top:13px;">
                <a class="vuenavbarhome" :href="url">{{name}}</a>
              </div>
              <div id="avatar" class="flex-item"  v-html="theavatar" @click="OpenMemberPanel()" style="cursor:pointer;" align="center"></div>
            </div>`,

  data() {
    return {
      unixtime: '',
      name: "The UNIX and Linux Forums",
      url: "https://www.unix.com/",
      theavatar: ''
    };
  },
  methods: {
    GetAvatar() {
      if ( vbuserId > 0 )
        this.theavatar = '<img src="' + vbtheURL + '" width="60px"></img>';
      else
        this.theavatar =
          '<span class="alt1" style="font-size:3em;cursor:pointer;float:right;background-color:transparent;margin-right:5px;margin-left:10px;top:0;padding:20px;" ><i style="color:#170072;" class="fas fa-user"></i></span>';
    },
    UpdateTime() {
      this.unixtime = Math.round(new Date().getTime() / 1000);
    },
    OpenMemberPanel() {
      if ( vbuserId > 0 )
        openMemberPanel();
      else
        openLogin();
    }
  },
  created() {
    setInterval(() => {
      this.UpdateTime();
    }, 1000);

    this.GetAvatar();
  }
});

var navbar =
  new Vue({
    el: "#vue-navbar"
  });

The Following User Says Thank You to Scott For This Useful Post:
Neo (1 Week Ago)
# 6  
Old 1 Week Ago
Thanks Scott,

On a number of occasions I have had trouble with the order of execution of included Javascript file.

Looking back, it seems obvious that the JS vars from vB global_start should have been before the Vue files. That also seem to explain why I could get it to work as a computed property before.

Thanks for your help sorting this out!

Much appreciated!

Now, I need to figure out why the HTML layout is different between the two conditions (guest vs. logged in user)..... That should be an easy CSS issue (update: confirmed CSS fixed).
# 7  
Old 1 Week Ago
OK... here is the cleaned up code for this header component for the navbar:

Vue.js:

Code:
Vue.component("unix-navbar", {
  template: `<div class="neo-table-border vue-navbar">
              <div  class="vue-site-info">
                <a class="vue-site-link" :href="url">{{name}}</a>
              </div>
              <div id="avatar"  v-html="theavatar" @click="OpenMemberPanel()"></div>
            </div>`,

  data() {
    return {
      name: "The UNIX and Linux Forums",
      url: "https://www.unix.com/",
      theavatar: ""
    };
  },
  methods: {
    GetAvatar() {
      if (vbuserId > 0)
        this.theavatar = '<img id="avatar-img" src="' + vbtheURL + '"></img>';
      else this.theavatar = '<i id="avatar-icon" class="fas fa-user"></span>';
    },
    OpenMemberPanel() {
      if (vbuserId > 0) openMemberPanel();
      else openLogin();
    }
  },
  created() {
    this.GetAvatar();
  }
});

var navbar = new Vue({
  el: "#vue-navbar"
});

CSS:

Code:
<style>
.vue-site-info{
   margin-bottom:10px;
   padding-top:10px;
}
#avatar{
   cursor:pointer;
}
#avatar-img{
   width="60px;
}
.vue-navbar{
   padding:30px 30px 30px 30px;
   display: flex;
   flex-wrap: wrap;
   justify-content: space-between;
}
.vue-site-link{
  font-size:1.2em;
  text-decoration:none;
  
}
#avatar-icon{
  color:#170072;
  font-size:2.5em;
}
</style>

Not sure if it a good idea to use the userid in the client-side code for security reasons, but it's worth a thought. Since the userid is only used to determine which panel is opened and which icon to show, it might be worth a bit of hacking in the dev console to see how this could be exploited.

Otherwise, the main goal of moving the jQuery to Vue.js has been accomplished (thanks and hats off to Scott for helping) and this demo header component for a navbar is basically done. Next would be to move the navbar menu to a Vue.js component and to also move the panels in the header to Vue components.

Obviously, it is much easier to design a new web site from the beginning with Vue other than trying to retrofit the old site into Vue; but on the other hand, having an existing site to modify does provide opportunities to improve the site along the way.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
A simple UNIXtime component in Vue.js Neo Web Programming 1 1 Week Ago 09:30 PM
A Simple Way to Set Meta Tags Using Vue.js Neo Web Programming 0 2 Weeks Ago 12:20 AM
JQuery to Add Code Tags to Selected Text Neo What is on Your Mind? 0 09-10-2018 11:22 AM
JQuery and CSS Flex Code for Responsive Forum Home Page Neo What is on Your Mind? 1 08-11-2018 07:45 AM
JQuery and CSS Flex Code for Responsive WOL Page Neo What is on Your Mind? 0 08-10-2018 12:02 PM
Xclient with xming on MSW to HPUX X11 / VUE 10.20 dlanor HP-UX 1 10-11-2007 01:50 PM
HPUX 10.20 and fx5 which patches for vue dlanor HP-UX 0 10-11-2007 01:33 PM
HP VUE Messaginbg system could not be started inquirer HP-UX 2 12-19-2005 10:41 PM


All times are GMT -4. The time now is 11:40 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password