【发布时间】:2016-05-17 07:02:34
【问题描述】:
我正在向 API 发送一个远程图像 URL。 API 进行一些图像处理,然后上传到 AWS S3。如果成功则返回response.data.attributes['image-url']中图片的url
我的控制器有:
imageUrl: Ember.computed.oneWay('model.imageUrl');
actions: {
uploadImage(imageUrl) {
Ember.$.ajax({
url: `api/v1/users/1/update-image`,
type: 'PUT',
data: {
data: {
attributes: {
'image-url': imageUrl
}
}
}
})
.success(response => {
this.set('imageUrl', response.data.attributes['image-url']);
})
.error(response => {
console.log(response);
});
}
}
我的模板有:
<img src={{imageUrl}}>
API 总是返回类似https://project1.imgix.net/products/1/image.jpg 的东西。即使image.jpg 是一个全新的图像。
Ember 不会重新渲染 img 元素。我必须手动刷新才能在浏览器上看到新图像。
我有什么方法可以告诉 Ember img 元素包含一个新图像并且应该重新渲染它?
我应该考虑其他方法吗?
编辑
所以我先尝试将imageUrl 设置为null,然后等待几秒钟设置实际的图片网址。似乎有效,但这是解决这种情况的正确方法吗:
.success(response => {
this.set('imageUrl', null);
Ember.run.later((() => {
this.set('imageUrl', response.data.attributes['image-url'] + '?t=' + (new Date()).getTime());
}), 3000);
})
我想知道这是否与在某个特定的 Ember 运行循环中运行 this.set 有关。
【问题讨论】:
-
查看 ember chrome 扩展中的控制器时,
imageURL是否设置正确?您是否尝试过直接设置model.imageURL而不是imageURL?我还没有使用oneWay,所以我不确定你是否可以通过它设置值。 -
.success(response => { this.set('imageUrl', response.data.attributes['image-url'] + '?t=' + (new Date()).getTime ()); })
-
@pbogut 这似乎适用于
Ember.run.later的组合。查看我的编辑 -
尝试在设置值后通知属性更改。 this.notifyPropertyChange('imageURL');