值#each循环必须是一个数组,控制器传递

Com*_*eek 4 javascript ember.js

我在加载页面时收到以下错误消息:

Assertion failed: The value that #each loops over must be an Array.
    You passed (generated snippets.index controller) ember-1.0.0.js:394

Uncaught TypeError: Object [object Object] has no method 'addArrayObserver'
Run Code Online (Sandbox Code Playgroud)

这是我的模板代码:

<script type="text/x-handlebars" data-template-name="snippets/index">
  {{#each}}
    {{title}}
  {{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)

我的"Snippet"模型非常简单:

TSLibrary.Snippet = DS.Model.extend({
  title: DS.attr('string')
});

TSLibrary.Snippet.FIXTURES = [{id: 1, title: 'Learn Ember.js'}];
Run Code Online (Sandbox Code Playgroud)

我的应用和我的路由器:

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

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

// ---

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

TSLibrary.SnippetsRoute = Ember.Route.extend({
  model: function () {
    // My guess is that this does not return the expected array
    //
    // This logs 'Class {toString: function, constructor: function, reason: null, isPending: undefined, isSettled: undefined…}'

    console.log(this.store.find('snippet'));
    return this.store.find('snippet');
  }
});
Run Code Online (Sandbox Code Playgroud)

所以我的猜测是this.store.find('snippet')没有返回正确的数据.我还在Chrome中安装了Ember调试扩展程序,它在我的模型中显示了所有正确的数据.

Ember版本:1.0.0
Ember数据版本:v1.0.0-beta.1-140-ga51f29c a51f29c (2013-09-07 16:34:55 -0700)
Handlebars版本:1.0.0
jQuery版本:1.10.2

小智 5

TSLibrary.Router.map(function () {
  this.resource('snippets', { path: '/' }, function () {
  });
});
Run Code Online (Sandbox Code Playgroud)

这种方式创建了'snippets.index'路径,这需要一个名为SnippetsIndexRoute的Route.

TSLibrary.Router.map(function () {
  this.resource('snippets', { path: '/' });
});
Run Code Online (Sandbox Code Playgroud)

这个只是'片段',它正确使用您定义的SnippetsRou​​te.