【问题标题】:how use model in emberJS pod structure?如何在 emberJS pod 结构中使用模型?
【发布时间】:2015-05-12 06:19:30
【问题描述】:

我在 ember 中使用 pods 结构。我想创建一个数据服务和一个下拉组件。哪种方式最好? 我的结构基于 ember-cli。

这些是我以前做过的:

这是我的 pod 结构:

下拉/route.js 中的代码:

import Ember from 'ember';

export default Ember.Route.extend({
   model: function() {
     var store = this.store;
     return  $.ajax({
        url: 'http://.../api/Category/Categories', //my api url
        type: 'get'
       }).then(function(items) {            
         items.forEach(function(item, index, enumerable) {
            store.push('model',{
                id:item.Id,
                name: item.Title,
                value: item.Id.toString()
            });
         });
      });
     }
  });

下拉列表/model.js 中的代码:

import Ember from 'ember-data';

export default Ember.Model.extend({
    name: Ember.attr('string'),
    value: Ember.attr('string')   
});

在我的下拉页面中,我有这个错误:

错误:找不到“模型”的模型

我该如何解决?

哪种方式更好?使用上述方式或使用重新适配器和序列化程序? 如果第二种方法更好,请帮助我怎么做。谢谢

【问题讨论】:

    标签: ember.js ember-data asp.net-web-api ember-cli


    【解决方案1】:

    您的 pod 结构有点错误。 Pods 结构用于在您的应用程序中包装资源。根据ember-cli docs,您应该以资源方式拆分目录,其中每个目录可以包含三个文件:

    • controller.js
    • route.js
    • template.hbs

    或者在组件的情况下:

    • component.js
    • template.hbs

    我能给你的建议是:

    • 将您的模型移动到pods 上方的models 目录(并可能更改名称,因为Model 可能会导致一些奇怪的行为)
    • 将下拉组件留在原处
    • 我不确定,但如果您的dataservice 是服务而不是组件,请将其移至services 目录,在pods 上方

    模型应该在models 目录中定义,而不是在 pods 结构中。此外,看起来您正在使用 ember-data(使用 store),但您通过扩展 Ember.Model 创建模型。我不确定是否更合适的方法是扩展DS.Model

    【讨论】:

    • 感谢您的回答,但我的结构是正确的,尝试在 commandlilne 中创建资源,它将每个模型添加到其路由的文件夹中,与我的相同。
    【解决方案2】:

    这就是你的 pods 文件夹应该如何结束(组件示例):

      ├── app
      │  ├── pods
      │  │  └── components
      │  │     └── x-foo
      │  │     │  ├── component.js
      │  │     │  └── template.hbs
      │  │     └── x-bar
      │  │        ├── component.js
      │  │        └── template.hbs
    

    (模型示例)

      ├── app
      │  ├── pods
      │  │  └── foo
      │  │     │  ├── controller.js
      │  │     │  ├── route.js
      │  │     │  └── template.hbs
      │  │  └── bar
      │  │     │  ├── controller.js
      │  │     │  ├── route.js
      │  │     │  └── template.hbs
    

    详细了解“pod”结构here

    【讨论】:

      【解决方案3】:

      我改变了我的 route.js : store.push('model',...)store.push('dropdown',...) 而且效果很好。

      【讨论】:

      • 很高兴听到这个,不知道它还加载模型。谢谢!
      猜你喜欢
      • 2015-11-08
      • 2016-08-23
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多