1

Ember 1.0.0 RC3 ships with a Starter Kit with quite a simplistic demo to display three colors in a list.

The model data is inserted directly on the IndexRoute like this:

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

I tried to change this simple demo to use ember-data (Models, Store, ...). However, with no success. The console output of my demo is:

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

My modified script looks like this:

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}];

My html looks like this:

<!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>

What am I doing wrong?

4

1 回答 1

2

第一个错误在于

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

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

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

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

于 2013-04-25T11:38:18.400 回答