{{action}}助手可能會將操作直接發(fā)送到視圖的控制器。目標(biāo)選項(xiàng)用于備用目標(biāo)。這最常用于將操作發(fā)送到視圖而不是控制器。
<button {{action "ActionName" parametrs target=view}}>Click</button>
<!DOCTYPE html> <html> <head> <title>Emberjs Specifying a Target</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars"> {{view Ember.CollectionView contentBinding="model" itemViewClass="App.View"}} </script> <script type="text/x-handlebars" data-template-name="index"> {{#if view.isShowVisible}} {{view.content.tag}}<br/><br/> <!-- action directly send to the view's controller --> <button {{action "User" target="view"}}>Edit</button> {{else}} {{input type="text" value=view.content.tag}}<br/><br/> <button {{action "User" target="view"}}>Done</button> {{/if}} </script> <script type="text/javascript"> App = Ember.Application.create(); App.ApplicationRoute = Ember.Route.extend({ //return the tag value model: function() { //specifying the primary value as tag: return [{tag:'Tutorialspoint'}]; } }); //creating the target view App.View = Ember.View.extend({ templateName: 'index', isShowVisible: true, actions: { User: function(){ //toggling the boolean property this.toggleProperty('isShowVisible'); } } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在temp_action_target.html文件中
在瀏覽器中打開此HTML文件。
更多建議: