简体   繁体   English

如何在Handlebars模板的输入字段中使用Ember.js的操作Helper传递参数?

[英]How to pass parameters with the action Helper of Ember.js inside an input field from an Handlebars template?

In my handlebars template I have this loop: 在我的车把模板中,我有这个循环:

{{#each itemController="fund"}}
    <li>                        
        <label>{{title}}</label>            
        <span>{{amount}}</span>
        {{input type="text" placeholder="new user"
        value=newFullName action="createUser"}}
        {{partial 'user-list'}}
    </li>
{{/each}}

and need to pass the current object as parameter to the 'createUser' action. 并且需要将当前对象作为参数传递给'createUser'动作。 Something like this: 像这样的东西:

action="createUser(this)"

or: 要么:

action 'createUser' this

But it seems that ember can't handle parameters for actions inside an input field... 但似乎ember无法处理输入字段内的操作参数......

Am i missing something? 我错过了什么吗?

You can now pass a function, along with values - 您现在可以传递一个函数以及值 -

submit=(action 'setName' 'Sal')

http://emberjs.com/blog/2015/06/12/ember-1-13-0-released.html#toc_closure-actions http://emberjs.com/blog/2015/06/12/ember-1-13-0-released.html#toc_closure-actions

I think that isn't possible to do this using the action property from input view helper. 我认为使用input视图助手中的action属性无法做到这一点。

A workaround could be wrap your input in a form that use the action view helper using the submit event, like the following: 解决方法可以使用submit事件将输入包装在使用action视图助手的表单中,如下所示:

Template 模板

{{#each}}
      <li>                   
          <form {{action "createUser" this on="submit"}}>
              {{name}}
              {{input type="text" value=name}}          
          </form>
      </li>
  {{/each}}

Route 路线

  ...
  actions: {
    createUser: function(user) {
      alert(user.get('name'));
    }
  }
  ...

So when the user hit enter, will have the event triggered. 因此,当用户按Enter键时,将触发事件。

The main difference between the action property and the action view helper is that the action view helper is more flexible and you can supply the context and put it inside of any tag: 动作属性和动作视图助手之间的主要区别在于动作视图助手更灵活,您可以提供上下文并将其放在任何标记内:

<div {{action "someAction" someObject}} on="click">Click me</div>

In the route: 在路线:

actions: {
  someAction: function(someObject) {
    // do something with the someObject
  }
}

See the docs for further information 有关详细信息,请参阅文档

Please give a look in the jsfiddle to see that sample in action http://jsfiddle.net/marciojunior/UAgjX/ 请在jsfiddle中查看该示例的实际操作http://jsfiddle.net/marciojunior/UAgjX/

I hope it helps 我希望它有所帮助

Finally i ended up with this solution: 最后我最终得到了这个解决方案:

Template 模板

{{input class="newUser" type="text" value=newFullName placeholder="add user"}}
<button {{action 'createUser' this newFullName}}>Send</button>

Controller 调节器

createUser: function (fund, newFullName) {
        var fullName = newFullName;                        
        var user = this.store.createRecord('appUser', {
            fullName: fullName,                
            fund: fund,
            payments:[]
        });
        user.save().then(function(){                
            fund.get('users').pushObject(user);                
            fund.save().then(function(){
                fund.reload();
            });
        });
    }

You can pass a parameter to an action helper : {{action "doSomething" xxx }} 您可以将参数传递给操作助手: {{action "doSomething" xxx }}

Where doSomething is your controller method ,and xxx is anything in the current context of the template. doSomething是您的控制器方法,xxx是模板当前上下文中的任何内容。

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

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