0

我总是得到这个错误:对象[对象对象]没有方法'addArrayObserver'。在网上检查我知道这不是一个特定的错误,可能我的问题出在我的路线上,但我看不到任何问题......这是我的代码:

window.Notes = Ember.Application.create();

Notes.ApplicationAdapter = DS.FixtureAdapter.extend();

Notes.Router.map(function () {
    this.resource('notes', { path: '/' });
});

Notes.NotesRoute = Ember.Route.extend({ 
    model: function (){             
        return this.store.find('note'); 
    }
});

Notes.NotesController = Ember.ObjectController.extend ();

Notes.Note = DS.Model.extend ({
    title: DS.attr('string'),
    body: DS.attr('string'),
    url: DS.attr('string'),
});

Notes.Note.FIXTURES = [
{
    id: 1,
    title: 'hello world',
    body: 'ciao ciao ciao ciao',
    url: '...'
},
{   
    id: 2,
    title: 'javascript frameworks',
    body: 'Backbone.js, Ember.js, Knockout.js',
    url: '...'

},
{
    id: 3,
    title: 'Find a job in Berlin',
    body: 'Monster, beralinstartupjobs.com',
    url: '...'
}
]

这里的html:

<script type="text/x-handlebars">
<div class="wrap">
  {{#each itemController="note"}}
    <section>
        <h2>{{title}}</h2>
        <p>{{body}}</p>
        <input type="text" placeholder="URL:" class="input" />
    </section>
  {{/each}}
</div>    

我已经尝试更改 Notes.NotesController = Ember.ObjectController.extend (); with Notes.NotesController = Ember.ArrayController.extend ();

但我仍然得到错误。我的代码有什么问题?

4

2 回答 2

0

Notes.NotesController需要成为 的子类Ember.ArrayController,因为从NotesRoute model钩子返回的数据是一个类似 (DS.RecordArray) 的数组。

在此之后,您将收到一个错误,因为您声明itemController="note"ember 需要一个名为的控制器Notes.NoteController,并且它需要是Ember.ObjectController. #each该控制器将在视图助手中包装每个元素。

总而言之,您的代码中缺少以下内容:

Notes.NotesController = Ember.ArrayController.extend();

Notes.NoteController = Ember.ObjectController.extend();

请用更新的代码看看这个小提琴http://jsfiddle.net/marciojunior/Ly8K6/

我希望它有帮助

于 2013-10-28T22:41:59.570 回答
0

正如他上面所说,我正在制作一个 JSBin,但我被拉走了。

给你:

http://emberjs.jsbin.com/OhUXiRi/1/edit

控制器会自动为您生成。

于 2013-10-28T23:15:22.160 回答