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
# 8  
Old 1 Week Ago
Also, for completeness, I changed the GetAvatar() method to a computed property and it works as well. I personally like this method because it is a few less lines of code and it's easy to use the dev tools Vue debugger to look at the computed data:

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/"
    };
  },
  computed: {
    theavatar: function() {
      if (vbuserId > 0)
        return '<img id="avatar-img" src="' + vbtheURL + '"></img>';
      else return '<i id="avatar-icon" class="fas fa-user"></span>';
    }
  },
  methods: {
    OpenMemberPanel() {
      if (vbuserId > 0) openMemberPanel();
      else openLogin();
    }
  }
});

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

Vue debugger:

# 9  
Old 1 Week Ago
Here is another question I have. Given this Vue code below, why does id not update when I manually update vbuserId in the dev console?



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/",
      id: vbuserId
    };
  },
  computed: {
    theavatar: function() {
      if (this.id > 0)
        return '<img id="avatar-img" src="' + vbtheURL + '"></img>';
      else return '<i id="avatar-icon" class="fas fa-user"></span>';
    }
  },
  watch: {
    id() {
      console.log("The id has changed!");  /* This never logs as expected */
    }
  },
  methods: {
    OpenMemberPanel() {
      if (this.id > 0) openMemberPanel();
      else openLogin();
    }
  }
});

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

# 10  
Old 1 Week Ago
I also tried variations of this code, with no joy getting vbuserId changes in the dev console to be reactive in Vue;

Code:
Vue.prototype.$id = vbuserId;
Vue.prototype.$avatar = vbtheURL;

Vue.component("unix-navbar", {
  props: ["myid"],
  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/"
    };
  },
  computed: {
    theavatar: function() {
      if (this.id > 0)
        return '<img id="avatar-img" src="' + this.$avatar + '"></img>';
      else return '<i id="avatar-icon" class="fas fa-user"></span>';
    },
    id: function() {
      return this.$id;
    }
  },
  methods: {
    OpenMemberPanel() {
      if (this.id > 0) openMemberPanel();
      else openLogin();
    }
  }
});

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

Tried various Vue.nextTick() and other Vue methods, but no joy either. And also tried many variations of:

Code:
vm.$el.myid = vbuserId;
vm.myid = vbuserId;

adding [ICODE]myid[/ICODE ]to data() in the component, but no joy.

Again, I must be missing something very basic in Vue
# 11  
Old 1 Week Ago
Added vbuserId to the component:
Code:
  data() {
    return {
      ...
      vbuserId: vbuserId
    };
  },
  computed:{
    ...
    id: function() {
      return  this.vbuserId;
    }

I guess there's no need for a computed value there, as it's not actually computing anything.
Turning jQuery Code into Vue.js-vbuserid_change-png  

# 12  
Old 1 Week Ago
What you propose did not change the fact it is not reactive to the JS var.

When I change vbuserId in the dev console, it it worked (was reactive), the avatar would change; but it does not change.

If the conditional in vue is from data(), computed: or from a method: all will change the value internal to Vue, but what I am trying to
do is get Vue to react to changes in the variable in the console which simulates a change outside of Vue.

IMHO, it is not related to data(), computed: or from a method directly, and there is something that needs to be added to get Vue to react (bind) to an external change outside of vue.
Turning jQuery Code into Vue.js-screen-shot-2018-11-09-4-50-18-pm-png  

# 13  
Old 1 Week Ago
Update:

After looking into this; it seems that it's not really in the design of Vue.js to bind and react to Javascript variables outside the Vue instance sandbox.

No big deal... we can manage without this capability and indeed, as Scott has pointed out to me, there are probably good security reasons for this design (Vue.js as a sandbox).

However, in the interest of being complete, I asked this question over at the Vue forums:

Bind Vue.js variable to external Javascript variable for reactivity - Get Help - Vue Forum
# 14  
Old 1 Week Ago
My "security" suggestion was just a guess.. probably not a very good one, or the right one for the wrong reasons! Encapsulation is good!

When the Vue instance is instantiated the execution context in JS knows about vbuserId, but that doesn't bind it in any way to the Vue instance. I've googled it to death, and can't find a way to make that happen. But it's certainly easy to update both, outside of the context of the Vue components with, for example, a function:
Code:
var vm  =
  new Vue({
    el: "#vue-navbar"
  });

var u = vm.$children["0"];

function updateId(newId) {
  vbuserId = newId || 0;
  u.id = vbuserId;
}

Of course, to anyone who is concerned, this vbuserId is only used for the purposes of displaying an avatar (and only either your own or the default, silhouette one, and doesn't "make you" that user
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 07:49 PM.

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