带有cli的ember.js中的夹具数据在哪里

时间:2014-05-23 23:09:09

标签: javascript ember.js

我试图在使用cli生成的ember应用中使用灯具数据。我无法找到我的数据。检查员显示我有一个名为post的模型,但没有任何内容。我不确定为什么它不起作用所以发布我认为相关的文件...

模型/ post.js

var Post = DS.Model.extend({
    title: DS.attr('string'),
    content: DS.attr('string'),
    publishDate: DS.attr('date')
});

Post.reopenClass({
    FIXTURES: [
        {
            id: 1,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        },
        {
            id: 2,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        }
    ]
});

export default Post;

router.js

var Router = Ember.Router.extend({
  location: ENV.locationType
});

Router.map(function() {
    this.resource('posts', { path: '/' });
});

export default Router;

路由/ index.js

export default Ember.Route.extend({
    model: function() {
        return this.store.find('post');
    }
});

控制器/ posts.js

var PostsController = Ember.ArrayController.extend({

});

export default PostsController;

模板/ posts.hbs

<p>Test</p>
<ul>
    {{#each}}
        <li>
            {{title}}
        </li>
    {{/each}}
</ul>

我认为这个问题是具体的ember-cli。我之前有过使用Ember App Kit的灯具,但想与ember-cli合作。我添加了适配器并尝试更改固定装置的声明方式:

适配器/ post.js

var PostAdapter = DS.FixtureAdapter.extend({});

export default PostAdapter;

已更改 models / post.js

var Post = DS.Model.extend({
    title: DS.attr('string'),
    content: DS.attr('string'),
    publishDate: DS.attr('date')
});

Post.FIXTURES = [
        {
            id: 1,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        },
        {
            id: 2,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        }
    ];

export default Post;

这仍然无法奏效。灰烬检查员显示具有正确字段(id,标题,内容publishDate)但没有实际数据的帖子。

4 个答案:

答案 0 :(得分:19)

我需要添加我的灯具适配器:

适配器/ application.js中

export default DS.FixtureAdapter.extend({});

然后它与returesnClass版本的灯具一起使用:

模型/ post.js

var Post = DS.Model.extend({
    title: DS.attr('string'),
    content: DS.attr('string'),
    publishDate: DS.attr('date')
});

Post.reopenClass({
    FIXTURES: [
        {
            id: 1,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        },
        {
            id: 2,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        }
    ]
});

export default Post;

答案 1 :(得分:2)

除了上面的reopenClass方法调用之外,还需要向Brocfile.js添加EmberData的导入

你可以这样做:

app.import({
  development: 'vendor/ember-data/ember-data.js',
  production:  'vendor/ember-data/ember-data.prod.js'
}, {
  'ember-data': [
    'default'
  ]
});

感谢这篇精心设计的文章,帮助我实现这一点:http://www.blakeerickson.com/posts/2014/06/17/ember_cli_todomvc_tutorial

答案 2 :(得分:0)

接受的答案是正确的,但你可以用ember-cli方式生成文件:

ember g adapter application

它生成:

installing adapter
  create app/adapters/application.js
installing adapter-test
  create tests/unit/adapters/application-test.js

答案 3 :(得分:-1)

您需要将灯具定义为:

Post.FIXTURES = [
        {
            id: 1,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        },
        {
            id: 2,
            title: "Writing a blog in Ember",
            content: "I am writting a blog",
            publishDate: "05/22/2104"
        }
];

您还必须设置ApplicationAdapter:

App.ApplicationAdapter = DS.FixtureAdapter;

这是一个示例http://emberjs.jsbin.com/yutas/1/editguide section