我目前在学习EmberJS,因为我觉得使用JS框架和使用后端API的单页应用程序是事情将会发生变化的方式。我最近学到了Laravel,没有任何问题,但我发现Ember非常困难。添加到Ember Data的空白条目
我经历过的大部分问题,但我有一个,我甚至不知道如何调试。
本质上,我的界面显示客户端列表。点击一个名字就去那个客户端。所有这些都连接到一个基于PHP/Laravel的API,这个API运行得非常好,并且格式正确。数据不是这里的问题。
这就是说,我应该指出,我的数据没有严格的“id”,但是使用更像guid:912ec803b2而不是231的连接。API的构建是为了处理这个问题,这是一个需求。这也意味着数据中没有“id”,但是有一个“散列”,可能会以某种方式混淆ember?
此基本功能似乎不能正常工作。当用户点击客户端链接时,它会导航到客户端。但是它将一个空行添加到客户端列表页面,然后进入客户端页面。返回到客户端页面显示额外的空白行,如果再次点击,同样的事情会重复出现。
在Chrome中检查Ember检查器显示增加了数据收集,并添加了空白数据。我不知道为什么。
我已经在这里发布了代码。这应该是所有必要的。这种行为是年轻球员常见的陷阱,我做过一些不寻常的事情吗?
道歉的长度,只是不想省略相关的东西。
// app.js
App = Ember.Application.create({
LOG_TRANSITIONS: true
});
App.Router.map(function() {
this.resource('clients', function() {
this.route('new');
});
this.resource('client', { path: 'clients/:client_hash' }, function(){
});
});
App.Store = DS.Store.extend({
url: 'http://myclientinfo'
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api'
});
App.Client = DS.Model.extend({
name: DS.attr('string'),
hash: DS.attr('string')
});
App.ClientsRoute = Ember.Route.extend({
model: function(){
return this.store.find('client');
}
});
App.ClientIndexRoute = Ember.Route.extend({
model: function(params){
return this.store.find('client', params.client_hash);
}
});
// templates
<script type="text/x-handlebars">
<div class="container">
<ul class="nav nav-pills">
<li>{{#link-to 'clients'}}Clients{{/link-to}}</li>
</ul>
{{outlet }}
</div>
</script>
<script type="text/x-handlebars" id="client">
client outer
{{outlet}}
</script>
<script type="text/x-handlebars" id="clients">
client outer
{{outlet}}
</script>
<script type="text/x-handlebars" id="client/index">
client index
<h1>{{name}}</h1>
</script>
<script type="text/x-handlebars" id="clients/index">
<div class="row">
<div class="col-md-12 col-lg-12">
<h1>All Clients</h1>
</div>
</div>
<div class="row"><div class="col-md-12 col-lg-12">
{{#link-to 'clients.new' class="btn btn-info add-btn"}}
<i class="fa fa-plus"> Add new client</i>
{{/link-to}}
</div></div>
<div class="row">
<div class="col-md-12 col-lg-12">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
{{#each item in model}}
<tr>
<td>{{#link-to 'client' item.hash}}{{item.name}}{{/link-to}}</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</div>
</script>
这听起来像您的适配器没有正确序列化数据。您可以发布您在从“客户”路线导航到“客户”时获得的网络响应吗? –
我不确定你指的是什么,但我假设的东西像Chrome开发工具的网络标签?如果有一个特定于烬的工具,请告诉我。 它所说的就是GET/api/clients - 200 OK 值得注意的是它永远不会说GET/api/clients/2/...不知道这是否意味着什么。 –
此外,我修改了API和Ember,使其不再使用上面提到的哈希。我现在使用一个标准的整数ID来简化任何可能会导致我的问题的约定。它没有什么区别。 –