2

需要帮助让 Ember-Data 与 Zend Rest 一起工作。

一开始,我对 Zend Framework 很熟悉,所以 Rest Adapter 很容易设置。telnet 的请求表明它可以工作,并且响应也具有格式良好的 HTTP 代码。

Ember Data 的设置稍微复杂一些。我用 Ubuntu 安装了一个虚拟机,安装了 Ruby 1.9.3,git clone编辑了 ember-data 存储库并用 rake 生成了 JS 文件。我还安装了捆绑器来解决所有依赖项。似乎工作没有错误。这对我来说是第一次。我对红宝石不熟悉。

不幸的是,它似乎不起作用。在我的测试应用程序中,我用 firebug 看到了其余请求。反应看起来也不错。但是对象仍然是空的。

响应:

[{"id":"1","user":"testuser","password":"123","mail":"my@me.com","role":"GUEST","active":"1","hash":null,"last_login":null}]

响应标头:

Cache-Control       no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection          Keep-Alive
Content-Encoding    gzip
Content-Length      121
Content-Type        application/json; charset=utf-8
Date                Wed, 20 Jun 2012 10:49:38 GMT
Expires             Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive          timeout=15, max=99
Pragma              no-cache
Server              Apache
Vary                Accept-Encoding
X-Powered-By        PHP/5.3.13
X-UA-Compatible     IE=Edge,chrome=1

我的应用程序:

    // my script
    App = Em.Application.create();

    App.store = DS.Store.create({
      revision: 4,
      adapter: DS.RESTAdapter.create({ bulkCommit: false, namespace: 'rest' })
    });

    App.User = DS.Model.extend({
        id: DS.attr('number'),
        user: DS.attr('string'),
        password: DS.attr('string'),
        mail: DS.attr('string'),
        role: DS.attr('string'),
        active: DS.attr('number'),
        hash: DS.attr('string'),
        last_login: DS.attr('date')
    });

    App.postsController = Em.ArrayController.create({
        content: App.store.findAll(App.User)
    });


    // my html page
    <script type="text/x-handlebars">
        {{#each App.postsController}}
           <li>{{user}}</li>
        {{/each}}
    </script>

我究竟做错了什么?我不确定我的 ember-data.js 是否正常工作。

4

1 回答 1

7

https://github.com/emberjs/data/issues/132

DS.RESTAdapter 需要一个根元素。如:

{
  users: [{
    "id":"1",
    "user":"test user",
    "password":"123",
    "mail":"my@me.com",
    "role":"GUEST",
    "active":"1",
    "hash":null,
    "last_login":null
  }]
}

这是因为 DS.RESTAdapter 支持侧加载。不幸的是,它是不可配置的。解决此要求的唯一方法是推出您自己的适配器。

于 2012-06-20T14:24:34.013 回答