2011-12-29 70 views
1

我试图使用https://github.com/emberjs/data作为参考使用余烬数据。Ember数据:正确使用方法findAll

具体而言,我试图使用数组控制器来显示我的数据库中的所有'Person'对象。我也想让用户创建一个新的'Person'。

我有下面的代码工作:

App.peopleController = Em.ArrayController.create 
    content: App.store.findAll(App.Person) 

    newPerson: (name) -> 
    App.store.create App.Person, 
     name: name 
    @set('content', App.store.findAll(App.Annotation)) 

然而,似乎没有效率的每次创建一个新的人时重置内容属性。如果我删除最后一行的代码更改为以下:

App.peopleController = Em.ArrayController.create 
    content: App.store.findAll(App.Person) 

    newPerson: (name) -> 
    App.store.create App.Person, 
     name: name 

一个新视图上不断newPerson呼叫仍会创建,但同一个对象被复制。基本上所发生的是所有新模板都使用第一个创建的对象,而不是每次都创建一个新对象。我认为这与以下错误有关:https://github.com/emberjs/data/issues/11

仅供参考,我的模板逻辑如下:

{{#each App.peopleController}} 
    {{#view App.PersonView contentBinding="this"}} 
    {{#with content}} 
     Client id is {{clientId}} 
    {{/with}} 
    {{/view}} 
{{/each}} 

当我用我的代码 - 一个与@set('content', App.store.findAll(App.Annotation)) line-- clientid以复制为每一个Person对象的第二个版本。在第一个版本中,客户端ID是正确的。

任何人都可以在这里发光?我是否正确地做这件事?我的直觉告诉我这是一个错误,但我不确定。

回答

4

我认为这是一个错误。我发布了一个说明此问题的related issue

+0

谢谢,希望这会很快得到解决。 – ghempton 2011-12-30 02:43:04

+4

是一个错误。提出了一个修复方案,并提交了对此问题的请求:https://github.com/emberjs/data/issues/11。 – ghempton 2011-12-30 22:37:42

0

请尝试使用#collection视图。

请参阅ToDo示例代码。有关文档,另请参阅http://guides.sproutcore20.com/using_handlebars.html第5节。

希望这会有所帮助。

+0

我试过这个以防万一,但它仍然不起作用,并且有相同的问题。根据我的理解,'#collection'只是'#each'的缩写,然后为底层集合中的每个元素创建一个视图。 – ghempton 2011-12-30 02:41:22