I'm using momentjs for my backbone project with requirejs. Everthing works fine the following way
require.config({
baseUrl : 'js/',
urlArgs: 'bust=' + ( new Date() ).getTime(),
paths: {
jquery : 'libs/jquery-2.0.3.min',
underscore : 'libs/underscore-min',
backbone : 'libs/backbone-min',
moment : 'libs/moment-2.3.1.min',
momenttweet : 'libs/moment.twitter',
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ['jquery', 'underscore'],
exports: 'Backbone'
},
moment: {
exports: 'moment'
},
momenttweet: {
deps: ['moment'],
exports: 'moment'
}
}
});
The above config breaks when I build using r.js to get .min file
Any solution to make it work.
Source of Moment.twitter
https://github.com/SpiderStrategies/moment.twitter
Or any idea how to make moment.twitter AMD Compatible
I tried wrapping up the moment.twitter within define call and removed the momemnt.twitter from the dependency and the shim
define([
'moment'
], function( moment ) {
var second = 1e3, minute = 6e4, hour = 36e5, day = 864e5, week = 6048e5;
var formats = {
seconds: {
short: 's',
long: ' sec'
...
...
...
return moment;
});
and include the above file as a dependency in files where the moment.twitter is required and call it as moment.twitterShort();
Related
I'm trying to migrate an application from Backbone to Marionette (v3), but I got stuck in a point for two days already.
When I try to run the app in browser, this error shows up in the console (and the screen is blank):
Uncaught SyntaxError: Unexpected token import in backbone.radio.js:1
First line in backbone.radio.js is the import statement for underscore:
import _ from 'underscore';
I use Requirejs as a module loader. This is the configuration in main.js:
require.config({
paths: {
jquery: '../bower_components/jquery/dist/jquery',
underscore: '../bower_components/underscore/underscore',
backbone: '../bower_components/backbone/backbone',
'backbone.radio': '../bower_components/backbone.radio/build/backbone.radio',
'backbone.babysitter': '../bower_components/backbone.babysitter/src/build/backbone.babysitter',
marionette: '../bower_components/marionette/lib/backbone.marionette',
bootstrap: '../bower_components/bootstrap/dist/js/bootstrap',
text: '../bower_components/requirejs-plugins/lib/text'
},
map: {
'*': {
'backbone.wreqr': 'backbone.radio'
}
},
shim: {
jquery: {
exports: '$'
},
underscore: {
exports: '_'
},
backbone: {
deps: [ 'underscore', 'jquery' ],
exports: 'Backbone'
},
marionette: {
deps: [ 'jquery', 'underscore', 'backbone' ],
exports: 'Marionette'
},
bootstrap: {
deps: [ 'jquery' ]
}
}
})
require(['appinstance'], function (app) {
app.start()
})
This is my appinstance.js:
define(function (require) {
var App = require('app')
return new App()
})
And this is my app.js file:
define(function (require) {
var $ = require('jquery')
var _ = require('underscore')
var Backbone = require('backbone')
var Router = require('router')
var Controller = require('controller')
var Marionette = require('marionette')
var CommonHeaderView = require('views/common/header')
return Marionette.Application.extend({
/**
* Define the regions for the application.
*
* #returns {Object}
*/
regions: function () {
return {
header: '#header'
}
},
/**
*
* #param {Object} options
*/
start: function (options) {
var commonHeaderView = new CommonHeaderView()
Marionette.Application.prototype.start.apply(this, [options])
this.header.show(commonHeaderView)
this.Router = new Router({ controller: new Controller() })
Backbone.history.start()
}
})
})
Does anyone know why I'm having this problem?
Unfortunately I ran out of ideas on how to solve this, any help will be greatly appreciated.
P.S.: I use Marionette v3.0.0, Backbone v1.2.3 and Requirejs v2.1.15
That it complains about an import-statement is an indication that you are referencing a source file. Make sure your backbone.radio-path goes to the build file.
I want to create Backbone.js app with Require.js.
But I have an error in console: Uncaught Error: Module name "underscore" has not been loaded yet for context: _. Use require([])
require.config({
baseUrl: 'js/',
paths : {
'jquery' : 'jquery',
'underscore' : 'underscore',
'backbone' : 'backbone',
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
}
}
});
define('app', ['jquery','backbone', 'underscore'], function ($, Backbone, _){
var Model = Backbone.model.extend({});
var model = new Model;
});
require(['app','jquery', 'backbone', 'underscore']);
How I can resolve this problem?
You still need to list underscore as a part of paths so you can refer to it in the shims. Also, not sure what your directory structure looks like, but I'm writing this with the assumption that library code is in the /js/libs directory). Finally, note you won't need to require any of the dependencies of app -- the joy of RequireJS is that it'll figure out what to load.
So...
require.config({
baseUrl: 'js/',
paths : {
'jquery' : 'lib/jquery',
'underscore' : 'lib/underscore',
'backbone' : 'lib/backbone',
},
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
}
});
define('app', ['jquery','backbone', 'underscore'], function ($, Backbone, _){
var Model = Backbone.Model.extend({});
var model = new Model({
foo: 'bar'
});
var app = {
model: model
};
// ...
return app;
});
require(['app'], function(App) {
App.model.get('foo'); // <<=== returns 'bar'
});
The shim is mentioned inside the paths object. I am not sure if that is the problem, but wanted to mention that here.
You need Underscore.js as listed in your require statement.
In my RequestJS config file, I am loading select2 and I'd like to also load locales for it. I currently have this setup :
require.config({
enforceDefine: true,
baseUrl: '/js',
shim: {
'bootstrap#3.1.1': {
deps: [ 'jquery' ],
exports: '$' // export jQuery...
},
'jquery#2.1.1': {
exports: '$'
},
'select2': {
deps: [
'bootstrap',
'lib/select2_locales/select2_locale_fr',
/// NOTE : add locales here
'css!/css/select2/select2.css',
'css!/css/select2/select2-bootstrap.css',
],
exports: '$.fn.select2'
}
},
paths: {
'bootstrap#3.1.1': 'lib/bootstrap.min',
'jquery#2.1.1': 'lib/jquery.min'
},
map: {
'*': {
'css': 'css#0.1.2',
'jquery': 'jquery#2.1.1',
'bootstrap': 'bootstrap#3.1.1'
}
}
});
The problem, here, is that lib/select2_locales/select2_locale_fr needs to be loaded after select2.min.js.
Is there some way I can change this configuration, or add an option, so I can load the locales along with select2, from the require config file?
If that is the case, then select2_locale_fr is not a dependency of select 2, select2 is a dependency of select2_locale_fr
define('locale_specific_select2',['select2'], function(select2){
select2.accomodateNewLocale= function(stuff){
doWhatYouNeedToWith(stuff);
}
locale = determineLocale()
function processLocaleData(localeData){
select2.accomodateNewLocale(localeData);
}
require(['pathTOLocalePlugin'+locale], processLocaleData);
}
I have managed to make my Backbone app work with IE8 - IE Edge... Yey:)
The last stone is IE7 - I get the following Backbone error:
'JSON' is undefined - file: backbone.js
And there is some part of the backbone library code highlighted by IE's console:
s.data=JSON.stringify(i.attrs||e.toJSON(i)
From what I read, I have to load JSON2 or JSON3 library because IE7 does not have it.
Ok, googled and go the following library - JSON 3:
http://cdnjs.cloudflare.com/ajax/libs/json3/3.2.6/json3.min.js
Now, I use require js to load my application, and I have no idea how to integrate this with my app config.
What are it's dependencies, what does it export?
Here is my config for require js:
/**
* Config.
*/
require.config({
paths: {
"jquery" : "libs/jquery",
"underscore" : "libs/underscore",
"backbone" : "libs/backbone",
"text" : "libs/require/text",
"global" : "libs/global",
templates: '../templates'
},
shim: {
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
},
underscore: {
exports: '_'
},
text : {
exports : 'text'
}
},
global : {
deps: ["jquery"],
exports : 'Global'
}
});
require([
'jquery',
'underscore',
'backbone',
'router',
'global'
], function ($, _, Backbone, Router) {
// Compatibility override, add a close function for the Backbone views.
var router = new Router();
Backbone.history.start();
});
Any ideas?
First add the json library to your path config:
paths: {
"json": "http://cdnjs.cloudflare.com/ajax/libs/json3/3.2.6/json3.min.js",
"jquery" : "libs/jquery",
"underscore" : "libs/underscore",
"backbone" : "libs/backbone",
"text" : "libs/require/text",
"global" : "libs/global",
templates: '../templates'
},
The json library has no dependencies and exports a global variable JSON. You will need to make the backbone library depend on the json library, and load the JSON library with a shim config. See below:
shim: {
json: {
exports: 'JSON'
},
backbone: {
deps: ["underscore", "jquery", "json"],
exports: "Backbone"
},
underscore: {
exports: '_'
},
text: {
exports: 'text'
}
}
What is the right way to execute code on Underscore when it gets loaded? I am trying to execute the below code to extend the _ exported namespace automatically when modules require it:
_.mixin(_.str.exports());
The docs are a bit vague but I think I put it in the shim init section? I tried the below but I can't even get a breakpoint to hit in the init:
require.config({
paths: {
jquery: 'libs/jquery/jquery.min',
underscore: 'libs/underscore/lodash.min',
underscorestring: 'libs/underscore/underscore.string.min'
},
shim: {
underscore: {
exports: '_'
}
underscorestring: {
deps: ['underscore'],
init: function (_) {
//Mixin plugin to namespace
_.mixin(_.str.exports());
return _;
}
}
}
});
When I try to do this and use underscorestring, I get this error:
Uncaught TypeError: Object function s(e){return new o(e)} has no
method 'startsWith'
Docs:
http://requirejs.org/docs/api.html#config-shim
http://requirejs.org/docs/api.html#config-callback
I don't know if it's the correct way, but I got it working by inverting things so that underscore depends on underscore.string. Also, this way you don't have to require underscore.string.
require.config({
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'underscore': {
deps: ['underscore.string'],
exports: '_',
init: function(UnderscoreString) {
_.mixin(UnderscoreString);
}
}
},
paths: {
'backbone' : 'lib/backbone',
'jquery' : 'lib/jquery/jquery',
'text' : 'lib/require/text',
'underscore' : 'lib/underscore',
'underscore.string' : 'lib/underscore.string'
}
});
.
Update: 3/14/2014
Underscore.js v1.6.0 brought back AMD compatibility and init() has been removed from RequireJS, so some refactoring is in order. To continue getting Underscore preloaded with Underscore.string I made a mixer module to pull them together.
New Require.js config
requirejs.config({
paths: {
'backbone' : '../lib/backbone/backbone',
'backbone.base' : '../lib/backbone/backbone.base',
'backbone.extensions' : '../lib/backbone/backbone.extensions',
'jquery' : '../lib/jquery/jquery',
'text' : '../lib/require/text',
'underscore' : '../lib/underscore/underscore',
'underscore.mixed' : '../lib/underscore/underscore.mixed',
'underscore.string' : '../lib/underscore/underscore.string'
},
shim: {
'backbone.base': {
deps: ['underscore.mixed', 'jquery'],
exports: 'Backbone'
},
}
});
underscore.mixed
define([
'underscore',
'underscore.string'
], function(_, _s) {
_.mixin(_s.exports());
return _;
});
The final step is to replace all instances of 'underscore' with 'underscore.mixed' in module definitions. I attempted moving Underscore into a file named underscore.base.js and making the regular underscore the mixer (like the Backbone setup) to avoid this step. Underscore, being a named module, disagreed with the plan.
Do you require underscorestring somewhere? Because if it isn't required, it won't be loaded.
I managed to get it working with almost exactly the same code you posted:
require.config({
paths: {
underscore: [
'//raw.github.com/documentcloud/underscore/master/underscore-min'
, 'lib/underscore'
]
, underscorestring: 'https://raw.github.com/epeli/underscore.string/master/dist/underscore.string.min'
}
, shim: {
underscore: { exports: '_' },
underscorestring: {
deps: ['underscore'],
init: function(_) {
_.mixin(_.str.exports());
return _; // guess, this is not needed.
}
}
}
, exclude: ['underscore']
});
require(['underscore', 'underscorestring'], function(_) {
console.log( _.chars("i'm a happy string.") );
});
Battling with this for hours before i understand what i was doing wrong
This is what i did wrong
You should not rename the file underscore.string in main.js
even though in my library i did rename the file in paths i name it back to 'underscore.string'
This is how your main.js should look like
require.config({
paths: {
underscore: 'lib/underscore',
'underscore.string' : 'lib/_string' ,
},
shim: {
underscore: {
exports: '_',
deps: [ 'jquery', 'jqueryui' ]
},
'underscore.string': {
deps: [ 'underscore' ]
},
}
....
You could then either add it as dependency with in your shim like i did for my mixin file
shim: {
mixin : {
deps: [ 'jquery', 'underscore', 'underscore.string' , 'bootstrap' ]
},
Or just define it in your different pages like
/*global define */
define([
'underscore.string'
], function ( ) {
it just work now you can access it through _.str or _.string
This is why you should do it this way and not try to name it something else
on line 663 of underscore.string.js
// Register as a named module with AMD.
if (typeof define === 'function' && define.amd)
define('underscore.string', [], function(){ return _s; });
Which means that it will only register it with AMD require JS if you are defining 'underscore.string'
For Mix in you could just with define
/*global define */
define([
'underscore',
'underscore.string'
], function ( ) {
_.mixin(_.str.exports());