Using Require.js to load jquery-ui is giving me problems - jquery-ui's dependencies don't see m to be working. Here is the error:
Uncaught TypeError: Cannot read property 'ui' of undefined
Here are the two files:
main.js
require.config({
baseUrl: '/git-cake-skeleton/js',
paths: {
'jquery': 'lib/jquery-1.10.2',
'jqueryui': 'lib/jquery-ui-1.10.3.min',
'bootstrap': 'lib/bootstrap.min'
},
shim: {
'jqueryui': {
exports: '$',
deps: ['jquery']
},
'bootstrap': {
deps: ['jquery']
}
} });
require([
'jquery',
'widgets/demowidget'
], function ($) {
$(document).ready(function() {
// This is where we bind our widgets to stuff on the page. All the real logic happens inside widgets!
$(".app-js-demowidget").demowidget();
});
} );
demowidget.js
// Example of simple plugin inside Require.js framework
define(['jquery', 'jqueryui'], function ($) {
$.widget('skeleton.demowidget', {
options: {
},
_init: function() {
this.element.click(function(e){
alert('Hello world');
});
}
});
});
File structure:
|-js
| main.js
|---lib
| bootstrap.min.js
| jquery-1.10.2.js
| jquery-ui-1.10.3.min.js
| require.js
|---widgets
| demowidget.js
edit: as expected, switching 'jqueryui' with 'bootstrap' in demowidget.js gives the following error:
Bootstrap requires jQuery
You first need to define the jquery dependency:
define('jquery', [], function () { return root.jQuery; });
Then you can use 'jquery' to load other libs depending on jQuery.
Related
I am trying to load bootstrap and kendo-ui using requirejs but they both depend on jquery to be loaded first.
Currently all three scripts are loading async (from CDNs) with:
require.config({
paths: {
"jquery": [
"https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min",
"libs/jquery"
],
'bootstrap': [
'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min',
'libs/bootstrap'
],
'kendo': [
'http://cdn.kendostatic.com/2014.1.416/js/kendo.ui.core.min',
'libs/kendo'
]
}
})
require(['jquery', 'bootstrap', 'kendo'], function () {
$('body').html('hi!')
})
How do get it to not load other scripts until jquery is loaded completely?
I assume there is a nicer way than:
require(['jquery'], function () {
require(['bootstrap', 'kendo'], function () {
$('body').html('hi!')
})
})
If that even works (off top of my head).
Try to use shim-option like,
require.config({
paths: {
'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min',
'bootstrap': 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min',
'kendo': 'http://cdn.kendostatic.com/2014.1.416/js/kendo.ui.core.min'
},
// Use shim for plugins that does not support ADM
shim: {
'bootstrap': ['jquery'],
'kendo': ['jquery']
}
});
Also, refer the example-of-jquery-shim
My jQuery library is getting loaded, but I'm getting an undefined error for any other modules I'm importing. Most of them have a jQuery dependency, which I shim in. Can anyone tell me why all my other modules are undefined?
requirejs.config({
baseUrl: 'js',
shim: {
'jqueryui': ['jquery'],
'dynatree': ['jquery'],
'noty' : ['jquery']
},
paths: {
jquery: 'vendor/jquery',
jqueryui: 'vendor/jqueryui',
dynatree: '../includes/dynatree/jquery.dynatree.min',
jsPlumb: '../includes/jsPlumb/dist/js/jquery.jsPlumb-1.5.5-min',
noty: '../includes/noty/packaged/jquery.noty.packaged.min'
}
});
requirejs(['jquery', 'jqueryui', 'dynatree', 'jsPlumb', 'noty'],
function ( $, jqueryui, dynatree, jsPlumb, noty ) {
console.log('hello', $, noty, jsPlumb, dynatree);
});
jquery ui doesn't export anything, it uses the same jquery symbol. I'd say the same applies for the rest of the libraries.
Even in the case they'd export something, it won't work because you're not setting the export symbol for your shims. Here you have an example of a shim with export
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
//Once loaded, use the global 'Backbone' as the module value.
exports: 'Backbone'
},
}
I'd replace your code by
requirejs(['jquery', 'jqueryui', 'dynatree', 'jsPlumb', 'noty'],
function ($) { // $ is the only symbol that needs to be used
});
I load AngularJs and jQuery using RequireJs in nodeJs framework.
That is main.js
require.config({
paths: {
angular: 'vendor/angular.min',
bootstrap: 'vendor/twitter/bootstrap',
jquery: 'vendor/jquery-1.9.0.min',
domReady: 'vendor/require/domReady',
underscore: 'vendor/underscore.min'
},
shim: {
angular: {
deps: [ 'jquery' ],
exports: 'angular'
}
}
});
require([
'app',
'angular-boot'
], function() {
});
in app.js
define(['angular'], function (angular) {
return angular.module('MyApp', []);
})
and in angular-boot.js
define([ 'angular', 'domReady' ], function (angular, domReady) {
domReady(function() {
angular.bootstrap(document, ['MyApp']);
});
});
In my html file I have only this line, in order to declare and use requirejs.
Not ng-ap or anything else.
<script data-main="js/main" src="js/require.js"></script>
The problem is that sometimes runs, sometimes not.
The error when it doesn't run is
Uncaught Error: No module: MyApp
If there is any thought about it, I would really appreciate it.
Thank you very much.
In your shim, you need to setup the app as a dependency to angular-boot. Your angular-boot file depends on app (where MyApp is defined), and because the load order for these two files is not specified, sometimes angular-boot loads before the app and thus produces that error.
To remedy, just update your shim as such:
shim: {
angular: {
deps: [ 'jquery' ],
exports: 'angular'
},
'angular-boot': {
deps: ['app']
}
}
and than, since you don't need to include the 'app' explicitly your require call can be reduced to:
require(['angular-boot']);
I can't seem to figure out how to load Bootstrap via RequireJS. None of the examples that I found worked for me.
Here is my shim:
require.config({
// Sets the js folder as the base directory for all future relative paths
baseUrl: "./js",
urlArgs: "bust=" + (new Date()).getTime(),
waitSeconds: 200,
// 3rd party script alias names (Easier to type "jquery" than "libss/jquery, etc")
// probably a good idea to keep version numbers in the file names for updates checking
paths: {
// Core libsraries
// --------------
"jquery": "libs/jquery",
"underscore": "libs/lodash",
"backbone": "libs/backbone",
"marionette": "libs/backbone.marionette",
// Plugins
// -------
"bootstrap": "libs/plugins/bootstrap",
"text": "libs/plugins/text",
"responsiveSlides": "libs/plugins/responsiveslides.min",
'googlemaps': 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDdqRFLz6trV6FkyjTuEm2k-Q2-MjZOByM&sensor=false',
// Application Folders
// -------------------
"collections": "app/collections",
"models": "app/models",
"routers": "app/routers",
"templates": "app/templates",
"views": "app/views",
"layouts": "app/layouts",
"configs": "app/config"
},
// Sets the configuration for your third party scripts that are not AMD compatible
shim: {
"responsiveSlides": ["jquery"],
"bootstrap": ["jquery"],
"backbone": {
// Depends on underscore/lodash and jQuery
"deps": ["underscore", "jquery"],
// Exports the global window.Backbone object
"exports": "Backbone"
},
"marionette": {
// Depends on underscore/lodash and jQuery
"deps": ["backbone", "underscore", "jquery"],
// Exports the global window.Backbone object
"exports": "Marionette"
},
'googlemaps': { 'exports': 'GoogleMaps' },
// Backbone.validateAll plugin that depends on Backbone
"backbone.validate": ["backbone"]
},
enforceDefine: true
});
and here is how I call Bootstrap:
define([
"jquery",
"underscore",
"backbone",
"marionette",
"collections/Navigations",
'bootstrap',
],
function($, _, Backbone, Marionette, Navigations, Bootstrap){
The error that I get is this:
Uncaught Error: No define call for bootstrap
Any ideas on how to get this resolved?
I found a working example here:
https://github.com/sudo-cm/requirejs-bootstrap-demo
I followed it to get my code to work.
According to that demo, especially app.js, you simply make a shim to catch Bootstrap's dependency on jQuery,
requirejs.config({
// pathsオプションの設定。"module/name": "path"を指定します。拡張子(.js)は指定しません。
paths: {
"jquery": "lib/jquery-1.8.3.min",
"jquery.bootstrap": "lib/bootstrap.min"
},
// shimオプションの設定。モジュール間の依存関係を定義します。
shim: {
"jquery.bootstrap": {
// jQueryに依存するのでpathsで設定した"module/name"を指定します。
deps: ["jquery"]
}
}
});
and then mark Bootstrap as a dependency of the app itself, so that it loads before app.js.
// require(["module/name", ...], function(params){ ... });
require(["jquery", "jquery.bootstrap"], function ($) {
$('#myModalButton').show();
});
Finally, since app.js is the data-main,
<script type="text/javascript" src="./assets/js/require.min.js" data-main="./assets/js/app.js"></script>
Bootstrap's JS is guaranteed to load before any application code.
Bootstrap lib does not return any object like jQuery, Underscore or Backbone: this script just modifies the jQuery object with the addition of new methods. So, if you want to use the Bootstrap library, you just have to add in the modules and use the jquery method as usual (without declarating Bootstrap like param, because the value is undefined):
define([
"jquery",
"underscore",
"backbone",
"marionette",
"collections/Navigations",
"bootstrap",
],
function($,_,Backbone,Marionette,Navigations){
$("#blabla").modal("show"); //Show a modal using Bootstrap, for instance
});
I found it was sufficient to add the following to my requirejs.config call (pseudocode):
requirejs.config({
...
shim: {
'bootstrap': {
deps: ['jquery']
}
}
});
I like to use Require.Js ORDER plugin, what it does? Simply loads all your Libraries in order, in this case you won't get any errors, ohh and bootstrap depends on jQuery, so we need to use shim in this case:
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',
bootstrap: '//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min'
},
shim: {
'bootstrap': {
deps:['jquery']
}
}
});
require(['order!jquery', 'order!bootstrap'], function($) {
});
I am having difficulty trying to get signalr to work with requirejs. This is my code I have but I get the following error:
_Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>._
Code:
<script src="~/Scripts/jquery.signalR-1.0.0-rc2.js"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
// requirejs configuration setup
requirejs.config({
baseUrl: '#string.Format("{0}://{1}{2}Scripts/modules", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))', // this might need to change as it depends on the number of / in the url...?
paths: {
'jquery': '../jquery-1.9.0',
'bootstrap': '../bootstrap',
'knockout': '../knockout-2.2.1',
'noext': '../noext',
'sigr': '../jquery.signalR-1.0.0-rc2'
},
shim: {
"sigr": {
deps: ['jquery']
},
"noext!signalr/hubs": {
deps: ['sigr']
}
}
});
Does anyone have any ideas as to why or how I can get this to work?
I was able to get require to work with requirejs..I followed the tutorial on http://requirejs.org/docs/jquery.html on how to plugin jquery and added signal references for this to work
require(["jquery", "jquery.alpha", "jquery.beta","jquery.signalr-1.0.0-rc2","/signalr/hubs"],
function($) {
}
);
I think you have to modify your configuration to do either of the following
Option1:
paths: {
'jquery': '../jquery-1.9.0',
'bootstrap': '../bootstrap',
'knockout': '../knockout-2.2.1',
'noext': '../noext',
'sigr': '../jquery.signalR-1.0.0-rc2'
'hubs': '/signalr/hubs'
},
Option2:
shim: {
"sigr": {
deps: ['jquery']
},
"noext!**/**signalr/hubs": {
deps: ['sigr']
}
}