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

Finally we’ll replace our fixture data with real persistence so todos will remain between application loads by replacing the fixture adapter with a localstorage-aware adapter instead.

最后我们将修改之前使用的夹具数据为真实持久化的数据。这样才能在每次应用加载的时候保持代办事项。该功能通过将夹具适配器替换为localstorage适配器就能实现。

Change js/application.js to:

修改js/application.js为:

  1. window.Todos = Ember.Application.create();
  2. Todos.ApplicationAdapter = DS.LSAdapter.extend({
  3. namespace: 'todos-emberjs'
  4. });

The local storage adapter, written by Ryan Florence, can be downloaded from its source. Add it to your project as js/libs/localstorage_adapter.js. You may place this file anywhere you like (even just putting all code into the same file), but this guide will assume you have created the file and named it as indicated.

localstorage适配器由Ryan Florence编写,可以从其源下载。将其添加至项目的js/libs/localstorage_adapter.js。当然你也可以将其放置到任何你喜欢的位置(或者将所有代码放置到一个文件中),不过本指南假设你按照指定的路径保存文件和对其命名。

In index.html include js/libs/localstorage_adapter.js as a dependency:

index.html引入js/libs/localstorage_adapter.js依赖:

  1. <!--- ... additional lines truncated for brevity ... -->
  2. <script src="js/libs/ember-data.js"></script>
  3. <script src="js/libs/localstorage_adapter.js"></script>
  4. <script src="js/application.js"></script>
  5. <!--- ... additional lines truncated for brevity ... -->

Reload your application. Todos you manage will now persist after the application has been closed.

重载应用,现在待办事项在应用被关闭后依然会被保存。

Live Preview

在线演示

Ember.js • TodoMVC

Additional Resources

附加资源