Domanda

ho bisogno di caricare jQuery1.7 come modulo, ho visto questo codice di @jrburke :

requirejs.config({
  paths: {
    'jquery' : 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min'
  }
});

require(['jquery'], function($) {
  //$ points to jQuery
});

Non è molto utile per me, perché tutti nome .js sono generati da server-side, le ho prese da php-array.

Così, ho scritto questo:

require(['http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'],
        function($) {
  //$ points to jQuery
});

Ma $ è nullo all'interno di questa funzione.

Aggiorna :

Ecco il mio php-modello che rendono i miei JS-scripts per questa pagina:

<script src="http://requirejs.org/docs/release/1.0.1/minified/require.js">
</script>

<script> 
    require([
        <?php echo "'". implode("',\n\t'", $this->scripts) . "'\n"; ?>
    ], function($){

        console.warn ($); // null ;(

        // loaded jQuery
        window.$ = $;

        // Load main client script for this page
        boot( '<?php echo $this->eprint($this->content_page); ?>' );

    });
</script>

ed è il mio php-array per questa pagina (pag index):

$scripts = array(
    'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js',
    'http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js',
    '/js/libs/jquery.history.js?v=1321687090',
    '/js/libs/coolclock.js?v=1321629683',
    '/js/libs/excanvas.js?v=1321629683',
    '/js/client.modules.js?v=1321703735',
    '/js/client.all.js?v=1322512192',
    '/js/boot.js?v=1322512037',
    '/js/client.index.js?v=1321689884'
);
È stato utile?

Soluzione

Avere il vostro array PHP nella forma:

$jquery = array (
   'jQuery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'
);

Quindi provare:

requirejs.config({
  paths: <?php echo json_encode($jquery) ?>
});

require(['jquery'], function($) {
  //$ points to jQuery
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top