带有灰烬数据的灰烬入门套件

时间:2013-04-25 10:56:27

标签: ember.js

Ember 1.0.0 RC3附带入门套件,带有非常简单的演示,可在列表中显示三种颜色。

模型数据直接插入IndexRoute,如下所示:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return ['red', 'yellow', 'blue'];
  }
});

我尝试将这个简单的演示改为使用ember-data(Models,Store,...)。但是,没有成功。我演示的控制台输出是:

DEBUG: ------------------------------- ember-1.0.0-rc.3.js:349
DEBUG: Ember.VERSION : 1.0.0-rc.3 ember-1.0.0-rc.3.js:349
DEBUG: Handlebars.VERSION : 1.0.0-rc.3 ember-1.0.0-rc.3.js:349
DEBUG: jQuery.VERSION : 1.9.1 ember-1.0.0-rc.3.js:349
DEBUG: ------------------------------- ember-1.0.0-rc.3.js:349
Uncaught TypeError: Cannot call method 'find' of undefined appDemo.js:8
Uncaught Error: assertion failed: an Ember.CollectionView's content must implement Ember.Array. You passed <(generated index controller):ember232> ember-1.0.0-rc.3.js:52

我修改过的脚本如下所示:

App = Ember.Application.create();

App.Router.map(function() {
  // put your routes here
});

App.IndexRoute = Ember.Route.extend({
    model: App.Color.find()
});

App.ColorsController = Ember.ArrayController.extend();


// Models
App.Store = DS.Store.extend({
  revision: 12,
  adapter: 'DS.FixtureAdapter'
});

App.Color = DS.Model.extend({
  name: DS.attr('string')
});
App.Color.FIXTURES = [{name: 1}, {name: 2}, {name: 3}, {name: 4}, {name: 5}, {name: 6}];

我的HTML看起来像这样:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ember Starter Kit</title>
  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/style.css">
</head>
<body>

  <script type="text/x-handlebars">
    <h2>Welcome to Ember.js</h2>

    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="index">
    <ul>
    {{#each color in controller}}
      <li>{{color.name}}</li>
    {{/each}}
    </ul>
  </script>

  <script src="js/libs/jquery-1.9.1.js"></script>
  <script src="js/libs/handlebars-1.0.0-rc.3.js"></script>
  <script src="js/libs/ember-1.0.0-rc.3.js"></script>
  <script src="js/libs/ember-data-latest.js"></script>
  <script src="js/appDemo.js"></script>

</body>
</html>

我做错了什么?

1 个答案:

答案 0 :(得分:2)

第一个错误在于

App.IndexRoute = Ember.Route.extend({
    model: App.Color.find()
});

您必须将模型选项定义为以下函数:

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Color.find();
    }
});

第二个错误我有点不确定,但先试试这个。