question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using jQuery plugin with Moon.js

See original GitHub issue

Hello,

First of all, thank you so much for Moon.js. This is exactly what I needed.

My first project using Moon must use a jQuery plugin. I was able to follow some instructions for Vue.js and adapt it to Moon. (http://vuejsdevelopers.com/2017/05/20/vue-js-safely-jquery-plugin/)

Everything works perfectly except that I can’t pass data from jQuery back to Moon. If you check the link above, there’s a section called “Passing data from jQuery to Vue”. I tried updating a value inside Moon’s data object, but it didn’t work. Here’s my component:

Moon.component("ztree", {
    props: ['id'],
    template: '<ul id="{{id}}" class="ztree"></ul>',
    hooks : {
        mounted: function() {
            var self = this;
            var settings = {
                callback: {
                    onClick: function(event, treeId, treeNode, clickFlag) {
                        self.emit('msg', treeNode.name);
                    }
                }
            }
            var tree = $.fn.zTree.init($(this.$el), settings, zNodes);
        }
    }
}); 

The issue here is inside the onClick callback. “self.emit(‘msg’, treeNode.name);” returns this error:

TypeError: can’t assign to properties of (new String(“Node Name”)): not an object

I also tried with “self.set(‘msg’, treeNode.name);” but nothing gets updated.

Is it a bug or what am I doing wrong?

Thank you for any help you can provide.

Terry

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kbrshcommented, Jul 5, 2017

Thank you! I got the issue, self is referring to the component data, but the msg is defined on the parent Moon instance data.

To overcome this, you can listen for an event on a global event bus, and set the msg property on the parent instance when the event is fired.

var bus = new Moon();
var t = $("#ztree_tmpl").text();

// ztree moon component
Moon.component("ztree", {
  props: ["id"],
  template: '<ul id="{{id}}" class="ztree"></ul>',
  hooks: {
    mounted: function() {
      var self = this;
      var settings = {
        callback: {
          onClick: function(event, treeId, treeNode, clickFlag) {
            bus.emit("msgChange", {
              msg: treeNode.name
            });
          }
        }
      };

      var tree = $.fn.zTree.init($(this.$el), settings, zNodes);
    }
  }
});

const app = new Moon({
  el: "#app1",
  data: {
    msg: "Node name will be here...",
    tree_id: "treeDemo"
  }
});

bus.on('msgChange', function(ctx) {
  app.set("msg", ctx.msg);
});
0reactions
terrybrcommented, Jul 5, 2017

I think it’d be easier for me to attach a zip file with the project. Hopefully it’s OK for me to do that.

moon_test.zip

Read more comments on GitHub >

github_iconTop Results From Across the Web

Display Today's Moon Phase On The Page - jQuery jsRapMoon
jsRapMoon is a super tiny jQuery plugin that allows you to create a graphic of the moon showing the current moon phase on...
Read more >
Moon – Minimal, Blazing Fast UI Library - jQuery Plugins
Moon is a simple front end javascript library heavily inspired by Vue. The library is fast, flexible, and easy to learn. Features.
Read more >
How to use Jquery Plugin with React js and react-router-dom
So what is the way to use Jquery Plugin in a react js app ?? My Code is below: Index.html page. Here i...
Read more >
FitVids.JS - A lightweight, easy-to-use jQuery plugin for fluid ...
A lightweight, easy-to-use jQuery plugin for fluid width video embeds. DownloadOn Github. FitVids.js was built by Chris Coyier and Paravel
Read more >
15 JavaScript libraries, frameworks and jQuery plugins to check out ...
JavaScript frameworks, libraries and even jQuery plugins are astonishingly useful ... Moon is a minimal, blazing fast library for building user interfaces.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found