Ember.js 将余烬数据与装置一起使用并返回单个对象

Ember.js 将余烬数据与装置一起使用并返回单个对象,ember.js,Ember.js,我有以下代码: App.UserAdapter = DS.FixtureAdapter.extend({}); App.User = DS.Model.extend({ activity: DS.hasMany('activity'), firstname: DS.attr('string'), lastname: DS.attr('string'), username: DS.attr('string'), email: DS.attr('string

我有以下代码:

App.UserAdapter = DS.FixtureAdapter.extend({});

App.User = DS.Model.extend({
    activity: DS.hasMany('activity'),
    firstname: DS.attr('string'),
    lastname: DS.attr('string'),
    username: DS.attr('string'),
    email: DS.attr('string'),
    phone: DS.attr('string')                    
});
和一个固定装置:

App.User.FIXTURES = [
    {
        id: 1,
        firstname: 'John',
        lastname: 'Doe',
        username: 'jdoe',
        email: 'jdoe@apple.com',
        phone: '408-111-1111'
    }
];

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('user');
   }
});
现在我想在模板中访问此数据:

  <script type="text/x-handlebars" id="index">
  <h2>Login</h2>
  {{#each}}
    <ul>
    <li>User: {{firstname}} {{lastname}}</li>    
    <li>User Name: {{username}}</li>
    <li>Password: {{input}}</li>
    </ul>
    {{/each}}
  </script>

登录
{{{#各}
  • 用户:{{firstname}}{{lastname}}
  • 用户名:{{username}
  • 密码:{{input}
{{/每个}}

它迫使我以数组的形式访问它。我在这里做错了什么吗?

当您使用DS.Store#find并且不传递id时,它将返回该类型的所有记录

如果要在存储中查找特定记录,需要传递该记录的id

例如,要查找id为1的用户:

App.Router.map(function() {
  this.route("user", { path: '/users/:id' });
});

App.UserRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('user', params.id);
  }
});

该“解决方案”的问题在于,它假定我在设计时知道id,而我不知道。我希望此路由动态显示正确的组织是对{{#link to}的响应。{{{{{{contact.firstname}}{{{contact.lastname}}{{/link to}}{{{contact.lastname}}}{/link to}}}{{contact.org}{{contact.firstname}}{/link to}{{input type=“checkbox”name=“completed”checked=completed}}}您应该花些时间阅读,特别是关于.是的部分,我应该重新阅读。我试图在这里做一些棘手的事情,这正是让我感到困难的原因。我希望该用户对象在应用程序的所有级别都可用。也许我应该在设计时将其映射到应用程序控制器或其他什么东西?params.id不是动态的,在运行时确定的吗?我做的第一件事就是查询并获取一个数组。然后,对生成的数组调用
.find()
.findBy()
。(因此您可以调用
.find()
两次,但一次在存储中,另一次在可枚举项上。