我有一个ember应用程序,允许您创建,编辑和删除用户以及查看用户列表。我通过遵循教程制作应用程序(仍在学习余烬),我的问题是所有的CRUD页面加载到不同的网点。我想要在应用程序{{outlet}}
中有一个用户列表,然后当我单击一个用户时,该用户的各个详细信息将显示在与列表相同的{{outlet}}
中。如果我点击编辑,我想要一个编辑表单出现在{{outlet}}
中,保存时我希望{{outlet}}
再次呈现用户列表。有人可以告诉我怎么去做这件事吗?或者提出一种更好的渲染方式?在ember.js中的一个插座上呈现不同的模板
这里是我的router.js
:
App.Router.map(function(){
this.resource('users', function(){
this.resource('user', { path:'/:user_id' }, function(){
this.route('edit');
});
this.route('create');
});
});
我的模板:
应用程序模板:
<script type="text/x-handlebars" id="application">
//This is where I want everything to render
{{outlet}}
</script>
用户模板
<script type = "text/x-handlebars" id = "users">
{{#link-to "users.create"}}Create New User {{/link-to}}
<ul class="list-group">
{{#each user in controller}}
<li class="list-group-item">
{{#link-to "user" user}}
{{user.firstName}} {{user.lastName}}
{{/link-to}}
</li>
{{/each}}
</ul>
//This is where the individual agent currently gets rendered, but it looks terrible
{{outlet}}
</script>
个人用户模板
<script type = "text/x-handlebars" id = "user">
<div class="user-profile">
{{#if deleteMode}}
<div class="confirm-box">
<h5>Really?</h5>
<button {{action "confirmDelete"}}> yes </button>
<button {{action "cancelDelete"}}> no </button>
</div>
{{/if}}
<h4>Name: {{firstName}} {{lastName}}</h4>
<h4>Email: {{email}}</h4>
<button {{action "edit"}}>Edit</button>
<button {{action "delete"}}>Delete</button>
</div>
//This is where the edit form gets rendered, again looking dreadful
{{outlet}}
</script>
编辑用户模板
<script type = "text/x-handlebars" id = "user/edit">
<h5>First Name</h5>
{{input value=firstName}}
<h5>Last Name</h5>
{{input value=lastName}}
<h5>Email</h5>
{{input value=email}}
<button {{action "save"}}> Save </button>
</script>
谢谢,这对用户和个人用户非常有用!我怎样才能创建/编辑? – bookthief
@bookthief很高兴它的工作。事实上,也许创建/编辑并不清楚,请看看http://emberjs.jsbin.com/iBEBEso/1#/users,答案也会更新。 – melc
这太棒了,完美的作品! – bookthief