英文原文:http://emberjs.com/guides/getting-started/using-fixutres/

Using Fixtures

使用静态数据

Now we’ll add fixture data. Fixtures are a way to put sample data into an application before connecting the application to long-term persistence.

现在,我们添加一些静态数据。在我们连接应用服务器对数据进行永久持久化之前,使用静态数据是在应用中添加示例数据的一种很好的方式。

First, update js/application.js to indicate that your application’s ApplicationAdapter is an extension of the DS.FixtureAdapter. Adapters are responsible for communicating with a source of data for your application. Typically this will be a web service API, but in this case we are using an adapter designed to load fixture data:

首先,更新js/application.js来指定应用的ApplicationAdapter是扩展自DS.FixtureAdapter。适配器主要负责与应用的一个数据源进行通信。通常可能会是一个Web服务器接口,但是在此使用了一个加载夹具数据的适配器:

  1. window.Todos = Ember.Application.create();
  2. Todos.ApplicationAdapter = DS.FixtureAdapter.extend();

Next, update the file at js/models/todo.js to include the following fixture data:

接下来,更新 js/models/todo.js ,添加如下数据:

  1. // ... additional lines truncated for brevity ...
  2. // ... 为确保简洁,略去头尾代码 ...
  3. Todos.Todo.FIXTURES = [
  4. {
  5. id: 1,
  6. title: 'Learn Ember.js',
  7. isCompleted: true
  8. },
  9. {
  10. id: 2,
  11. title: '...',
  12. isCompleted: false
  13. },
  14. {
  15. id: 3,
  16. title: 'Profit!',
  17. isCompleted: false
  18. }
  19. ];

Reload your web browser to ensure that all files have been referenced correctly and no errors occur.

重新加载浏览器页面,确保所有的文件被正确引用,且没有错误产生。

Live Preview

在线演示

Ember.js • TodoMVC

Additional Resources

附加资源