隨著應(yīng)用程序的增長,您需要創(chuàng)建視圖的層次結(jié)構(gòu),以確定頁面的不同區(qū)域,并且每個視圖都負(fù)責(zé)處理事件及其屬性。{{view}}助手將子視圖添加到父視圖,并使用字符串查找視圖類。
{{view.VarName}}
在上面的代碼中,{{view}} 助手渲染了在Ember.View中聲明的每個屬性的值。
<!DOCTYPE html> <html> <head> <title>Emberjs Inserting Views in Templates</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"> <!-- specifying the view names --> {{view "user"}} {{view "info"}} </script> <script type="text/x-handlebars" data-template-name="user"> <!-- accessing values by using {{view}} helper --> <b>User:</b> {{view.firstName}} {{view.lastName}} <!-- Setting Child View Templates --> {{#view "info"}} <b>Age:</b>{{view.age}} <br> <b>Hobbies:</b>{{view.hobbies}} {{/view}} </script> <script type="text/x-handlebars" data-template-name="info"> <b>Age:</b> {{view.age}} <br> <b>Hobbies:</b> {{view.hobbies}} </script> <script type="text/javascript"> App = Ember.Application.create({}); //parent view App.UserView = Ember.View.extend({ //template name is 'user' templateName: 'user', firstName: "Mack", lastName: "Smith" }); //child view App.InfoView = Ember.View.extend({ //template name is 'info' templateName: 'info', age: 25, hobbies: "Reading books" }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在insert_view.html文件中
在瀏覽器中打開此HTML文件。
更多建議: