組件使用'sendAction'與Ember應(yīng)用程序交互。
<!DOCTYPE html> <html> <head> <title>Emberjs Validation In Components</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://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.prod.js"></script> <script src="https://code.jquery.com/qunit/qunit-1.18.0.js"></script> <script src="https://rawgit.com/rwjblue/ember-qunit-builds/master/ember-qunit.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> <div id="qunit"></div> <div id="ember-testing"></div> <script type="text/x-handlebars" data-template-name="index"> <h2>Action Not Specified</h2> <p>Tag: {{title}}</p> <p>{{my-comp title=title action="compFunc"}}</p> </script> <script type="text/x-handlebars" data-template-name="components/my-comp"> <input type="button" value="Click me" {{action "compFunc"}}/> </script> <script type="text/javascript"> //Creates an instance of Ember.Application and assign it to a global variable App = Ember.Application.create(); App.MyComponent = Em.Component.extend({ //action not specified: error click: function() { this.set('title', "Tutorialspoint..."); this.sendAction(); } }); //These methods are used to prepare the application for testing //emq.globalize(); App.setupForTesting(); App.injectTestHelpers(); App.rootElement = '#ember-testing'; //Ember.js applications's root element setResolver(Ember.DefaultResolver.create({ namespace: App })); //Test the component done using the 'moduleForComponent' helper and will find the component by name(my-color) moduleForComponent('my-comp', 'MyComponent'); test('trigger external action when button is clicked', function() { //'this.subject()' is available because we used 'moduleForComponent' var component = this.subject(); //Defining the component dom instance var $component = this.append(); var targetObject = { externalAction: function(){ ok(true, 'external Action was called!'); } }; //External action to be called when button is clicked component.set('internalAction', 'externalAction'); //Set the targetObject to our dummy object component.set('targetObject', targetObject); click('button'); }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作
將上面的代碼保存在testing_validation.html文件中
在瀏覽器中打開(kāi)此HTML文件。
更多建議: