Ember.js 成功承诺后的余烬路径获取空模型
我已经构建了一个Ember.js 成功承诺后的余烬路径获取空模型,ember.js,ember-data,Ember.js,Ember Data,我已经构建了一个RESTAdapter来使用couchdb,并且正在测试它,以确保它能正常工作,到目前为止,事情似乎还不错,但我的测试路线似乎还有其他问题 对不起,这么长时间了,我可能应该为它竖起一把小提琴。。。我以前从来没有这样做过,但现在我会调查的 我构建了以下(相关)东西: 在App.ApplicationAdapter=DS.RESTAdapter.extend中: buildURL: function(type, id) { id = id || '_all_docs?incl
RESTAdapter
来使用couchdb
,并且正在测试它,以确保它能正常工作,到目前为止,事情似乎还不错,但我的测试路线似乎还有其他问题
对不起,这么长时间了,我可能应该为它竖起一把小提琴。。。我以前从来没有这样做过,但现在我会调查的
我构建了以下(相关)东西:
在App.ApplicationAdapter=DS.RESTAdapter.extend中:
buildURL: function(type, id) {
id = id || '_all_docs?include_docs=true';
return this._super(type, id);
}
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);
}
在App.ApplicationSerializer=DS.RESTSerializer.extend中:
buildURL: function(type, id) {
id = id || '_all_docs?include_docs=true';
return this._super(type, id);
}
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>
但是当呈现模板时,它只显示空
,当我将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…}
类
对象具有以下特性:
content: Array[3]
0: Class
1: Class
2: Class
每个Class
es都有一个与我的对象相对应的id
字段
发生了什么事?为什么我的路线即使在适配器似乎能很好地完成它的工作之后也无法获得该模型?您应该使用find而不是findAll我认为您的问题是因为模板中的
things
变量不存在,请尝试更新到模型
<script type="text/x-handlebars" data-template-name="things/index">
{{#each thing in model}}
{{thing.rev}}
{{thing.price}}
{{else}}
Empty.
{{/each}}
</script>
你有关联的控制器吗?我不认为这是必要的,因为没有控制逻辑,而且Ember
通过命名隐式地连接所有这些。有什么原因可以改变吗?没有必要,我只是检查一下你是否有任何控制器可能会破坏它。顺便说一句,为了将来的参考,请使用emberjs.jsbin.com来设置你的项目以供参考(至少作为你的一些框架设置)。Marcio可能有你的答案。很好,另外,您可以使用{{{{each things}},{{{{each things in controller}},宾果,这两种方法都有效,我将更改为模型
,因为它不需要任何额外的js
,您能解释一下things
数组为什么不按名称传递吗?还有为什么简单的{{{每件事}}
不起作用?我在这里遗漏了一些东西。@Daniel{{{{每件事}}
不起作用,但{{{控制器中的每件事}}
起作用。things属性仅用于解析余烬数据的things对象。一旦您从钩子返回它,它的属性名就是控制器中的model/content。控制器将代理get/set请求发送到模型。是的,第一个请求完全不正确,我的意思是这样{{{each}}在这里,上下文将是事情,所以你可以做{{rev}{{price}{{{this}}}等等{/each},我个人更喜欢{{each}控制器中的每个项目}
<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')
});