gpt4 book ai didi

sproutcore - 使用Sproutcore 2将参数从 View 传递到 Controller

转载 作者:行者123 更新时间:2023-12-03 13:38:23 24 4
gpt4 key购买 nike

如果我有 Controller :

HTH.todosController = SC.ArrayProxy.create({
content: HTH.store.find(HTH.Todo)
});

我可以显示执行此操作的所有待办事项:
...
<script type="text/x-handlebars">
{{#collection contentBinding="HTH.todosController"}}
{{content}}
{{/collection}}
</script>
...

但是,如何从 View 中显示具有特定ID的待办事项呢?
...
<script type="text/x-handlebars">
{{#view contentBinding="HTH.todosController.specific" specificId="1"}}
{{content}}
{{/view}}
</script>
...

最佳答案

这是一个解决方案in jsfiddle,您可以像在示例中一样在 View 的 Handlebars 定义中定义待办事项的特定ID。有点粗糙,但是可以用。

Handlebars

<script type="text/x-handlebars">
{{view App.SpecificIdWrapper}}
<hr />
{{#collection App.TodosView contentBinding="App.todosController"}}
{{content.name}}
{{/collection}}
<hr />
{{view App.SelectedToDoView}}
</script>

<script type="text/x-handlebars" data-template-name="specificId">
{{#view App.SpecificIdView dataBinding="App.todosController.content" specificId=3}}
Specific Todo Id: {{content.id}}
<br />
Specific Todo Name: {{content.name}}
{{/view}}
</script>

<script type="text/x-handlebars" data-template-name="selectedToDo">
{{#view contentBinding="App.todosController.selectedToDo.content"}}
Selected Todo Id: {{content.id}}
{{/view}}

JavaScript:
App = Ember.Application.create();

App.SpecificIdWrapper = Ember.View.extend({
templateName: 'specificId'
});

App.SpecificIdView = Ember.View.extend({
content: null,
data: null,
specificId: null,

_getTodoById: function() {
var data = this.get('data');
if(data) {
for(var i=0;i<data.length;i++) {
if(data[i].get('id') === this.get('specificId')) {
this.set('content', data[i]);
break;
}
}
}
},

// This will make sure the content is set when the view is rendered
didInsertElement: function() {
this._getTodoById();
},
// And this will update the content whenever specificId is changed
specificIdDidChange: function() {
this._getTodoById();
}.observes('specificId')
});

App.SelectedToDoView = Ember.View.extend({
templateName: 'selectedToDo'
});

App.TodosView = Ember.CollectionView.extend({
itemViewClass: Ember.View.extend({
click: function() {
App.todosController.set('selectedToDo', this);
}
})
});

App.todosController = Ember.ArrayController.create({
content: [
Ember.Object.create({id: 1, name: "obj1"}),
Ember.Object.create({id: 2, name: "obj2"}),
Ember.Object.create({id: 3, name: "obj3"}),
Ember.Object.create({id: 4, name: "obj4"}),
Ember.Object.create({id: 5, name: "obj5"}),
Ember.Object.create({id: 6, name: "obj6"})
],
selectedToDo: null
});

关于sproutcore - 使用Sproutcore 2将参数从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8028779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com