更新灯具?

时间:2014-06-28 19:03:23

标签: ember.js

我跟随官方Ember guide做了Todo应用。

是否有可能对灯具进行持续改变?如果是这样,怎么样?因为使用我的代码,单击复选框并不会将值持久保存到灯具中。

的index.html:

<script type="text/x-handlebars" data-template-name="todos">
  <ul id="todo-list">
    {{#each itemController="todo"}}
      <li {{bind-attr class="isCompleted:completed"}}>
        {{input type="checkbox" checked=isCompleted class="toggle"}}
        <label>{{title}}</label><button class="destroy"></button>
      </li>
    {{/each}}
  </ul>
</script>

的application.js:

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

模型/ todo.js:

Todos.Todo = DS.Model.extend({
  title: DS.attr('string'),
  isCompleted: DS.attr('boolean')
});

Todos.Todo.FIXTURES = [
 {
   id: 1,
   title: 'Learn Ember.js',
   isCompleted: true
 }
];

控制器/ todo_controller.js:

Todos.TodoController = Ember.ObjectController.extend({
  isCompleted: function(key, value){
    var model = this.get('model');

    if (value === undefined) {
      // property being used as a getter
      return model.get('isCompleted');
    } else {
      // property being used as a setter

      model.set('isCompleted', value);
      model.save();
      return value;
    }
  }.property('model.isCompleted')
});

1 个答案:

答案 0 :(得分:1)

我不相信。在该教程的最后,有一节用于使用本地存储替换夹具适配器,以便您获得真正的持久性。 http://emberjs.com/guides/getting-started/using-other-adapters/

JS / application.js中

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

Todos.ApplicationAdapter = DS.LSAdapter.extend({
  namespace: 'todos-emberjs'
});

的index.html

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