SORU
15 Kasım 2011, Salı


Ve RequireJS kullanarak alt Çizgi yükleme Omurga

RequireJS ile Omurga ve alt Çizgi (jQuery) yüklemeye çalışıyorum. Omurga ve alt Çizgi en son sürümleri ile biraz zor gibi görünüyor. Bir Çizgi otomatik olarak bir modül olarak kendisini kaydeder, ama Omurga genel olarak kullanılabilir alt Çizgi varsayar. Ayrıca Omurga bir bakıma diğer libs ile tutarsız kılan bir modül olarak kendini kayıt görünmüyor dikkat etmelisiniz. Bu işleri yapabileceğim en iyi main.js :

require(
{
    paths: {
        'backbone': 'libs/backbone/backbone-require',
        'templates': '../templates'
    }
},
[
    // jQuery registers itself as a module.
    'http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js',

    // Underscore registers itself as a module.
    'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.1/underscore-min.js'
], function() {

    // These nested require() calls are just due to how Backbone is built.  Underscore basically says if require()
    // is available then it will automatically register an "underscore" module, but it won't register underscore
    // as a global "_".  However, Backbone expects Underscore to be a global variable.  To make this work, we require
    // the Underscore module after it's been defined from within Underscore and set it as a global variable for
    // Backbone's sake.  Hopefully Backbone will soon be able to use the Underscore module directly instead of
    // assuming it's global.
    require(['underscore'], function(_) {
        window._ = _;
    });

    require([
        'order!http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js',
        'order!app'
    ], function(a, app) {
        app.initialize();
    })
});

İşe yarıyor olsa da, iyileştirici boğulur bahsetmeliyiz. Aşağıdaki alıyorum:

Tracing dependencies for: main
js: "/home/httpd/aahardy/requirejs/r.js", line 7619: exception from uncaught JavaScript throw: Error: Error: Error evaluating module "undefined" at location "/home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js":
JavaException: java.io.FileNotFoundException: /home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js (No such file or directory)
fileName:/home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js
lineNumber: undefined
http://requirejs.org/docs/errors.html#defineerror
In module tree:
    main

Bu işleme daha iyi bir yolu var mı? Teşekkürler!

CEVAP
16 Kasım 2011, ÇARŞAMBA


Güncelleme: 1.3.0 Underscore removed AMD (RequireJS) support versiyonu gibi.

amdjs/Backbone 0.9.1 ve James Burke AMD desteği amdjs/Underscore 1.3.1 çatal (RequireJS sürdürücü) kullanabilirsiniz.

AMD support for Underscore and Backbone hakkında daha fazla bilgi.

// main.js using RequireJS 1.0.7
require.config({
    paths: {
        'jquery': 'libs/jquery/1.7.1/jquery',
        'underscore': 'libs/underscore/1.3.1-amdjs/underscore', // AMD support
        'backbone': 'libs/backbone/0.9.1-amdjs/backbone', // AMD support
        'templates': '../templates'
    }
});

require([
    'domReady', // optional, using RequireJS domReady plugin
    'app'
], function(domReady, app){
    domReady(function () {
        app.initialize();
    });
});

Modüller düzgün kayıtlı ve sipariş eklenti ihtiyacı vardır

// app.js
define([
    'jquery', 
    'underscore',
    'backbone'
], function($, _, Backbone){
    return {
        initialize: function(){
            // you can use $, _ or Backbone here
        }
    };
});

Çizgi Omurgası artık kendi ayakları üzerinde bağımlılıkları alır çünkü aslında isteğe bağlıdır:

// app.js
define(['jquery', 'backbone'], function($, Backbone){
    return {
        initialize: function(){
            // you can use $ and Backbone here with
            // dependencies loaded i.e. Underscore
        }
    };
});

AMD sugar bazıları da şöyle yazabilirsiniz:

define(function(require) {
    var Backbone = require('backbone'),
        $ = require('jquery');

    return {
        initialize: function(){
            // you can use $ and Backbone here with
            // dependencies loaded i.e. Underscore
        }
    };
});

Yapılandırma. doublecheck inşa doktoru hata ile ilgili: Yol yapılandırmanızı kapalı olduğunu varsayıyorum. Eğer directory setup similar to the RequireJS Docs varsa kullanabilirsiniz:

// app.build.js
({
    appDir: "../",
    baseUrl: "js",
    dir: "../../ui-build",
    paths: {
        'jquery': 'libs/jquery/1.7.1/jquery',
        'underscore': 'libs/underscore/1.3.1-amdjs/underscore',
        'backbone': 'libs/backbone/0.9.1-amdjs/backbone',
        'templates': '../templates'
    }, 
    modules: [
        {
            name: "main"
        }
    ]
})

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BASS212M

    BASS212M

    15 Temmuz 2009
  • ChasesAndCrashes

    ChasesAndCra

    31 Temmuz 2009
  • Top Gear

    Top Gear

    27 Mart 2006