6

RequireJS で複数のページを構成するには? 次のサンプルのように、app.js ですべてのクラスを宣言することは正しいことですか? 宣言するすべてのhtmlファイルがあり<script data-main="src/main" src="src/require.js"></script>ますか?

私が避けたいのは、ユーザーがサイトの最初のページに到達したときにすべてのスクリプトを読み込むことです。

すべての外部依存関係を定義する main.js:

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 ファイル:

define([ "product/ProductSearchView",
         "product/ProductCollection"
         ], function (ProductSearchView,
                      ProductCollection) {
         return {
             start: function() {
                  var products = new ProductCollection();
                  var searchView = new ProductSearchView({ collection: products });
                  products.fetch();
                  return {};
             }
        }
});
4

2 に答える 2

9

既存のモジュール内でファイルを要求できます。たとえば、誰かがリンクをクリックすると、次のことを行う関数をトリガーできます。

// 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();
        });
    });
});
于 2011-06-08T16:42:01.097 に答える
3

これは、これがすべてどのように機能するかの完全な例です。require.jsorder.jsは、アプリのJSファイルと同じディレクトリにあります。

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

test.js(jsフォルダー内)

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();
    });
  }
);

Employee.jsをオンデマンドでロードするapp.js(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(jsフォルダー内):

define('Employee', function () {
  return function Employee(first, last) {
    this.first = first; 
    this.last = last;
    this.wep = function() {
        console.log('wee');
    }
  };
});
于 2011-06-15T15:55:47.953 に答える