Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 如何在路线中使用“model”?_Ember.js_Ember Cli - Fatal编程技术网

Ember.js 如何在路线中使用“model”?

Ember.js 如何在路线中使用“model”?,ember.js,ember-cli,Ember.js,Ember Cli,我不明白以下哪种方式在Ember工作流中更有意义。什么是清洁剂 最好在本代码中有一个类似的模型 app/routes/index.js import Ember from 'ember'; export default Ember.Route.extend({ model: function() { return { categories: this.store.find('category'), products: this.store.find('pro

我不明白以下哪种方式在Ember工作流中更有意义。什么是清洁剂

最好在本代码中有一个类似的
模型

app/routes/index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return {
      categories: this.store.find('category'),
      products: this.store.find('product')
    };
  }
});
import Ember from 'ember';

export default Ember.Route.extend({
  categories: function() {
    return categories: this.store.find('category')
  },
  products: function() {
    return products: this.store.find('product')
  }
});
还是完全不要像本代码中那样使用
模型

app/routes/index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return {
      categories: this.store.find('category'),
      products: this.store.find('product')
    };
  }
});
import Ember from 'ember';

export default Ember.Route.extend({
  categories: function() {
    return categories: this.store.find('category')
  },
  products: function() {
    return products: this.store.find('product')
  }
});

或者这两种方法都很好,我不必担心这个问题?

第一个代码绝对是正确的“余烬方法”<代码>类别和
产品
将作为属性显示在控制器的模型上,因此您可以从模板执行以下操作:

{{#each category in model.categories}}<!-- use category -->{{/each}}
{{{#model.categories}}{{/each}}

我甚至不知道你将如何使用你的第二个代码。控制器根本无法到达它们。

除了语法之外,这两种方法没有任何显著差异。第二部分需要注意的一点是,您有一个语法错误。不需要
类别:
部分,您可以直接从
商店返回承诺。查找
。此外,您应该使用
.property()
将这两个函数转换为
计算属性
,以便可以像处理模型一样处理它们。现在,它们只是函数,所以它们无法被观察到,这在大多数情况下是您想要的。以下是第二个选项的外观:

categories: function() {
    return this.store.find('category');
}.property(),

products: function() {
    return this.store.find('product');
}.property()
我同意@panta的观点,第一种方式是“余烬”方式。我可以看到,当您只想获得模型的相关部分,而不是整个模型时,第二种方法非常有用

假设您的模型是一个仓库,它有产品和产品类别,还有名称、地址等

{
    name: "My Warehouse",
    size: 10,
    address: "123 St"
    // ... other properties
    products: // ...
    categories: // ...
} 
然后,您可以方便地从存储中仅获取特定控制器中实际需要的数据(同时在其他地方加载实际的仓库模型):