2013-10-19 56 views
2

我已经构建了RESTAdaptercouchdb一起工作,并且正在测试它以确保它可以正常工作,而且目前看起来情况似乎不错,但是我的测试路线似乎存在其他问题。灰烬之路获得成功承诺后获得空模型

很抱歉,这是这么长时间,我大概应该为它设置一个小提琴......我只是从来没有这样做之前,但会研究它现在....

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

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

(我试过ThingsRoute与不Index没有任何变化)

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.log S IN extractArraynormalize都表现出以下完全格式化和正确的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 

而且每个Class ES具有对应于一个id场我对象。

发生了什么事?为什么即使在适配器似乎完成它的工作之后,我的路线还没有获得该模型?

回答

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

良好的捕捉,另外你可以使用{{#each thing}},{{#each thing in controller}}, – Kingpin2k

+0

宾果,两者都有效,并且我正在改变'model',因为它不需要任何额外的'js'你能解释为什么'things'数组不能通过名字传递吗?而且为什么只是'{{每件事}}'不起作用?我在这里错过了一些东西。 – blaineh

+0

And @Daniel'{{#each thing}}'不工作,但是'{{#each thing in controller}}'做了。 – blaineh

0

您应该使用找到替代的findAll

+0

刚刚试过了,它没有改变任何东西:/ – blaineh

+0

你有没有联系d控制器? – Kingpin2k

+0

我不认为这是必要的,因为没有控制逻辑,'Ember'通过命名隐含了所有这一切。这有什么原因可以产生影响吗? – blaineh