检测 hasMany 属性 何时被填充但没有结果
Detecting when a hasMany property has been populated with no results
我想做的事情看起来很简单,但我不太清楚如何去做。
我正在使用 Emberjs 2.8。
我有以下模型 async: true
属性:
models/my-model.js
import DS from 'ember-data';
export default DS.Model.extend({
childModels: DS.hasMany('otherModel', {async: true})
});
我有一个显示以下模型之一的组件:
/components/my-component.js
import Ember from 'ember';
export default Ember.Component.extend({
theModel: // model of type my-model here
})
可能my-model.childModels
没有任何记录。如果是这种情况,我想做的是在模板中显示 "no children found" 消息,但我只想在 之后 对服务器进行异步调用并且返回空响应。所以我希望模板看起来像这样:
templates/components/my-component.hbs
<div>
{{#if theModel.childModels.length == 0}}
// display message saying there are no children
{{else}}
{{#each theModel.childModels as |child}}
// do something
{{/each}}
{{/if}}
</div>
棘手的部分是知道关系是否已经从服务器填充。我不能只检查 .length == 0
,因为在进行异步调用之前这是真的,我不希望 DOM 不必要地来回切换。我需要的是 theModel.childModels.isLoaded
之类的东西,但在查看文档后我不确定如何完成此操作。
有人可以建议一种方法吗?任何意见,将不胜感激。非常感谢!
好吧,我现在觉得很傻,因为我找到了答案,毕竟它在文档中。 PromiseManyArray
使用的 PromiseProxyMixin 确实有 isPending
属性。所以我可以像这样创建一个计算 属性:
childModelsEmpty: Ember.computed('theModel.childModels', function () {
return !this.get('theModel.childModels.isPending') && this.get('theModel.childModels.length') === 0;
}),
这会告诉我服务器是否已被击中并返回空响应。
我想做的事情看起来很简单,但我不太清楚如何去做。
我正在使用 Emberjs 2.8。
我有以下模型 async: true
属性:
models/my-model.js
import DS from 'ember-data';
export default DS.Model.extend({
childModels: DS.hasMany('otherModel', {async: true})
});
我有一个显示以下模型之一的组件:
/components/my-component.js
import Ember from 'ember';
export default Ember.Component.extend({
theModel: // model of type my-model here
})
可能my-model.childModels
没有任何记录。如果是这种情况,我想做的是在模板中显示 "no children found" 消息,但我只想在 之后 对服务器进行异步调用并且返回空响应。所以我希望模板看起来像这样:
templates/components/my-component.hbs
<div>
{{#if theModel.childModels.length == 0}}
// display message saying there are no children
{{else}}
{{#each theModel.childModels as |child}}
// do something
{{/each}}
{{/if}}
</div>
棘手的部分是知道关系是否已经从服务器填充。我不能只检查 .length == 0
,因为在进行异步调用之前这是真的,我不希望 DOM 不必要地来回切换。我需要的是 theModel.childModels.isLoaded
之类的东西,但在查看文档后我不确定如何完成此操作。
有人可以建议一种方法吗?任何意见,将不胜感激。非常感谢!
好吧,我现在觉得很傻,因为我找到了答案,毕竟它在文档中。 PromiseManyArray
使用的 PromiseProxyMixin 确实有 isPending
属性。所以我可以像这样创建一个计算 属性:
childModelsEmpty: Ember.computed('theModel.childModels', function () {
return !this.get('theModel.childModels.isPending') && this.get('theModel.childModels.length') === 0;
}),
这会告诉我服务器是否已被击中并返回空响应。