Action handlers implemented directly on controllers are deprecated -how to correct this?

混江龙づ霸主 提交于 2020-01-12 06:41:32

问题


I just upgraded from ember.js RC7 to RC8 and found that a simple template (shown below) would throw a deprecated warning

"Action handlers implemented directly on controllers are deprecated"

{{input class="firstName" type="text" placeholder="first name" value=firstName }}                                      
{{input class="lastName" type="text" placeholder="last name" value=lastName }}                                         
<button class="submit" {{action addPerson}}>Add</button>                                                               
<br />                                                                                                                 
<table>                                                                                                                
{{#each person in controller}}                                                                                         
<tr>                                                                                                                   
  <td class="name">{{person.fullName}}</td>                                                                            
  <td><button class="delete" {{action deletePerson person}}>Delete</button></td>                                       
</tr>                                                                                                                  
{{/each}}                                                                                                              
</table>

How should I modify the above template to correct this?


回答1:


It looks like I just needed to give the PR a look that changed this :)

In my controller I just needed to move the addPerson / deletePerson under actions like so

App.PeopleController = Ember.ArrayController.extend({                                                                  
    actions: {                                                                                                         
        addPerson: function() {                                                                                        
            var person = {                                                                                             
                firstName: this.get('firstName'),                                                                      
                lastName: this.get('lastName')                                                                         
            };                                                                                                         
            App.Person.add(person);                                                                                    
        },                                                                                                             
        deletePerson: function(person) {                                                                               
            App.Person.remove(person);                                                                                 
        }                                                                                                              
    }                                                                                                                  
});



回答2:


You should define your actions inside the actions hash in the controllers, views and routes to favour consistency.

Refer this https://github.com/emberjs/ember.js/pull/3091

Demo Fiddle

App.ApplicationController = Em.ObjectController.extend({
  actions : {
   // your actions here
  }       
});


来源:https://stackoverflow.com/questions/18511458/action-handlers-implemented-directly-on-controllers-are-deprecated-how-to-corre

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!