[英]How do I get the total number of records in a fixture from a controller?
I'd like to get the total number of records in my fixture from my controller. 我想从控制器中获取固定装置中的记录总数。 Here is my fixture code: 这是我的灯具代码:
FIXTURE 固定装置
App.Items.FIXTURES = [
{
id: 1
},
{
id: 2
},
{
id: 3,
}
];
In this case, there are 3 total records in the fixture. 在这种情况下,固定装置中总共有3条记录。 How do I get that total in my controller? 如何在我的控制器中获得总计?
If you are using something like this: 如果您使用的是这样的东西:
App.ItemsRoute = Ember.Route.extend({
model: function() {
return this.store.find('items');
}
});
The store.find
returns a Ember.PromiseProxyMixin instance, so you can observe the isFulfilled
property to know when the data is loaded: store.find
返回一个Ember.PromiseProxyMixin实例,因此您可以观察isFulfilled
属性来了解何时加载数据:
App.ItemsController = Ember.ArrayController.extend({
doSomethingWithTotal: function() {
var length = this.get('model.length');
// do something with de length
}.observes('model.isFulfilled')
});
Like this sample http://jsfiddle.net/marciojunior/UeCWV/ 像这个样本http://jsfiddle.net/marciojunior/UeCWV/
You can ask ArrayController for the property content
and it will return an array, which gives you access to length
. 您可以向ArrayController询问属性content
,它将返回一个数组,使您可以访问length
。
Here's an updated Fiddle: http://jsfiddle.net/bYSjD/ 这是更新的小提琴: http : //jsfiddle.net/bYSjD/
App.ItemsController = Ember.ArrayController.extend({
doSomethingWithTotal: function() {
return this.get('content.length')
}.property('content')
});
The way this works is that App.ItemsRoute
. 这种工作方式是App.ItemsRoute
。 model
returns a Promise to find items, and when that completes, another method of ItemsRoute
is called: setupController
. model
返回一个Promise来查找项目,完成后, ItemsRoute
另一个方法称为: setupController
。
Ember will automatically handle setupController
for you, which populates a controller with the returned model data. Ember将自动为您处理setupController
,该控制器将使用返回的模型数据填充控制器。 The content
property of ItemsController
will be set, giving your property doSomethingWithTotal
a real value. 将设置ItemsController
的content
属性,为您的属性doSomethingWithTotal
一个真实值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.