I want to upgrade from requirejs version 2.0.0 to 2.1.5
Here is the code:
define(['jquery', 'test.js'],
function ($, test) {
var test = new $.test({
//options
});
....
});
test.js
(function($) {
var registerEvents = function() {
//dosth
};
$.test = function(options) {
$(document).bind('ready', function() {
registerEvents();
});
...
return test;
}
...
});
In version 2.0.0, requirejs holds the dom ready event till all resources are downloaded, so it worked correctly
When I upgrade to requirejs version 2.1.5, the registerEvents function will never be called.
But supprisingly, if I change:
$(document).bind('ready', function() {
registerEvents();
});
To:
$(document).ready(function() {
registerEvents();
});
It worked fine
So my question is: What are the difference between them?
Edit: I am using jQuery v1.7.2
$(document).on('ready', function(){}) not working
I want to upgrade from requirejs version 2.0.0 to 2.1.5
Here is the code:
define(['jquery', 'test.js'],
function ($, test) {
var test = new $.test({
//options
});
....
});
test.js
(function($) {
var registerEvents = function() {
//dosth
};
$.test = function(options) {
$(document).bind('ready', function() {
registerEvents();
});
...
return test;
}
...
});
In version 2.0.0, requirejs holds the dom ready event till all resources are downloaded, so it worked correctly https://github./jrburke/requirejs/issues/249
When I upgrade to requirejs version 2.1.5, the registerEvents function will never be called.
But supprisingly, if I change:
$(document).bind('ready', function() {
registerEvents();
});
To:
$(document).ready(function() {
registerEvents();
});
It worked fine
So my question is: What are the difference between them?
Edit: I am using jQuery v1.7.2
$(document).on('ready', function(){}) not working
Share Improve this question edited Dec 31, 2014 at 9:45 sharon asked Dec 31, 2014 at 9:27 sharonsharon 1351 silver badge7 bronze badges 13- 1 To everyone answering that there's no difference, can you explain why she got different results? Maybe requirejs treats them differently, even if jQuery itself doesn't. – Barmar Commented Dec 31, 2014 at 9:33
-
2
@mangseth:
bind
is deprecated, not removed. – T.J. Crowder Commented Dec 31, 2014 at 9:35 - 5 "I am using jQuery v1.7.2" Why?!?!?! – T.J. Crowder Commented Dec 31, 2014 at 9:35
-
2
The difference is, as the docs say
This behaves similarly to the ready method but if the ready event has already fired and you try to .on( "ready" ) the bound handler will not be executed.
– blgt Commented Dec 31, 2014 at 9:36 -
3
As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements.
As I read it now, it's not deprecated, just not "preferred" then. – mnsth Commented Dec 31, 2014 at 9:39
1 Answer
Reset to default 8The difference is, as the docs say
There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8. This behaves similarly to the ready method but if the ready event has already fired and you try to .on( "ready" ) the bound handler will not be executed. Ready handlers bound this way are executed after any bound by the other three methods above. [em mine]
.bind
and .on
behave similarly.
This is the only difference between
$( document ).ready( handler )
$().ready( handler ) // (this is not remended)
$( handler )
and
$( document ).on( "ready", handler )
$( document ).bind( "ready", handler )
that's mentioned in the docs, so I'm guessing is the most likely source of your issue