使用 Sproutcore 2 将参数从视图传递到控制器

发布于 2024-12-14 08:31:34 字数 559 浏览 0 评论 0原文

如果我有一个控制器:

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

我可以显示所有执行此操作的待办事项:

...
<script type="text/x-handlebars">
  {{#collection contentBinding="HTH.todosController"}}
    {{content}}
  {{/collection}}
</script>
...

但是如何显示来自视图的具有特定 ID 的待办事项?

...
<script type="text/x-handlebars">
  {{#view contentBinding="HTH.todosController.specific" specificId="1"}}
    {{content}}
  {{/view}}
</script>
...

If I have a controller:

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

I can display all todos doing this:

...
<script type="text/x-handlebars">
  {{#collection contentBinding="HTH.todosController"}}
    {{content}}
  {{/collection}}
</script>
...

But how would I display a todo with a specific ID coming from the view?

...
<script type="text/x-handlebars">
  {{#view contentBinding="HTH.todosController.specific" specificId="1"}}
    {{content}}
  {{/view}}
</script>
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清欢 2024-12-21 08:31:34

这是jsfiddle中的一个解决方案,允许您在视图的把手定义中定义待办事项的特定ID正如您在示例中所示。这有点粗糙,但很有效。

车把:

<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
});

Here's a solution in jsfiddle allowing you to define the specific id of the todo in the handlebars definition of the view as you have in your example. It's a bit crude, but it works.

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
});
愿得七秒忆 2024-12-21 08:31:34

您是否正在考虑 CRUD 风格的主/详细信息工作流程?

如果您是的话,这里有一系列教程< /a> 我刚刚为 SC2 中的 CRUD 操作编写。

基本上,我将双击处理程序附加到表行,该处理程序触发状态图操作以在模式对话框中显示详细信息。

CollectionView : SC.CollectionView.extend({
contentBinding: 'App.resultsController',

itemViewClass: SC.View.extend({
  tagName: 'tr',

  // Spit out the content's index in the array proxy as an attribute of the tr element
  attributeBindings: ['contentIndex'],

  willInsertElement: function() {
    this._super();

    // Add handler for double clicking
    var id = this.$().attr('id');
    this.$().dblclick(function() {
      App.statechart.sendAction('showUser', $('#' + this.id).attr('contentIndex'));
    });
  }
})

本教程的第 4 部分显示我是怎么做到的。

希望这有帮助。

Are you thinking about a CRUD style master/details workflow?

If you are, here's a series of tutorials I just wrote for CRUD operations in SC2.

Basically, I attach a double click handler to the table row that triggers a state chart action to display the details in a modal dialog.

CollectionView : SC.CollectionView.extend({
contentBinding: 'App.resultsController',

itemViewClass: SC.View.extend({
  tagName: 'tr',

  // Spit out the content's index in the array proxy as an attribute of the tr element
  attributeBindings: ['contentIndex'],

  willInsertElement: function() {
    this._super();

    // Add handler for double clicking
    var id = this.$().attr('id');
    this.$().dblclick(function() {
      App.statechart.sendAction('showUser', $('#' + this.id).attr('contentIndex'));
    });
  }
})

Part 4 of the tutorial shows how I did this.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文