-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix test cases. #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Like testTwo, but we detach the top level view. | ||
// This leads to a single DOM op instead | ||
// of `n+1` DOM ops. | ||
// | ||
|
||
(function() { | ||
var btn = document.querySelector('#test4'); | ||
|
||
btn.addEventListener('click', function() { | ||
|
||
// Create our list, and append it to the test node | ||
var $list = $('<ul></ul>'); | ||
var lis = new Array(10000); | ||
for(var i=0; i<lis.length; i++) { | ||
var $li = $('<li></li>').addClass('l'+i); | ||
lis[i] = $li; | ||
$list.append($li); | ||
} | ||
test.$el.append($list); | ||
|
||
// Rather than create a new tree from a template, I'm going ahead and cloning it | ||
var $listClone = $list.clone(); | ||
|
||
// Kick off the test | ||
test.start('three'); | ||
|
||
var $span = $('<span>').insertAfter($list); | ||
$list.detach(); | ||
|
||
// Loop through the children in the cloned tree, replacing | ||
// them with cloned versions of the elements in the existing | ||
// tree. This causes 0 ops | ||
$listClone.children().each(function(index, li) { | ||
$(li).replaceWith(lis[index]); | ||
}); | ||
|
||
// The only op is the replace | ||
$list.empty().append($listClone); | ||
$span.replaceWith($list); | ||
|
||
test.stop(); | ||
}); | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,11 @@ | |
|
||
// Create our list, and append it to the test node | ||
var $list = $('<ul></ul>'); | ||
for(var i=0; i<10000; i++) { | ||
$list.append($('<li></li>').addClass('l'+i)); | ||
var lis = new Array(10000); | ||
for(var i=0; i<lis.length; i++) { | ||
var $li = $('<li></li>').addClass('l'+i); | ||
lis[i] = $li; | ||
$list.append($li); | ||
} | ||
test.$el.append($list); | ||
|
||
|
@@ -23,15 +26,17 @@ | |
// Kick off the test | ||
test.start('two'); | ||
|
||
$list.contents().detach(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A very crucial line :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very. See Using the "render" and "rendered" events, you can get within ~5ms of the placeholder approach I put in Test 4, all at the Dev's discretion. I didn't want to do this by default, because someone is invariably going to to to globally query while it's detached... view.on('render', function() {
this._$span = Backbone.$('<span>').insertAfter(this.$el);
this.$el.detach();
});
view.on('rendered', function() {
this._$span.replaceWith(this.$el);
delete this._$span;
}); |
||
|
||
// Loop through the children in the cloned tree, replacing | ||
// them with the children in the existing tree. This gives | ||
// us 4 removal ops | ||
$listClone.children().each(function(index, li) { | ||
$(li).replaceWith(test.$el.find('.'+li.className)); | ||
$(li).replaceWith(lis[index]); | ||
}); | ||
|
||
// The 5th op is the replace | ||
test.$el.find('ul').replaceWith($listClone); | ||
test.$el.find('ul').append($listClone.contents()); | ||
|
||
test.stop(); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want the clone to deeply clone data and events, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, let me update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it defaults to the value of
withDataAndEvents
. 😃There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, would ya look at that!