我有一个与 DOM 密切相关的应用程序。我需要跟踪代表其背后对象的元素的大小和位置。
myViewModel.js
export class MyViewModel {
// my root view model has important properties
// that all other functions and objects need to use
constructor() {
this.importantProperty = 'veryimportant';
this.things = [];
}
// i create things in the view model that are
// represented in the dom
createThing() {
this.things.push({
isAThing: true
});
}
// i do things with things in the view model
// that depend strongly on the root view model
doSomethingWithThing(thing, property) {
thing[property] = `${this.importantProperty}${property}`;
}
// but i need to know all about the dom representation
// of the things in the view model
doAnotherThingWithThing(thing) {
console.log(`the height of the thing is ${thing.height}`);
}
lookAndSeeWhatSizeThisThingIs(element, thing) {
thing.height = element.clientHeight;
thing.width = element.clientWidth;
console.assert('That was easy!');
}
}
myViewModel.html
<template>
<!-- these things can change in size and shape, and I have
no idea what they will be until runtime
<div repeat.for="thing of things"
<!-- so ideally I'd like to call something like this -->
composed.delegate="lookAndSeeWhatSizeThisThingIs($element, thing)">
<img src="img/${$index}.png" />
</div>
</div>
今天有没有办法做到这一点?