我创建了一个自定义的itemTemplateFunction,如此处所述。但是我想访问在DOM渲染过程中定义的属性(如height或clientHeight)。这是为了允许子元素在布局中动态偏移。
目前我已经研究了两种方法:
目前我已经研究了两种方法:
renderComplete
- returned as an element in the itemPromise then return object. The event fired as expected, however the attributes (such as height) did not have set values, so it looks as though the item is not in the DOM at this point.setInterval
- although this would work it's a bad solution as I am relying on a constant time offset, something which ideally I don't want to do.function itemTemplateFunction(itemPromise) { return itemPromise.then(function (item) { var div = document.createElement("div"); var img = document.createElement("img"); img.src = item.data.picture; img.alt = item.data.title; div.appendChild(img); var childDiv = document.createElement("div"); var title = document.createElement("h4"); title.className += "title"; title.innerText = item.data.title; childDiv.appendChild(title); var desc = document.createElement("h6"); desc.innerText = item.data.text; childDiv.appendChild(desc); div.appendChild(childDiv); return { element: div, renderComplete: itemPromise.then(function (item){ return item.ready; }).then(function (item){ var height_a = div.querySelector(".title").clientHeight; var height_b = WinJS.Utilities.query(".title", div).clientHeight; }) } }); };
创建第一个项目时,尽管返回了承诺,但属性仍未最终确定。但是,如果我将item.ready的返回值包装在一个超时间隔为0的第二个承诺中,它会如预期返回。
return {
element: div,
renderComplete: itemPromise.then(function (item){
return WinJS.Promise.timeout(0).then(function () {
return item.ready;
});
}).then(function (item){
var height_a = div.querySelector(".title").clientHeight;
var height_b = WinJS.Utilities.query(".title", div).clientHeight;
})
}