Wednesday, 15 July 2015

jquery - Converting a button click to require.js style coding -



jquery - Converting a button click to require.js style coding -

i'm getting lost require.js. define seems homecoming in require.js sample pages.

how convert next require.js style coding?

$("button_1").click(function(event) { alert("button 1 has been clicked"); });

as returns nothing.

i think youre missing require part. when youre using amd need "import" youre other modules before can utilize em. @ next illustration shod plenty going. using requrejs+jquery download:

index.html

<!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="path/to/requirejs_jquery" data-main="path/to/main.js" ></script> </head> <body> <button id="button_1">sup</button> </body> </html>

main.js

require(["jquery"],function($){ //import jquery script $(function(){ $("#button_1").click(function(){ alert("#button_1 got click") }) }) })

you mention define aswell, thats when want declare new module can later "import" youre project. lets expand abit , create folder called "my" , place next main script

my/module.js

define(function(){ homecoming { foo: function(){alert("bar")}, barz: function(){return "hey barz"} } })

now can include in main file (or other module matter) this:

main.js

require(["jquery", "my/module"],function($, module){ //import jquery script $(function(){ $("#button_1").click(function(){ alert("#button_1 got click") console.log(module,"sup") }) }) })

jquery requirejs

No comments:

Post a Comment