Skip to content

Commit 4abcf67

Browse files
committed
Emit @layout-ready at a later, correct time
As it was, if one was to investigate the size of GridItems in an @layout-ready event handler, the size was not yet correct. The problem was that layout-ready was emitted in mounted() after a call to onWindowResize(). onWindowResize() set the width with this.width = this.$refs.item.offsetWidth but the width (and hence size generally) change only actually took effect in the watcher for 'width' with: this.eventBus.$emit("updateWidth", this.width); and in addition, that was done inside a this.$nextTick() for some undocumented reason. Now, the layout-ready event is emitted after "updateWidth" has been emitted, so callers can investigate GridItem sizes safely. Care is taken in the width watcher to only emit layout-ready the first time width is set, and layout-ready is emitted in a this.$nextTick() so that the 'updateWidth' has a chance to take effect in the child GridItems before layout-ready is emitted. See comment in GridLayout.vue for more details.
1 parent 0f5d445 commit 4abcf67

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/components/GridLayout.vue

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,39 @@
173173
self.onWindowResize();
174174
});
175175
});
176-
177-
self.$emit('layout-ready', self.layout);
178176
});
179177
});
180178
},
181179
watch: {
182-
width: function () {
180+
width: function (newval, oldval) {
183181
this.$nextTick(function () {
184182
//this.$broadcast("updateWidth", this.width);
185183
this.eventBus.$emit("updateWidth", this.width);
184+
if (oldval === null) {
185+
/*
186+
If oldval == null is when the width has never been
187+
set before. That only occurs when mouting is
188+
finished, and onWindowResize has been called and
189+
this.width has been changed the first time after it
190+
got set to null in the constructor. It is now time
191+
to issue layout-ready events as the GridItems have
192+
their sizes configured properly.
193+
194+
The reason for emitting the layout-ready events on
195+
the next tick is to allow for the newly-emitted
196+
updateWidth event (above) to have reached the
197+
children GridItem-s and had their effect, so we're
198+
sure that they have the final size before we emit
199+
layout-ready (for this GridLayout) and
200+
item-layout-ready (for the GridItem-s).
201+
202+
This way any client event handlers can reliably
203+
invistigate stable sizes of GridItem-s.
204+
*/
205+
this.$nextTick(() => {
206+
this.$emit('layout-ready', self.layout);
207+
});
208+
}
186209
this.updateHeight();
187210
});
188211
},

0 commit comments

Comments
 (0)