EmberJS 在模板中插入视图
在模板中插入视图
随着应用程序的增长,您需要创建视图的层次结构,以确定页面的不同区域,并且每个视图都负责处理事件及其属性。{{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>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上面的代码保存在insert_view.html文件中
在浏览器中打开此HTML文件。