Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] jQuery with moon #222

Closed
herOwOw opened this issue Apr 25, 2018 · 5 comments
Closed

[question] jQuery with moon #222

herOwOw opened this issue Apr 25, 2018 · 5 comments

Comments

@herOwOw
Copy link

herOwOw commented Apr 25, 2018

Hey, i'm using moon cli and after generating the project i'm working with moon router then i face this problem. after including jQuery script into index.html and jQuery click function for a button, if i change the router to /admin and i click on button that being attached already with jQuery's click it wont work, until i refresh the page. any solution?

@herOwOw
Copy link
Author

herOwOw commented Apr 26, 2018

the problem was in the router, since when i clicked on the router-link that lead me to the /admin page, jQuery is still loaded and the function that trigger the button exist in browser's console , so when i tape again on the adresse or refresh somethings happened in the moon router then it works!
here my router setup like this:

const Moon = require("moonjs");
require("./components/main.moon")(Moon);
require("./components/ui/admin/dashboard.moon")(Moon);

const MoonRouter = require("moon-router");
Moon.use(MoonRouter);
const router = new MoonRouter({
  default: "/",
  map: {
    "/": "main",
    "/admin": "dashboard"
  }
});

new Moon({
  el: "#app",
  router,
});

am i messing something?

@kbrsh
Copy link
Owner

kbrsh commented Apr 26, 2018

When Moon Router loads a new page, instead of actually navigating to a new page, it changes the URL and updates the page using JavaScript. The button is likely being replaced with a new one when Moon builds the DOM — this is why Moon is not compatible with JQuery in most cases because it needs full control of the DOM. This is also the case with other frameworks.

I'd suggest attaching the event with m-on so that Moon can update the event listener when it updates the page.

@herOwOw
Copy link
Author

herOwOw commented Apr 26, 2018

thx :) <3

@herOwOw herOwOw closed this as completed Apr 26, 2018
@kbrsh
Copy link
Owner

kbrsh commented Apr 26, 2018

No problem!

@herOwOw
Copy link
Author

herOwOw commented Apr 26, 2018

now it works ,i wrapped jQuery code into function and pass it into onclick native event inside admin moon componenet and it work like a charm :p, thx for your answer and for <3 moon <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants