我的問題有點奇怪,因爲我不明白這是如何工作的。 我有一個項目顯示在我的網頁上,當用戶點擊一個,這將觸發一個動作事件,也將收到被點擊的產品(對象)。Emberjs - 逐個渲染畫廊圖像
當動作處理程序被觸發時,我打電話loadGallery
(見下面),通常應該在我的模板中逐個渲染我的圖像。 SelectedItem是我的控制器的一個屬性。
loadGallery: function(device) {
var that = this;
var images = device.images.map(function(item, idx) {
return new Ember.RSVP.Promise(function(resolve) {
if (item.url) {
resolve(item);
}
else {
resolve(that.imageStore.readImage(that, item));
}
})
.then(function(image) {
var imageURLs = device.imageURLs.slice(0, device.imageURLs.length);
imageURLs[idx] = image.url;
//that.set('selectedItem.imageURLs.'+idx, image.url);
that.set('selectedItem.imageURLs', imageURLs);
that.set('selectedItem.images.' + idx, image);
return image;
});
});
return Ember.RSVP.Promise.all(images);
},
我在這裏做什麼,就是我替換每個URL的陣列device.images
內由OBJET是這樣的:{ key: 'someKey', url: 'url' }
但是當它有我的模板不刷新..
,我設法使它工作的唯一方法,它等待所有圖像完成加載,刷新這樣的屬性:
return Ember.RSVP.Promise.all(images).then(function(imgs){
that.set('selectedItem.images', imgs);
});
爲什麼這是工作,但不是一個一個的形象?
[編輯]這是我的模板:
<div class="gallery">
{{#each img in selectedItem.images}}
{{#if img.url}}
<div href="{{unbound img.url}}" class="gallery-item" style="background-image: url({{unbound img.url}})"></div>
{{else}}
<div class="gallery-item" style="background-image: url(images/loading.png)"></div>
{{/if}}
{{/each}}
</div>
這裏是readImage功能:
this.readImage = function(controller, key) {
var that = this;
return new Ember.RSVP.Promise(function(resolve) {
controller.store.findQuery('image', { key: key })
.then(function(image) {
var result = undefined;
var content = image.content[0];
var data = content && content._data;
if(data) {
if (iOSVersion()[0] >= 7 || isBlobSupported()) {
result = {
key: key,
url: dataURIToURL(data.base64, key)
}
} else {
result = {
key: key,
url: data.base64
}
}
}
resolve(result);
})
.catch(function(e) {
console.log(e);
resolve();
});
});
};
是這條線基本上是什麼都不做,但?它是否將財產設置爲現在的狀態? 。我要猜測是的 – Kingpin2k
好吧,如果它工作正常,我應該能夠在我的模板中看到結果。所以,我認爲這沒有做任何事情。是的,它將屬性設置爲已有內容,如更新,修改對象內的數組。 – SuperMarco
實際上這條線,'device.images.set(idx +'',image);',或任何正在觀看每個圖像,是問題。你能展示你的模板嗎?或計算屬性是依賴於它? – Kingpin2k