javascript - 尝试让按钮在 Ember.js 中显示

标签 javascript html ember.js

我正在编写 Ember.js 的入门教程。我到达这里:

http://emberjs.com/guides/getting-started/display-a-button-to-remove-completed-todos/

由于某种原因,我无法显示删除已完成的待办事项的按钮。我检查了代码,但无法找出问题所在。

以下是 index.html 页面上按钮的 HTML:

{{#if hasCompleted}}
  <button id="clear-completed" {{action "clearCompleted"}}>
  Clear completed ({{completed}})
  </button>
{{/if}}

我确实对 todos_controller 做了一些更改:

Todos.TodosController = Ember.ArrayController.extend({
  actions: {

    createTodo: function() {
      // Get the todo title set by the "New Todo" text field
      var title = this.get('newTitle');
      if (!title.trim()) { return; }

      // Create the new Todo model
      var todo = this.store.createRecord('todo', {
        title: title,
        isCompleted: false
      });

      // Clear the "New Todo" text field
      this.set('newTitle', '');

      // Save the new model
      todo.save();
    },

    clearCompleted: function() {
      var completed = this.filterBy('isCompleted', true);
      completed.invoke('deleteRecord');
      completed.invoke('save');
    },

    remaining: function() {
      return this.filterBy('isCompleted', false).get('length');
    }.property('@each.isCompleted'),

    inflection: function() {
      var remaining = this.get('remaining');
      return remaining === 1 ? 'item' : 'items';
    }.property('remaining'),

    hasCompleted: function() {
      return this.get('completed') > 0;
    }.property('completed'),

    completed: function() {
      return this.filterBy('isCompleted', true).get('length');
    }.property('@each.isCompleted'),

  }
});

我试图在 JSFiddle 中展示这些更改,但我无法让该应用程序在 JSFiddle 上运行。如果您想查看,请点击以下链接:

http://jsfiddle.net/cspears2002/pQ5AN/9/

最佳答案

您的计算属性应该位于操作哈希之外

http://emberjs.jsbin.com/ILEZAsU/1/edit

actions: {

    createTodo: function() {
      // Get the todo title set by the "New Todo" text field
      var title = this.get('newTitle');
      if (!title.trim()) { return; }

      // Create the new Todo model
      var todo = this.store.createRecord('todo', {
        title: title,
        isCompleted: false
      });

      // Clear the "New Todo" text field
      this.set('newTitle', '');

      // Save the new model
      todo.save();
    },

    clearCompleted: function() {
      var completed = this.filterBy('isCompleted', true);
      completed.invoke('deleteRecord');
      completed.invoke('save');
    },
  },

  remaining: function() {
    return this.filterBy('isCompleted', false).get('length');
  }.property('@each.isCompleted'),

  inflection: function() {
    var remaining = this.get('remaining');
    return remaining === 1 ? 'item' : 'items';
  }.property('remaining'),

  hasCompleted: function() {
    return this.get('completed') > 0;
  }.property('completed'),

  completed: function() {
    return this.filterBy('isCompleted', true).get('length');
  }.property('@each.isCompleted'),

关于javascript - 尝试让按钮在 Ember.js 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21504445/

相关文章:

html - SASS 的关键 CSS

使用 Ember js 的动态选项卡/列表

javascript - ASP.Net MVC 在全屏模式下使导航栏消失

javascript - 从文件加载的 JSON 对象数组

html - CSS 被缓存在某个地方,而不是本地,而不是在浏览器上?

javascript - 如何在没有事件的情况下调用 ember 辛烷模板内的函数?

javascript - 使用 Rails 后端渲染 Ember 组件(谷歌折线图)

javascript - 使用 Node.js 将图像上传到 AWS S3 存储桶。 JS

javascript - 如何计算 SharePoint 库/列表中的新元素数?

html - 让列在 3 列 CSS 布局中扩展到相同的高度?