码迷,mamicode.com
首页 > Web开发 > 详细

【转】require.js学习笔记(二)

时间:2014-10-25 22:53:49      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   java   strong   sp   

require.js遵循AMD规范,通过define定义模块,require异步加载模块,一个js文件即一个模块。

一、模块加载require
1.加载符合AMD规范模块

HTML:

<script src="js/require.js" data-main="js/main"></script>

 

MAIN.JS

  require.config({

    baseUrl: "js/lib",

    paths: {

      "jquery": "jquery.min"

    }

  });


require([‘jquery‘], function ($){     // some code here   });

 

2.加载不符合AMD规范模块

require.config({
    shim: {
      ‘underscore‘:{
       exports: ‘_‘  
      },
      ‘backbone‘: {
        deps: [‘underscore‘, ‘jquery‘],
        exports: ‘Backbone‘
      }
    }
  });

 

 

 

二、模块定义define

define([‘math‘, ‘graph‘], 
    function ( math, graph ) {
        return {
            plot: function(x, y){
                return graph.drawPie(math.randomGrid(x,y));
            }
        }
    };
);

 

 

三、require+jquery+domready

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="js/require.js"></script>
</head>
<body>
    <div id="333">123124</div>
    <script> 
    require.config({
        paths: {"jquery": "js/lib/jquery-1.11.1.min",
        "domReady": "js/lib/domReady"
    }  
    });

    require(["domReady!", "jquery"], function() { 
             //alert(‘22‘)
             change();
     }); 

    function change(){
        $(#333).text(5555);
    }
     </script>
    
     
</body>
</html>

 

 

 


原文地址:
http://javascript.ruanyifeng.com/tool/requirejs.html

http://www.ruanyifeng.com/blog/2012/11/require_js.html



【转】require.js学习笔记(二)

标签:style   blog   http   color   io   ar   java   strong   sp   

原文地址:http://www.cnblogs.com/grape1211/p/4050804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!