如何在路线中使用`model`?
How to use `model` in the route?
我不明白以下哪种方式在 Ember 工作流程中更有意义。什么是清洁剂?
最好有一个model
这样的代码?
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')
};
}
});
还是完全不使用 model
更好?
app/routes/index.js
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')
}
});
或者两种方式都可以,我不应该担心这个?
第一个密码肯定是正确的"Ember way"。 categories
和 products
将作为属性出现在您的控制器模型上,因此您可以从您的模板中执行以下操作:
{{#each category in model.categories}}<!-- use category -->{{/each}}
我什至不确定您将如何使用您的第二个代码。控制器根本无法联系到他们。
除了语法之外,这两种方式没有任何显着差异。第二部分要注意的一件事是您有语法错误。不需要 categories :
部分,直接 return 来自 store.find
的承诺即可。此外,您应该使用 .property()
将这两个函数设为 computed properties
,以便可以像对待模型一样对待它们。现在它们只是函数,所以它们不能被观察到,这在大多数情况下是你想要的。您的第二个选项应该如下所示:
categories: function() {
return this.store.find('category');
}.property(),
products: function() {
return this.store.find('product');
}.property()
我同意@panta 的观点,第一种方式是"Ember"。当您只想获取模型的相关部分而不是整个模型时,我可以看到第二种方法很有用。
假设您的模型是一个仓库,其中包含产品和产品类别,还有名称、地址等信息。
{
name: "My Warehouse",
size: 10,
address: "123 St"
// ... other properties
products: // ...
categories: // ...
}
然后您可以方便地仅从存储中获取您在特定控制器中实际需要的数据(同时您在其他地方加载实际的仓库模型):
categories: function() {
return this.store.all('category'); // use .all to get all records currently in the store
}.property(),
products: function() {
return this.store.all('product'); // use .all to get all records currently in the store
}.property()
我不明白以下哪种方式在 Ember 工作流程中更有意义。什么是清洁剂?
最好有一个model
这样的代码?
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')
};
}
});
还是完全不使用 model
更好?
app/routes/index.js
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')
}
});
或者两种方式都可以,我不应该担心这个?
第一个密码肯定是正确的"Ember way"。 categories
和 products
将作为属性出现在您的控制器模型上,因此您可以从您的模板中执行以下操作:
{{#each category in model.categories}}<!-- use category -->{{/each}}
我什至不确定您将如何使用您的第二个代码。控制器根本无法联系到他们。
除了语法之外,这两种方式没有任何显着差异。第二部分要注意的一件事是您有语法错误。不需要 categories :
部分,直接 return 来自 store.find
的承诺即可。此外,您应该使用 .property()
将这两个函数设为 computed properties
,以便可以像对待模型一样对待它们。现在它们只是函数,所以它们不能被观察到,这在大多数情况下是你想要的。您的第二个选项应该如下所示:
categories: function() {
return this.store.find('category');
}.property(),
products: function() {
return this.store.find('product');
}.property()
我同意@panta 的观点,第一种方式是"Ember"。当您只想获取模型的相关部分而不是整个模型时,我可以看到第二种方法很有用。
假设您的模型是一个仓库,其中包含产品和产品类别,还有名称、地址等信息。
{
name: "My Warehouse",
size: 10,
address: "123 St"
// ... other properties
products: // ...
categories: // ...
}
然后您可以方便地仅从存储中获取您在特定控制器中实际需要的数据(同时您在其他地方加载实际的仓库模型):
categories: function() {
return this.store.all('category'); // use .all to get all records currently in the store
}.property(),
products: function() {
return this.store.all('product'); // use .all to get all records currently in the store
}.property()