开发者

How to structure multiple pages with RequireJS

开发者 https://www.devze.com 2023-03-09 04:17 出处:网络
How to structure multiple pages with RequireJS? Is, like the following sample, declaring every class in app.js is the right thing to do? Has every html file to declare the <script data-main=\"src/m

How to structure multiple pages with RequireJS? Is, like the following sample, declaring every class in app.js is the right thing to do? Has every html file to declare the <script data-main="src/main" src="src/require.js"></script>?

What I want to avoid is loading all the script when a user reach the first page of a site.

main.js defining all external dependencies:

require(
    {
        baseUrl:'/src'
    },
    [
        "require",
        "order!http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js",
        "order!http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js",
        "order!http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js",
        "order!http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"
    ], 
    function (require) {
        require(["app"], function (app) {
            app.start();
        });
    }
);

app.js file defining every component:

define([ "product/ProductSearchView",
         "product/ProductCollection"
         ], function (ProductSearchView,
                      ProductCollection) {
         return 开发者_StackOverflow中文版{
             start: function() {
                  var products = new ProductCollection();
                  var searchView = new ProductSearchView({ collection: products });
                  products.fetch();
                  return {};
             }
        }
});


You can require files within your existing module. So say when someone clicks a link you could trigger a function that does the following:

// If you have a require in your other module
// The other module will execute its own logic
require(["module/one"], function(One) {
    $("a").click(function() {
        require(["new/module"]);
    });
});

// If you have a define in your other module
// You will need to add the variable to the require
// so you can access its methods and properties
require(["module/one"], function(One) {
    $("a").click(function() {
        require(["new/module"], function(NewModule) {
            NewModule.doSomething();
        });
    });
});


This is a complete example of how this all works; require.js and order.js are in the same directory as the app's JS files.

<html> 
  <head>  
    <script data-main="js/test" src="js/require.js"></script>
  </head> 
  <body> 
    <button>Clickme</button>
  </body> 
</html>

test.js (in js folder)

require(
  {
    baseUrl:'/js'
  },
  [
    "order!//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js",
    "order!//ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"
  ], 
  function () {
    require(["app"], function (app) {
      app.start();
    });
  }
);

app.js (in js folder) with a on-demand load of Employee.js:

define([], function () {
  return {
    start: function() {
      $('button').button();
      $('button').click(function() {
        require(['Employee'], function(Employee) {
          var john = new Employee('John', 'Smith');
          console.log(john);
          john.wep();
        });
      });

      return {};
    }
  }
});

Employee.js (in js folder):

define('Employee', function () {
  return function Employee(first, last) {
    this.first = first; 
    this.last = last;
    this.wep = function() {
        console.log('wee');
    }
  };
});
0

精彩评论

暂无评论...
验证码 换一张
取 消