javascript - 在 ember.js 中添加子路由

标签 javascript ember.js handlebars.js

我现在正在编写 ember.js 教程。我在 "adding child routes" chapter 上遇到问题.我的待办事项列表未显示。 “todos”模板输出很好,但“todos/index”模板根本不起作用。控制台不显示任何错误。我猜这是一些本地问题或一些错误。也许有人已经遇到过类似的问题。这个问题的原因可能是什么?我该如何解决? 谢谢。

HTML:

<html>
  <head>
    <meta charset="utf-8">
    <title>Ember.js • TodoMVC</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
   <script type="text/x-handlebars" data-template-name="todos/index">
    <ul id="todo-list">
      {{#each itemController="todo"}}
        <li {{bind-attr class="isCompleted:completed isEditing:editing"}}>
          {{#if isEditing}}
            {{edit-todo class="edit" value=title focus-out="acceptChanges" insert-newline="acceptChanges"}}
          {{else}}
            {{input type="checkbox" checked=isCompleted class="toggle"}}
            <label {{action "editTodo" on="doubleClick"}}>{{title}}</label><button {{action "removeTodo"}} class="destroy"></button>
          {{/if}}
        </li>
      {{/each}}
    </ul>
  </script>

  <script type="text/x-handlebars" data-template-name="todos">

    <section id="todoapp">
      <header id="header">
        <h1>todos</h1>
        {{input type="text" id="new-todo" placeholder="What needs to be done?" 
                value=newTitle action="createTodo"}}
      </header>

        <section id="main">
          {{outlet}}
          <input type="checkbox" id="toggle-all">
        </section>

        <footer id="footer">
          <span id="todo-count">
            <strong>{{remaining}}</strong> {{inflection}} left</span>
          <ul id="filters">
            <li>
              <a href="all" class="selected">All</a>
            </li>
            <li>
              <a href="active">Active</a>
            </li>
            <li>
              <a href="completed">Completed</a>
            </li>
          </ul>

          <button id="clear-completed">
            Clear completed (1)
          </button>
        </footer>
    </section>

    <footer id="info">
      <p>Double-click to edit a todo</p>
    </footer>

  </script>
    <script src="js/libs/jquery-1.10.2.js"></script>
    <script src="js/libs/handlebars-1.1.2.js"></script>
    <script src="js/libs/ember-1.2.0.js"></script>
    <script src="js/libs/ember-data.js"></script>
    <script src="js/app.js"></script>
    <script src="js/route.js"></script>
    <script src="js/models/todo.js"></script>
    <script src="js/controllers/todo_controller.js"></script>
    <script src="js/controllers/todos_controller.js"></script>
    <script src="js/views/edit_todo_view.js"></script>
  </body>
</html>

JS:

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

    Todos.ApplicationAdapter = DS.FixtureAdapter.extend();
    Todos.Router.reopen({
      rootURL: '/one/'
    });

    Todos.Router.map(function () {
      this.resource('todos', { path: '/' });
    });

    Todos.TodosRoute = Ember.Route.extend({
      model: function () {
        return this.store.find('todo');
      }
    });

    Todos.TodosIndexRoute = Ember.Route.extend({
      model: function () {
        return this.modelFor('todos');
      }
    });

    Todos.Todo = DS.Model.extend({
        title:DS.attr('string'),
        isCompleted:DS.attr('boolean')
    });
    Todos.Todo.FIXTURES = [
      {
        id: 1,
        title: 'Learn Ember.js',
        isCompleted: true
      },
      {
        id: 2,
        title: '...',
        isCompleted: false
      },
      {
        id: 3,
        title: 'Profit!',
        isCompleted: false
      }
    ];
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();
    }
  },

  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'),

});

Todos.TodoController = Ember.ObjectController.extend({
  actions:{
     editTodo: function () {
       this.set('isEditing', true);
     },
     acceptChanges: function () {
        this.set('isEditing', false);

        if (Ember.isEmpty(this.get('model.title'))) {
          this.send('removeTodo');
        } else {
          this.get('model').save();
        }
      },

      removeTodo: function () {
        var todo = this.get('model');
        todo.deleteRecord();
        todo.save();
      },
   },

  isEditing: false,

  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')
});

最佳答案

从技术上讲这不是一个错误,团队认为如果你不能在路由器中走得更深,那么有一个索引路由是没有意义的(这是因为 todos 路由和模板会渲染,所以索引路由并不需要。话虽这么说,如果你真的想要它,添加一个匿名函数,你就会得到它。

Todos.Router.map(function () {
  this.resource('todos', { path: '/' },function () {});
});

https://github.com/emberjs/ember.js/issues/3995

关于javascript - 在 ember.js 中添加子路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763320/

相关文章:

javascript - 2 个数组的笛卡尔积

javascript - Ember View 在 mouseDown 上重复

javascript - "TypeError: chains is undefined "添加 Handlebars (如果是助手)并导航到应用程序主页时

javascript - 在 Ember 中将 Handlebars 插入 DOM

javascript - Json函数不传递数组

Javascript : function bind. 单击&每个复选框

javascript - JS 表单验证

ruby-on-rails - Rails Ember 混合应用程序

javascript - 循环内的 Handlebars 助手

meteor - 如何将模板变量传递给模板辅助函数以保留上下文?