litedom icon indicating copy to clipboard operation
litedom copied to clipboard

function outside litedom

Open kethan opened this issue 5 years ago • 14 comments

how to call custom event outside litedom scope?

Eg.

<button @some-click="a()" class="button-outline">DOWN

kethan avatar Aug 02 '19 18:08 kethan

This is a general JavaScript question, but the function is in a different scope than the LiteDom instance. So, you would have to figure out a way to import the function into the LiteDom scope, or attach it to something global like window. What about defining your function in the same script tag where you define the LiteDom instance?

brylie avatar Aug 03 '19 00:08 brylie

Thanks for answering. Firstly how to create custom events that can be called outside litedom scope?

Eg.

https://tencent.github.io/omi/site/docs/index.html#/event?index=1&subIndex=2

kethan avatar Aug 03 '19 06:08 kethan

@kethan if the function is defined outside of the JavaScript scope where the LiteDom instance is defined, you have to import it into the same scope as the LiteDom instance. Otherwise, a LiteDom instance cannot find the function. How is your code organized?

brylie avatar Aug 04 '19 06:08 brylie

Oh ok that means litedom can only call it's scope functions not the outside of it's scope. Then how can I use a litedom component in vue or angular for reusing?

kethan avatar Aug 04 '19 10:08 kethan

that means litedom can only call it's scope functions not the outside of it's scope

This is generally true in JavaScript, in that there is local and global scope:

https://scotch.io/tutorials/understanding-scope-in-javascript

If something is not defined in the global scope, meaning available everywhere, then it needs to be defined or imported into local scope.

In order to import a LiteDom component into a Vue project, you would probably need to define it in its own file and export the LiteDom instance first.

https://coryrylan.com/blog/using-web-components-in-vue

brylie avatar Aug 04 '19 13:08 brylie

Oh nice thanks for the resources.

kethan avatar Aug 04 '19 15:08 kethan

Unable to dispatch custom event.

https://jsfiddle.net/kethan/2cbjnuv5/4/

kethan avatar Aug 09 '19 16:08 kethan

Just define all of your JavaScript in the same script tag.

brylie avatar Aug 11 '19 22:08 brylie

Still not working. Can you check and let me know?

kethan avatar Aug 12 '19 17:08 kethan

The same similar code is working on other similar libraries

kethan avatar Aug 13 '19 22:08 kethan

In the fiddle you show, I get the following console error:

TypeError: this.dispatchEvent is not a function

I think this is referring to the LiteDom instance, not the DOM element. You might need to dispatch the event from an actual DOM element, and possibly listen for the event in a parent DOM element.

brylie avatar Aug 14 '19 08:08 brylie

In the fiddle you show, I get the following console error:

TypeError: this.dispatchEvent is not a function

I think this is referring to the LiteDom instance, not the DOM element. You might need to dispatch the event from an actual DOM element, and possibly listen for the event in a parent DOM element.

If you can edit my code it will be very useful. Thanks

kethan avatar Aug 14 '19 21:08 kethan

Ques.2:

Two-way data binding is not reflected on view on loading the page.

https://jsfiddle.net/kethan/L0xz2q6d/2/

kethan avatar Aug 14 '19 21:08 kethan

For question 1, it would be good to have a semantically correct example from you, as part of due diligence. Perhaps just get a reference to the DOM item directly, rather than using properties of this.

Please consider opening a second support request for question 2, here or on StackOverflow.

brylie avatar Aug 15 '19 07:08 brylie