Requirejs Simple Application

Source: Internet
Author: User

Project Structure Directory:

HTML page

<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Text/javascript"data-main="/jquery/require/app.js"Src="/jquery/require/require.js"></script> <div>Spring River Tide even sea-level, the sea moon together tidal.        Yan Yan with wave tens of miles, where Spring River no moon Ming!        The river Wanzhuan around the Fragrant meadow, the moon as the flower forest is like sleet, empty flow frost not fly, ting on white sand see.        Jiang Tianyi no dust, Jiaojiao sky solitary Moon round. Who is the moon in the riverside?        When did shine shine the first person?    Life generation is endless, the river month only similar. </div> <div id=" Best"> </div></body>

Parameter configuration:

Requirejs commonly used methods and commands are two, so Requirejs is very simple to use.

    • Require
    • Define

defineis used to define modules, and require is used to load modules and load configuration files.

require.config()Is the core method used to configure the parameter, which receives an object with a fixed format and properties as a parameter, and this object is our configuration object.
In the Configuration objectbaseUrlDefines the base directory, which is associated with the&NBSP; the address of the module in paths is automatically spliced to form the actual address of the module, and when the configuration parameter is passed script When the tag is embedded in an HTML file, the BaseURL default pointing path is the address where the HTML file is located. The value of the
paths   property is also an object that holds the module Key/value value. Where key is the name and ID of the module, the general use of file name to name, and value is the address of the module, in Requirejs, when the module is a JS file, it can be omitted. js extension, such as "index.js" can be directly written as "index" And when the defined module does not need to be associated with &NBSP; baseUrl The value of   can be spliced by &NBSP; "/"   and &NBSP; /http   and &NBSP; . js   to bypass &NBSP; baseUrl settings.

App.js

Require.config ({//baseUrl: '/jquery/bs/'//another folder with the same nameBASEURL:'/jquery/require/', paths: {'jquery':'Http://cdn.webfont.youziku.com/wwwroot/js/jquery-1.10.2.min'   }});//OK reference, no this code, can't use jqueryRequire (["jquery"]);

Require provides a data-main property that is written directly on the script tag introduced into the require.js, and automatically loads the configuration file app.js when the require.js is loaded.

Require (['bjtest'], function (a) {    var" xiaoming ";    Alert (a.addname (name));});
Require (['bjtest'tjtest'], function (A, b) {     var " Xiao Ming " ;    Alert (a.addname (name));    Alert (b.apple (name));});

by data-main loading the portal file, the properties in the configuration object will default to the baseUrl address to the location where App.js is located, which I would recommend, because it is more likely to be quick and easy.

When our project is large enough, I would also recommend the portal file as a normal module, then in this module, according to the different business to load different configuration files.

Bjtest.js

define ({addname:function (n) {$ ("#best"). Text ("life is unfocused to be satisfied"); return "today's test user names are:"+N; }, Fang:function (A, B, c) {return "Beijing-Tianjin New Town:"+ A +"community, high-speed railway station:"+ B +"to Beijing:"+ C +"minutes!"; }});

For more information, please click here.

Requirejs Simple Application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.