2

我已经构建了一个RESTAdapter可以使用的couchdb,并且正在对其进行测试以确保它可以正常工作,到目前为止一切似乎都很好,但我的测试路线似乎还有其他问题。

对不起,这太长了,我可能应该为它设置一个小提琴......我以前从未这样做过,但现在会调查它......

我已经构建了以下(相关)的东西:

App.Thing = DS.Model.extend({
    rev: DS.attr(),
    price: DS.attr()
});

App.Things<Index>Route = Ember.Route.extend({
    model: function () {
        return this.get('store').findAll('thing');
    }
});

(我试过ThingsRouteIndex没有任何改变)

App.Router.map

this.resource('things', function() {
    this.route('thing', { path: ':thing_id'})
});

App.ApplicationAdapter = DS.RESTAdapter.extend

buildURL: function(type, id) {
    id = id || '_all_docs?include_docs=true';
    return this._super(type, id);
}

App.ApplicationSerializer = DS.RESTSerializer.extend

extractArray: function(store, type, payload, id, requestType) {
    root = type.typeKey;
    root = Ember.String.pluralize(root);
    newJSON = {};

    newJSON[root] = payload.rows.map(function(row) {
        return row.doc;
    });
    payload = newJSON;

    console.log(payload);
    return this._super(store, type, payload, id, requestType);
},

normalize: function(type, hash, property) {
    var json = { id: hash._id, rev: hash._rev};
    delete hash._id;
    delete hash._rev;

    for (var prop in hash) {
        json[prop] = hash[prop]; 
    }

    console.log(json);
    return this._super(type, json, property);
}

这个模板:

<script type="text/x-handlebars" data-template-name="things/index">
    {{#each thing in things}}
        {{thing.rev}}
        {{thing.price}}
    {{else}}
        Empty.
    {{/each}}
</script>

中的console.logsextractArraynormalize两者都显示以下格式完美且正确的 json:

Object {things: Array[3]}
Object {id: "8117701d38cf9a1112ce8ed38000064d", rev: "1-14918623fedb103cf035ff2489e0a6a1", price: 1}
Object {id: "8117701d38cf9a1112ce8ed3800006e5", rev: "1-09b1e6aa1fb391e11c90bca86daccb7a", price: 5}
Object {id: "8117701d38cf9a1112ce8ed38000144e", rev: "1-2a682bf7ce58829ad2054bb8f5fbe869", price: 4}

但是当模板被渲染时它只是显示Empty,当我将model钩子替​​换ThingsRoute为这个时:

return {things: [{id: 1, rev: 234, price: 4}, {id: 2, rev: 235, price: 3}]};

它完全按预期工作。当我定义afterModel

afterModel: function(things, transition) {
    console.log(things);
    console.log(transition);
}

它记录了这个:

Class {type: function, store: Class, isLoaded: true, isUpdating: false, toString: function…}
Transition {router: Router, promise: Promise, data: Object, resolvedModels: Object, providedModels: Object…}

Class对象有这个:

content: Array[3]
  0: Class
  1: Class
  2: Class

并且每个 THOSE Classes 都有一个id与我的对象相对应的字段。

发生了什么?为什么即使适配器似乎完美地完成了它的工作,我的路线也没有得到那个模型?

4

2 回答 2

1

我认为您的问题是因为things模板中的变量不存在,请尝试更新为model

<script type="text/x-handlebars" data-template-name="things/index">
    {{#each thing in model}}
        {{thing.rev}}
        {{thing.price}}
    {{else}}
        Empty.
    {{/each}}
</script>

或者,如果您想要该变量,您可以在控制器中创建一个别名:

App.ThingsIndexController = Ember.ArrayController.extend({
    things: Ember.computed.alias('model')
});
于 2013-10-19T19:04:23.940 回答
0

您应该使用 find 而不是 findAll

于 2013-10-19T18:53:56.503 回答