简体   繁体   English

如何从控制器获取固定装置中的记录总数?

[英]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. 将设置ItemsControllercontent属性,为您的属性doSomethingWithTotal一个真实值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用聚合时如何在运行限制之前获取记录总数 - How do I get the total number of records before I run limit when using Aggregation 如何仅获取一次变量中的对象总数? - How do I get total number of objects in a variable only once? 如何获取 RestAPI 响应中收到的总列数? - How do I get total number of columns received in RestAPI response? 获取Algolia索引的记录总数 - Javascript - Get total number of records in index for Algolia - Javascript 在 JavaScript 中对 JSON 数据执行聚合。 我必须得到每所学校的记录总数和学校记录的总分 - Perform an aggregation on JSON data in JavaScript. I have to get the total number of Records for each school and total Marks of School Records Ember.js:完全加载后,如何从视图中的装置访问记录? - Ember.js: How can I access records from a fixture in a view after they have been fully loaded? 如何在其他模块中从其他语言获取记录 - How do I get records from ohter languages in a backend Modul 我如何获得 JavaScript 中的准确总数 - How do i get the accurate total for in JavaScript 如何获得 TypeScript 中数字的总位数? - How can I get a count of the total number of digits in a number in TypeScript? 如何从 javascript 中的 firebase 实时数据库中获取子节点的总数? - How do I fetch the total number of child nodes from my firebase realtime database in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM