简体   繁体   English

成功承诺后,灰烬路线将变为空模型

[英]Ember route getting empty model after successful promise

I've built a RESTAdapter to work with couchdb , and am testing it to make sure it works, and so far things seem fine, but my test route seems to be having other problems. 我已经建立了一个RESTAdapter一起工作couchdb ,并正在测试它,以确保它的工作原理,到目前为止,事情看起来不错,但我的测试路线似乎有其他问题。

Sorry this is so long, I probably ought to set up a fiddle for it... I've just never done that before, but will look into it right now.... 抱歉,这个时间太长了,我可能应该为此设置一个小提琴...我以前从未做过,但是现在将对其进行调查...。

I've built the following (relevant) things: 我构建了以下(相关)内容:

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');
    }
});

(I've tried ThingsRoute with and without the Index without any change) (我已经尝试ThingsRoute使用和不使用Index而没有任何更改的ThingsRoute

In App.Router.map : App.Router.map

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

In App.ApplicationAdapter = DS.RESTAdapter.extend : App.ApplicationAdapter = DS.RESTAdapter.extend

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

In App.ApplicationSerializer = DS.RESTSerializer.extend : 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);
}

And this template: 和这个模板:

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

The console.log s in extractArray and normalize both show the following perfectly formatted and correct json: extractArraynormalizeconsole.log都显示了以下格式正确且正确的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}

but when the template is rendered it simply shows Empty , and when I replace the model hook in the ThingsRoute to this: 但是当渲染模板时,它只是显示Empty ,而当我将ThingsRoutemodel挂钩替换为:

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

it works exactly as expected. 它完全按预期工作。 AND when I define afterModel : 并且当我定义afterModel

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

It logs this: 它记录以下内容:

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

that Class object has this: Class对象具有以下内容:

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

and each of THOSE Class es has an id field corresponding to my objects. 并且每个THOSE Class es都有一个与我的对象相对应的id字段。

What's happening? 发生了什么? Why isn't my route getting that model even after the Adapter seems to do it's job perfectly? 为什么即使Adapter看起来做得很好,我的路线也无法获得该模型?

I think that your problem is because the things variable in your template, doesn't exist, try to update to model 我认为您的问题是因为模板中的things变量不存在,请尝试更新为model

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

Or if you want that variable you can create a alias in your controller: 或者,如果您想要该变量,则可以在控制器中创建别名:

App.ThingsIndexController = Ember.ArrayController.extend({
    things: Ember.computed.alias('model')
});

您应该使用find而不是findAll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM