Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: hufman/ember.js
...
head fork: hufman/ember.js
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
24 packages/ember-handlebars/lib/views/metamorph_view.js
@@ -20,11 +20,25 @@ var DOMManager = {
});
},
- after: function(view, nextView) {
- nextView._insertElementLater(function() {
- var morph = view.morph;
- morph.after(nextView.outerHTML);
- nextView.outerHTML = null;
+ after: function(parentView, view, newView) {
+ newView._insertElementLater(function() {
+ var morph;
+ var nextView;
+ var prevView = view;
+ // Find a previous item that actually exists in the page
+ while (prevView !== null && prevView.get('state') === 'destroyed') {
+ prevView=prevView.get('prevView');
+ }
+ if (prevView === null) {
+ morph = parentView.get('morph');
+ morph.prepend(newView.outerHTML);
+ newView.outerHTML = null;
+ } else {
+ morph = prevView.morph;
+ morph.after(newView.outerHTML);
+ newView.outerHTML = null;
+ }
+
});
},
View
55 packages/ember-handlebars/tests/each_test.js
@@ -70,6 +70,61 @@ test("it updates the view if an item is removed", function() {
assertHTML(view, "Annabelle");
});
+test("it updates the view if an item is replaced", function() {
+ Ember.run(function() {
+ people.removeAt(0);
+ people.insertAt(0, { name: "Kazuki" });
+ });
+
+ assertHTML(view, "KazukiAnnabelle");
+});
+
+test("can add and replace in the same runloop", function() {
+ Ember.run(function() {
+ people.pushObject({ name: "Tom Dale" });
+ people.removeAt(0);
+ people.insertAt(0, { name: "Kazuki" });
+ });
+
+ assertHTML(view, "KazukiAnnabelleTom Dale");
+});
+
+test("can add and replace the object before the add in the same runloop", function() {
+ Ember.run(function() {
+ people.pushObject({ name: "Tom Dale" });
+ people.removeAt(1);
+ people.insertAt(1, { name: "Kazuki" });
+ });
+
+ assertHTML(view, "Steve HoltKazukiTom Dale");
+});
+
+test("can add and replace complicatedly", function() {
+ Ember.run(function() {
+ people.pushObject({ name: "Tom Dale" });
+ people.removeAt(1);
+ people.insertAt(1, { name: "Kazuki" });
+ people.pushObject({ name: "Firestone" });
+ people.pushObject({ name: "McMunch" });
+ people.removeAt(3);
+ });
+
+ assertHTML(view, "Steve HoltKazukiTom DaleMcMunch");
+});
+
+test("can add and replace complicatedly harder", function() {
+ Ember.run(function() {
+ people.pushObject({ name: "Tom Dale" });
+ people.removeAt(1);
+ people.insertAt(1, { name: "Kazuki" });
+ people.pushObject({ name: "Firestone" });
+ people.pushObject({ name: "McMunch" });
+ people.removeAt(2);
+ });
+
+ assertHTML(view, "Steve HoltKazukiFirestoneMcMunch");
+});
+
module("the #each helper", {
setup: function() {
people = Ember.A([{ name: "Steve Holt" }, { name: "Annabelle" }]);
View
29 packages/ember-views/lib/views/collection_view.js
@@ -108,6 +108,15 @@ Ember.CollectionView = Ember.ContainerView.extend(
if (removingAll) { childView.removedFromDOM = true; }
childView.destroy();
}
+
+ // If there is an element before the ones we deleted
+ if (start>0) {
+ childViews[start-1].set('nextView', start<childViews.length ? childViews[start] : null);
+ }
+ // if there is an element after the ones we deleted
+ if (start<childViews.length) {
+ childViews[start].set('prevView', start>0 ? childViews[start-1] : null);
+ }
},
/**
@@ -147,6 +156,12 @@ Ember.CollectionView = Ember.ContainerView.extend(
content: item,
contentIndex: idx
});
+
+ // link together the chain of addedViews
+ if (addedViews.length>0) {
+ view.set('prevView', addedViews[addedViews.length-1]);
+ addedViews[addedViews.length-1].set('nextView', view);
+ }
addedViews.push(view);
}
@@ -158,7 +173,19 @@ Ember.CollectionView = Ember.ContainerView.extend(
addedViews.push(emptyView);
set(this, 'emptyView', emptyView);
}
-
+
+ if (added>0) {
+ // if there is a childview before the ones we're adding
+ if (start>0) {
+ childViews.objectAt(start-1).set('nextView', addedViews[0]);
+ addedViews[0].set('prevView', childViews.objectAt(start-1));
+ }
+ // if there is a childview after the ones we're adding
+ if (start<childViews.length) {
+ childViews.objectAt(start).set('prevView', addedViews[addedViews.length-1]);
+ addedViews[addedViews.length-1].set('nextView', childViews.objectAt(start));
+ }
+ }
childViews.replace(start, 0, addedViews);
},
View
2  packages/ember-views/lib/views/container_view.js
@@ -142,7 +142,7 @@ Ember.ContainerView = Ember.View.extend({
*/
_scheduleInsertion: function(view, prev) {
if (prev) {
- prev.domManager.after(prev, view);
+ prev.domManager.after(this, prev, view);
} else {
this.domManager.prepend(this, view);
}
View
35 packages/ember-views/lib/views/view.js
@@ -227,7 +227,11 @@ Ember.View = Ember.Object.extend(Ember.Evented,
return parent;
}
}).property('_parentView'),
-
+
+ // allow navigation between the next and previous views
+ prevView: null,
+ nextView: null,
+
// return the current view, not including virtual views
concreteView: Ember.computed(function() {
if (!this.isVirtual) { return this; }
@@ -1502,10 +1506,31 @@ var DOMManager = {
});
},
- after: function(view, nextView) {
- nextView._insertElementLater(function() {
- var element = view.$();
- element.after(nextView.$());
+ after: function(parentView, view, newView) {
+ newView._insertElementLater(function() {
+ var nextView;
+ var prevView = view;
+ while (prevView !== null && prevView.get('state') === 'destroyed') {
+ prevView=prevView.get('prevView');
+ }
+ if (prevView === null) {
+ var childViews = parentView.get('childViews');
+ for (var i=0; i<childViews.get('length'); i++) {
+ nextView = childViews[i];
+ if (nextView.get('state') === 'inDOM') {
+ break;
+ }
+ }
+ if (i === childViews.get('length')) {
+ nextView = null;
+ }
+ var element = parentView.$();
+ element.prepend(newView.$());
+ } else {
+ var element = prevView.$();
+ element.after(newView.$());
+ }
+
});
},

No commit comments for this range

Something went wrong with that request. Please try again.