Underscore.string with RequireJS

I am trying to use Underscore and Underscore.string with RequireJS .

Content main.js:

require.config({
    paths: {
        'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
        'underscore-string': '//cdnjs.cloudflare.com/ajax/libs/underscore.string/2.3.0/underscore.string.min',
    },
    shim: {
        'underscore': {
            exports: '_'
        },
        'underscore-string': {
            deps: ['underscore'],
            exports: '_s'
        },
    }
});

var modules = ['underscore-string'];

require(modules, function() {
    // --
});

The browser sees _, but does not see _s- it is undefined.

Ideally, I want to be under the Underscore _and Underscore.string under _.str, but _and _salso beautiful. How can i do this?

Versions: RequireJS 2.1.5, Underscore 1.4.4, Underscore.string 2.3.0

Note. Thanks to @jgillich, make sure that these paths have two slashes ( //cdnjs.cloudfare.com/...), otherwise the browser will think that the URL is relative to the server and Firebug will throw:

Error: Script error
http://requirejs.org/docs/errors.html#scripterror
+5
4

. - RequireJS Underscore.string cdnjs.com, Github. , - commit 9df4736.

:

require.config({
    paths: {
        'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
        'underscore-string': '//raw.github.com/epeli/underscore.string/master/dist/underscore.string.min',
    },
    shim: {
        'underscore': {
            exports: '_'
        },
        'underscore-string': {
            deps: ['underscore'],
        },
    }
});

var modules = ['underscore', 'underscore-string'];

require(modules, function(_) {
    // --
});

Underscore.string _.str.

: 16 2013 CDNJS ​​ .

+10

, ,

underscore.string main.js

, 'underscore.string'

main.js

require.config({
paths: {
    underscore: 'lib/underscore', 
    'underscore.string' : 'lib/_string' ,
},
shim: { 
    underscore: {
        exports: '_', 
        deps: [ 'jquery', 'jqueryui' ]
    }, 
    'underscore.string': { 
        deps: [ 'underscore' ]
    },
} 
....

, mixin

shim: { 
    mixin : {
        deps: [ 'jquery',  'underscore', 'underscore.string' , 'bootstrap'  ] 
    },  

,

/*global define */
define([    
    'underscore.string'
], function ( ) {   

, _.str _.string

, -

663 underscore.string.js

  // Register as a named module with AMD.
  if (typeof define === 'function' && define.amd)
    define('underscore.string', [], function(){ return _s; });

, , AMD JS, "underscore.string"

+2

, "underscore.string" . , undererscore.string

underscore.string( , ):

 // Register as a named module with AMD.
  if (typeof define === 'function' && define.amd)
    define('underscore.string', [], function(){ return _s; });

So, for me, the only working configuration is:

require.config({
    paths: {
        'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
        'underscore.string': '//raw.github.com/epeli/underscore.string/master/dist/underscore.string.min',
    },
    shim: {
        'underscore': {
            exports: '_'
        },
        'underscore.string': {
            deps: ['underscore'],
        },
    }
});

var modules = ['underscore', 'underscore.string'];

require(modules, function(_) {
    // --
});
+1
source

Here's the working code using the Requirejs "order" plugin, also includes jQuery, and everything loads without conflict:

requirejs.config({
    baseUrl: "assets",
    paths: {
        order: '//requirejs.org/docs/release/1.0.5/minified/order',
        jquery: 'http://code.jquery.com/jquery-2.1.0.min',
        underscore: '//underscorejs.org/underscore-min',
        underscorestring: '//raw.githubusercontent.com/epeli/underscore.string/master/dist/underscore.string.min',
        underscoremixed: 'js/underscore.mixed' // Create separate file 
    },
    shim: {
        underscore: { exports: '_' },
        underscorestring: { deps: ['underscore'] }
    }
});
require(['order!jquery','order!underscoremixed'], function($,_) {
    // test
    console.log( _.capitalize('capitalized text') );
});

Inside js / underscore.mixed.js enter the following ...

define(['underscore','underscorestring'], function() {
    _.mixin(_.str.exports());
    return _;
});

Hooray!:)

+1
source

All Articles