Ember:如何设置在选择视图中使用的控制器型号 [英] Ember: How to set controller model for use in select view

查看:131
本文介绍了Ember:如何设置在选择视图中使用的控制器型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅: http://jsfiddle.net/cyclomarc/VXT53/8/

我有一个选择视图,其中我只想列出灯具数据中可用的所有作者。因此,我尝试使用单独的控制器,其中我想设置content = App.Author.find(),但是不起作用。

I have a select view in which I simply want to list all the authors available in the fixtures data. I therefore try to use a separate controller in which I want to set the content = App.Author.find(), but that doesn't work ...

App.AuthorsController = Ember.ArrayController.extend({
  //content: App.Store.findAll(App.Author)
  //content: App.Author.find()
});

然后我想在selectView中使用AuthorController for contentBinding,但这不是succsesful .. 。

I then want to use the AuthorController for contentBinding in the selectView, but also this is not succsesful ...

{{view Ember.Select
       contentBinding="App.AuthorsController"
       optionValuePath="content.id"
       optionLabelPath="content.name" 
       valueBinding="App.PublicationsEditController.author"
       prompt="Select an author"
   }}

用例是我有一个出版物,其中设置了一个作者(例如Marc),我想允许用户将其更改为可供选择的作者之一,然后将新的所选作者绑定到发布模型,以便在保存新作者后保存。

The use case is that I have a publication in which an author is set (e.g. Marc) and I want to allow the user to change this to one of the available authors and then bind the new selected author to the publication model so that after a save the new author is saved.

推荐答案

我猜这是你以后的: http://jsfiddle.net/VXT53/10/

我不得不做一些改变,首先你的路由器地图在哪里错误的是,编辑段落必须跟随id匹配您的模板名称 pulifying / edit

I had to do a couple of changes, first your router map where slightly wrong, the edit segment had to go after the id to match your template name pulications/edit:

App.Router.map(function () {
  this.resource('publications', { path: '/' }, function () {
    this.route("edit", { path: "/edit/:publication_id" });
  });
});

您的 Ember.Select 类而不是某些内容,要正确设置我已经定义了一个 PublicationsEditControllet 以通过API访问 AuthorsController 的内容:

Your Ember.Select where binding to a class instead to some content, to set it up correctly I've defined a PublicationsEditControllet to requiere trough the needs API access to the AuthorsController's content:

App.PublicationsEditController = Ember.ObjectController.extend({
  needs: ['authors'],
  ...
});

这是你如何使用它来选择:

this is how you then use it for your select:

{{view Ember.Select
   contentBinding="controllers.authors.content"
   optionValuePath="content.id"
   optionLabelPath="content.name" 
   valueBinding="content.name"
   selectionBinding="selectedAuthor"
   prompt="Select an author"
}}

此外,我创建了一个 setupController hook,用于设置 AuthorsController 对作者列表的内容:

Furthermore I've created a setupController hook which is used to set the AuthorsController's content to the list of authors:

App.PublicationsRoute = Ember.Route.extend({
  model: function () {
    return App.Publication.find();
  },
  setupController: function(controller, model) {
    this._super(controller, model);
    this.controllerFor('authors').set('content', App.Author.find());
  }
});

最后在 PublicationsEditController 是一个新的属性附加 selectedAuthor 保存当前所选作者的绑定等。

And lastly on you PublicationsEditController is a new property attached selectedAuthor to hold the currently selected author for binding etc.

App.PublicationsEditController = Ember.ArrayController.extend({
  needs: ['authors'],
  selectedAuthor: null
});

我想这应该现在工作,让你进一步。

I guess this should work now and brings you one step further.

希望有帮助。

这篇关于Ember:如何设置在选择视图中使用的控制器型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆