Ember.js:如何访问 CollectionView 中的特定项目?
首先我想说我真的很喜欢 ember.js。我尝试过 Knockout 和 Angular,但发现它们有点唐突,一切都必须按照它们的方式进行。我觉得 ember 让我有更多的自由来按照你认为合适的方式构建事物。话虽如此,我有几个问题。
1。我想做类似以下的事情,但显然不起作用:
{{ content.name }}< ;/h3>
相反,我必须创建一个绑定:
如何在视图中创建 url 路径?我可以轻松地在模型上创建一个名为 url 的计算属性,但这感觉很糟糕,而且不太 MVC。我是否必须为元素创建一个新视图或注册一个感觉有点麻烦的助手?
这是完整的代码:
App = Ember.Application.create();
var sampleData = [ Ember.Object.create({ id: '123456789', name: 'John' }), Ember.Object.create({ id: '987654321', name: 'Anne' }) ]
App.itemController = Ember.ArrayController.create({
content: sampleData,
removeItem: function(item) {
this.content.removeObject(item);
}
});
App.ItemListView = Ember.View.extend({
itemDetailView: Ember.CollectionView.extend({
contentBinding: 'App.itemController',
itemViewClass: Ember.View.extend({
tagName: 'li',
url: '' // HOW TO GET '/item/123456789'
deleteButton: Ember.Button.extend({
click: function(event) {
var item = this.get('content');
App.itemController.removeItem(item);
}
})
})
})
});
<script type="text/x-handlebars">
{{#view App.ItemListView}}
<ul id="item-list">
{{#collection itemDetailView}}
<div class="item">
<a {{ bindAttr href="url" }}><h3>{{ content.name }}</h3></a>
{{#view deleteButton class="btn" contentBinding="content"}}Delete{{/view}}
</div>
{{/collection}}
</ul>
{{/view}}
</script>
2。我觉得视图“拥有”控制器,而不是相反。难道视图不应该不知道它连接到哪个控制器,以便您可以重用该视图吗?我正在考虑视图中的这些行:
contentBinding: 'App.itemController',
和
App.itemController.removeItem(item);
您如何构建它?
3。我意识到一切都在进行中,并且随着名称的更改而变得相当新,除了文档之外的所有内容都相当不清楚。这些示例使用旧的命名空间 SC,与 Sproutcore 2.0 指南相比,emberjs.com 上缺少很多东西,例如集合、数组控制器。我在这里读到过,集合将被逐步淘汰。这是真的吗?我应该使用#each 代替吗?
感谢您的帮助和很棒的框架!
First off I want to say that I really like ember.js. I have tried both Knockout and Angular but found them a bit to obtrusive and everything had to be done their way. I feel like ember allows me a bit more freedom to structure things how you see fit. With that said I have a couple of questions.
1. I would like to do something like the following which obviously doesn't work:
<a href="/item/{{ content.id }}"><h3>{{ content.name }}</h3></a>
Instead I would have to create a binding:
<a {{ bindAttr href="url" }}><h3>{{ content.name }}</h3></a>
How do i create the url path in the view? I could easily create a computed property called url on the model but that feels horrible and not very MVC. Do I have to create a new view for the element or register a helper which feels a bit cumbersome?
Here's the complete code:
App = Ember.Application.create();
var sampleData = [ Ember.Object.create({ id: '123456789', name: 'John' }), Ember.Object.create({ id: '987654321', name: 'Anne' }) ]
App.itemController = Ember.ArrayController.create({
content: sampleData,
removeItem: function(item) {
this.content.removeObject(item);
}
});
App.ItemListView = Ember.View.extend({
itemDetailView: Ember.CollectionView.extend({
contentBinding: 'App.itemController',
itemViewClass: Ember.View.extend({
tagName: 'li',
url: '' // HOW TO GET '/item/123456789'
deleteButton: Ember.Button.extend({
click: function(event) {
var item = this.get('content');
App.itemController.removeItem(item);
}
})
})
})
});
<script type="text/x-handlebars">
{{#view App.ItemListView}}
<ul id="item-list">
{{#collection itemDetailView}}
<div class="item">
<a {{ bindAttr href="url" }}><h3>{{ content.name }}</h3></a>
{{#view deleteButton class="btn" contentBinding="content"}}Delete{{/view}}
</div>
{{/collection}}
</ul>
{{/view}}
</script>
2. I feel that the view "owns" the controller and not the other way around. Shouldn't the view be unaware of which controller it is hooked up to so you can reuse the view? I'm thinking about these to lines in the view:
contentBinding: 'App.itemController',
and
App.itemController.removeItem(item);
How do you structure this?
3. I realize everything is a work in progress and quite new with the name change and all but the documentation is quite unclear. The examples use the old namespace SC and there are lot of things missing on emberjs.com compared to the Sproutcore 2.0 guides, for example collections, arraycontrollers. I read somewhere here that collections will be phased out. Is that true and should I use #each instead?
Thanks for your help and for an awesome framework!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.) 如果您想使用
,您将需要一个计算属性。它可能在您的模型或视图上。另一种技术是使用 Ember.Button:
{{#view Ember.Button tagName="a" target="..." action="..."}}...{{/view}}
2.) 通常,您需要在模板中而不是在视图中声明控制器绑定。例如:
{{#view App.ItemListView contentBinding="App.itemController"}}
3.)
#collection
帮助器可能会被弃用,因此您应该使用改为#each
。1.) If you want to use
<a href="...">
, you will need a computed property. It could be on your model or on a view. Another technique would be to use Ember.Button:{{#view Ember.Button tagName="a" target="..." action="..."}}...{{/view}}
2.) Typically you'll want to declare your controller binding in the template, rather than in the view. For example:
{{#view App.ItemListView contentBinding="App.itemController"}}
3.) The
#collection
helper will likely be deprecated, so you should probably use an#each
instead.