简体   繁体   English

这些动作如何解决并在此Ember.js应用程序中冒泡?

[英]How do these actions resolve and bubble in this Ember.js app?

Here is my basic app in Ember.js: 这是我在Ember.js中的基本应用程序:

This is my app/router.js: 这是我的app / router.js:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('todos', { path: '/'}, function() {
    this.route('complete');
    this.route('incomplete');
  });
});

export default Router;

So when I hit the root path or '/', my application.hbs loads first, then my app/templates/todos.hbs, then my app/templates/todos/index.hbs right? 因此,当我点击根路径或“ /”时,首先加载application.hbs,然后加载我的app / templates / todos.hbs,然后加载我的app / templates / todos / index.hbs,对吗? The app/templates/todos/index.hbs gets loaded inside the outlet of the todos.hbs right? app / templates / todos / index.hbs被加载到todos.hbs的出口内吧?

This is my app/templates/todos.hbs: 这是我的app / templates / todos.hbs:

<p>
  this is the app/templates/todos.hbs.
</p>
{{todo-input action="createTodo"}}
{{#todo-list todos=model}}
    {{outlet}}
{{/todo-list}}

So my todo-input component has an action called 'createTodo'. 因此,我的todo-input组件具有一个称为“ createTodo”的动作。 When does this get called? 什么时候叫这个?

This is my todo-input component handlebars template: 这是我的todo-input组件把手模板:

<p>
  this is the todo-input.hbs component. It gets called inside todos.hbs
</p>
{{input type="text" id="new-todo" placeholder="What needs to be done?"
    value=newTitle enter="submitTodo"}}

Questions : 问题

  1. When I hit enter in the input field, it calls submitTodo right? 当我在输入字段中按Enter键时,它会调用SubmitTodo对吗? Where does it look first? 首先看哪里? Does it look in the component's js file which is app/components/todo-input.js right? 它是否在正确的app / components / todo-input.js组件的js文件中查找? This is that code: 这是该代码:

     import Ember from 'ember'; export default Ember.Component.extend({ actions: { submitTodo(newTitle) { if (newTitle) { this.sendAction('action', newTitle); } this.set('newTitle',''); } } }); 
  2. What argument gets passed to the submitTodo method? 什么参数传递给submitTodo方法?

  3. What is this line: 这行是什么:

     this.sendAction('action', newTitle); 
  4. Where should I define this 'createTodo' action? 我应该在哪里定义这个“ createTodo”动作? Should it be put in the routes/todos.js ? 是否应该将其放在routes / todos.js中?

    import Ember from 'ember'; 从'ember'导入Ember;

    export default Ember.Route.extend({ model() { return this.store.findAll('todo'); }, actions: { createTodo(newTitle) { this.store.createRecord('todo', { title: newTitle, complete: false }).save(); } } }); 导出默认的Ember.Route.extend({model(){return this.store.findAll('todo');},操作:{createTodo(newTitle){this.store.createRecord('todo',{title:newTitle, complete:false})。save();}}});

I am mainly confused as to how the action in this line: 对于这一行的操作方式,我主要感到困惑:

{{todo-input action="createTodo"}}

relates to the enter attribute in the todo-input component template: 与todo-input组件模板中的enter属性有关:

{{input type="text" id="new-todo" placeholder="What needs to be done?"
    value=newTitle enter="submitTodo"}}

When does the action createTodo even get fired? 动作createTodo何时被解雇?

When I hit enter in the input field, it calls submitTodo right? 当我在输入字段中按Enter键时,它会调用SubmitTodo对吗?

Yes, because you've specified that this action should fire on enter event. 是的,因为您指定了该操作应在输入事件时触发。 More info can be found here. 更多信息可以在这里找到。

Where does it look first? 首先看哪里? Does it look in the component's js file which is app/components/todo-input.js right? 它是否在正确的app / components / todo-input.js组件的js文件中查找?

Yes, it does look in component's javascript code for appropriate action. 是的,它会在组件的javascript代码中查找适当的操作。

What argument gets passed to the submitTodo method? 什么参数传递给submitTodo方法?

Input value is passed to to submitTodo action as first and only argument. 输入值作为第一个也是唯一的参数传递给submitTodo操作。

What is this line: 这行是什么:

this.sendAction('action', newTitle);

It fires an action passed as parameter (in this case createTodo ) to component with newTitle as argument. 它以newTitle作为参数触发作为参数传递的操作(在本例中为createTodo )。

Where should I define this 'createTodo' action? 我应该在哪里定义这个“ createTodo”动作? Should it be put in the routes/todos.js ? 是否应该将其放在routes / todos.js中?

Controller would be better place for your action, so you should put createTodo action in controllers/todos.js . 控制器将是您执行操作的最佳场所,因此您应将createTodo操作放入controllers/todos.js

When does the action createTodo even get fired? 动作createTodo何时被解雇?

Check this out: 看一下这个:

  1. User presses enter when he has input focused 用户输入焦点后按Enter
  2. Input helper fires action attached to enter event - which is submitTodo 输入助手触发附加到输入事件的动作-这是submitTodo
  3. submitTodo gets called because it's located in component's actions set 由于submitTodo位于组件的操作集中,因此它被调用
  4. submitTodo calls action passed to todo-input component with this.sendAction() - this action is createTodo submitTodo使用this.sendAction()调用传递给todo-input组件的操作-此操作是createTodo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM