Pergunta

For a week i started learning how to use require.js with Backbone.js and Underscore.js.

It's a really difficult stuff, but 3 days ago i read that Underscore will not supporting Require.js(AMD) anymore! Now i'm a bit confuse.

I really like the Concept of Script Loader and dont want to miss it!

Have someone already successfully used a Script Loader with Backbone.js(0.5.3) and Underscore(1.3.0)?

Thank you for helping!

Link : another solution here

Foi útil?

Solução 6

I found a Solution which really work for me : Tim Brayen

define([
'jquery',
'use!underscore',
'use!backbone',

I use it to add jquery plugin, custom js , underscore and backbone( without changing the code!)... but it seems to have a problem with jquery mobile...

you can also take a look to wrap, i have not tested it yet!

Outras dicas

I am currently using underscore 1.3 and Backbone 0.5.3 in my Backbone Boilerplate. You can see what I'm doing there:

https://github.com/tbranyen/backbone-boilerplate

you can still make underscore work with require.js even though it does not natively supports AMD module style.

you can still load it as a normal external javascript source, through a proxy module.

the gist of it is this piece of code:

// Filename: libs/underscore/underscore
// Loads the original underscore file from the libs/underscore folder
define(['order!libs/underscore/underscore-min'], function(){
  // Tell Require.js that this module returns a reference to Underscore
  return _;
});

the full tutorial can be found here: http://backbonetutorials.com/organizing-backbone-using-modules/

Require.js is the best alternative in my opinion, since it includes an optimizer (minification and concatenation) and allows you to decouple your Backbone code into modules.

If you are confused on how to integrate Require.js with the latest versions of Backbone.js and Underscore.js, check out the boilerplate code I created on github. Keep in mind that I am using lodash by John-David Dalton instead of underscore because lodash provides better performance and a custom build process. I am also using the Shim configuration that Require.js 2.0 provided to make non-AMD compatible scripts, like Backbone, AMD/Require.js compatible.

https://github.com/gfranko/Backbone-Require-Boilerplate

I have a few here as well...

https://github.com/jcreamer898/RequireJS-Backbone-Starter
https://github.com/jcreamer898/Savefavs

UPDATE July 7/08/2012

The latest version of RequireJS allows NON-AMD compliant libraries with the following code.

require.config({ 
    'paths': { 
    "underscore": "libs/underscore-min", 
        "backbone": "libs/backbone-min"
    },
    'shim': 
    {
        backbone: {
            'deps': ['jquery', 'underscore'],
            'exports': 'Backbone'
        }
    }   
});

Do you tried labjs or headjs?

http://labjs.com/

http://headjs.com

Ah, theres the yepnope too!

http://yepnopejs.com/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top