控制器动作创建子视图?

时间:2013-01-10 15:50:33

标签: ember.js

使用最新的ember.js构建,我正在尝试使用控制器操作来创建一个childView并将其推送到当前视图中。但我无法弄清楚如何与相关视图交谈。

在我的search.handlebars中:

<p>Results:</p>
{{#each animal in someResults}}
  <li><a {{action showAnimal animal}}>{{animal.species.commonName}}</a></li>
{{/each}}

在App.SearchController中,我有:

showAnimal: function(animal) {
  // Now what?? The below is obviously wrong
  this.animalView = App.AnimalView.create({controller: animal});
  var childView = App.SearchView.createChildView(this.animalView);
  App.SearchView.get('childViews').pushObject(childView);
}

showAnimal内可见的对象是:

  • animal - 好的
  • this.container - 在这里看不到任何有用的内容
  • this.target - 看似路由器?

无论如何,我很困惑。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

您是否有理由使用{{action}}代替{{linkTo}}助手? {{linkTo}}将生成与助手一起传递的动物的链接。 http://emberjs.com/guides/templates/links/

然后,您可以按the Router API Guides

中所述定义路线

答案 1 :(得分:0)

认识到我的问题有点神秘/具体情况,这是我最终做的事,如果有人关心的话。请注意,某些API随后发生了变化。

回想一下,我正在尝试在当前视图中弹出一个jQuery UI对话框。 Luke Melia's great article on this topic是我原始方法的基础。

在把手中:

{{#each animal in someResults}}
  <li><a {{action showAnimal animal target="view" href=true}}>{{animal.species.commonName}} </a></li>
{{/each}}

然后在App.SearchView:

App.SearchView = Ember.View.extend({
  templateName: 'my-appname/~templates/search',
  showAnimal: function(animal) {
    animalDialog = App.AnimalView.create()
    animalDialog.set('context', animal);
    animalDialog.append();
  }
});

只是为了完整性,这里是AnimalView。

App.AnimalView = JQ.Dialog.extend({
  templateName: 'my-appname/~templates/animal',
  autoOpen: true,
  modal: true,
  closeText: "Ok"
});
相关问题