Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for jQuery's inserted event and some quick inserted helpers #574

Merged
merged 1 commit into from Nov 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions util/inserted/inserted_test.js
@@ -0,0 +1,20 @@
(function(){

module("can/util/inserted")

if(window.jQuery){
test("jquery", function(){

var el = $("<div>");

el.bind("inserted", function(){
ok(true,"inserted called")
})

$("#qunit-test-area").append(el)


})
}

})()
25 changes: 25 additions & 0 deletions util/inserted/test.html
@@ -0,0 +1,25 @@
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../lib/qunit/qunit.css"/>
</head>
<body>
<h1 id="qunit-header">inserted Test Suite</h1>

<h2 id="qunit-banner"></h2>

<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-test-area"></div>

<script type="text/javascript" src="../../lib/steal/steal.js"></script>
<script type="text/javascript" src="../../lib/qunit/qunit.js"></script>
<script type="text/javascript">
QUnit.config.autostart = false;
steal("can/util").then("can/test", "can/util/inserted/inserted_test.js", function() {
QUnit.start();
});
</script>
</body>
</html>
51 changes: 35 additions & 16 deletions util/jquery/jquery.js
Expand Up @@ -119,24 +119,43 @@ steal('jquery', 'can/util/can.js', 'can/util/array/each.js', "can/util/inserted"
oldClean(elems);
};

var oldDomManip = $.fn.domManip;
var oldDomManip = $.fn.domManip,
cbIndex;

$.fn.domManip = function(){
var args = can.makeArray(arguments),
isNew$ = $.fn.jquery >= "2.0.0",
cbIndex = isNew$ ? 1 : 2,
callback = args[cbIndex];

args[cbIndex] = function(elem) {
var isFragment = elem.nodeType === 11, //Node.DOCUMENT_FRAGMENT_NODE,
targets = isFragment ? can.makeArray(elem.childNodes) : [elem],
ret = callback.apply(this, arguments);
can.inserted(targets);
return ret;
};
// feature detect which domManip we are using
$.fn.domManip = function(args, cb1, cb2){
for(var i = 1; i< arguments.length; i++){
if(typeof arguments[i] === "function"){
cbIndex = i;
break;
}
}
return oldDomManip.apply(this, arguments)
}
$(document.createElement("div")).append(document.createElement("div"))

$.fn.domManip = (cbIndex == 2 ?
function(args, table, callback){
return oldDomManip.call(this,args,table, function(elem){
if(elem.nodeType === 11){
var elems = can.makeArray(elem.childNodes);
}
var ret = callback.apply(this, arguments);
can.inserted(elems ? elems : [elem]);
return ret;
})
} :
function(args, callback){
return oldDomManip.call(this,args,function(elem){
if(elem.nodeType === 11){
var elems = can.makeArray(elem.childNodes);
}
var ret = callback.apply(this, arguments);
can.inserted(elems ? elems : [elem]);
return ret;
})
})

return oldDomManip.apply(this, args);
};

$.event.special.inserted = {};
$.event.special.removed = {};
Expand Down