Reference Error in Module Definition - Require JS - javascript

I'm trying to get my head around require in simple web applications. I've got everything working fairly well until I try to execute some code inside a 'define' block;
define(['jquery'], function() {
var Playlist = function() {
// bunch of helper code here
};
return Playlist;
});
If i comment out the define wrapper the code works perfectly, but something I'm doing wrong here is causing 'Uncaught ReferenceError: Playlist is not defined'.
What am I missing about 'define' in AMD?

Try
define(['jquery'], function($) {
var Playlist = function() {
// bunch of helper code here
};
return Playlist;
});
And when will you get the Uncaught ReferenceError: Playlist is not defined error? Because Playlist is in your callback function and you can not use it outside of course.

Related

How to inject some string and evaluate it in Vue?

in a SPA, using options API, how (where) to define a function that will be called from an html href?
In the following codepen for demo, everything works fine.
CODEPEN
However, on my Single Page:
<template>
<div v-html="my_div" />
</template>
<script>
/* function MyFunction() {
alert();
}; */
const MyFunction = () => {
alert();
};
/* Just for explanation, because the data comes from the database
export default {
data() {
return {
my_div:'link call function ',
}
}
*/
}
</script>
call the function return the error:
Uncaught ReferenceError: MyFunction is not defined
Apparently, the function is defined after the call
Any idea how to solve this?
Not sure what you're trying to do here but don't evaluate some random string coming from nowhere into a weird format. It will not work, will be clunky, non-debuggable.
If your codebase is legacy and has a lot of debt, refacto it or use jQuery or whatever to hack it even further.

"ReferenceError: <function> is not defined" - javascript modules

When I try to integrate the script type="module"... I get a reference error. My functions inside the "module" script are not found.
Im trying to integrate a printer into an existing webpage, the printer functionality is defined in the file "bpac.js". Its a javascript module using exports.
I have tried using this code in a sample website an everything worked fine. When I use my code in the existing file I get the error. When I dont use the "module" tag everything works fine, but then I cant use the functionality provided by "bpac.js". I have a simple button that when pressed, should execute some testfunction.
<script type = "module">
import * as bpac from './bpac.js';
window.testfunc = async function testfunc() {
const doc = bpac.IDocument;
const pName = await doc.GetPrinterName();
console.log(pName);
}
</script>
...
<button type="button" id="testen" onclick="testfunc()">test</button><br>
...
When I press the Button I get the Message: Uncaught ReferenceError: testfunc is not defined
at HTMLButtonElement.onclick ((index):1)
You are declaring function in wrong way. Use this.
window.testfunc = async function() {
const doc = bpac.IDocument;
const pName = await doc.GetPrinterName();
console.log(pName);
}
Why don't you use addEventListener to add as many listeners as you want?
document.getElementById("testen").addEventListener('click',function () {
console.log("Hello");
// Add your logic inside this function
});

Cannot set property 'classRules' of undefined

am just don't know what happened it was work correctly .... What's most reasons that led us to this error ????
I was trying to run my website locally then this error comes to me from I don't know so what is this error mean and how can I solve it
the error occurs in this code .... actually , its complete website and I'm a beginner with JS and SO so please help me
// disable class and attribute rules defined by jquery.validate
$.validator.classRules = function() {
return {};
};
$.validator.attributeRules = function() {
return {};
};
Your Code tries to access an non existing JQuery namespace. You are either missing some sort of JQuery plugin, or you need to create on your self.
If you would like to create the validator namespace you can use such sample code as described here
(function ($) {
// do not overwrite the namespace, if it already exists
$.validator= $.validator|| {};
$.validator.classRules = function () { return {};}
$.validator.attributeRules = function () { return {};}
})($);

Broserify exported function error: is not a function

I have a module "./lib/common.js", like this:
function foo(text){
console.log(text);
}
function boo(text){
console.log(text);
}
module.exports=foo;
module.exports=boo;
I'm trying to include these functions in another js file using browserify :
var common =require('./lib/common.js');
$(document).ready(function() {
common.foo('hi');
});
Browserify creates the bundle,but on the browser I get
Uncaught TypeError: common.foo is not a function
Ok this was very stupid, I was overwriting my module.exports with the last row module.exports=boo;
With this change it works:
module.export.foo=foo;
module.export.boo=boo;

Write a namespaced function for jQuery that operates on parameters and not selectors

I've been tying to start a library of functions in jQuery to use transversally in my code
I thought that a good way of doing this would be to build a jQuery namespace and add my
methods under that (and provide the namespace for all who would like to contribute).
This is what I came up with:
(function( $ )
{
$.extend
({
mynamespace:
{
myfunction: function(a, b)
{
if (!Math.prototype.newmethod)
{
Math.prototype.newmethod = function(c, d)
{
// newmethod code
}
}
// myfunction code (uses Math.newmethod)
}
}
})
})(jQuery);
Now this worked fine in the small test file where I originally called it (the code did what I wanted it to) but after execution I got the following error:
DOM Exception:
INVALID_CHARACTER_ERR (5)
I ignored it, not being sure where it was coming from nor how to fix it (the few references I found on the web associated the problem with IE9 which I was not using) but when I used my new code site-wide everything started breaking with error messages such as
Uncaught TypeError: undefined is not a function
and
Uncaught TypeError: Object function (c, d) { // newmethod code }
Any ideas as to what I'm doing wrong are welcome
P.S.
The way I call the code described above is:
var temp = $.mynamespace.myfunction(a, b);

Categories