简体   繁体   English

在Ember.js中构建嵌入式/内联表单的“正确”方法是什么

[英]What is the 'correct' way to build an embedded / inline form in Ember.js

I'm wondering what the best way to build an embedded or inline form is in Ember.js. 我想知道在Ember.js中构建嵌入式嵌入式表单的最佳方法是什么。 An example of this type of form would be the new tweet form on the Twitter timeline page, the comment form on a GitHub issue, or the answer form on this very site. 这种类型的表单的一个示例是Twitter时间轴页面上的新tweet表单,GitHub问题上的评论表单或该站点上的答案表单。 Such a form has a few properties that make it desirable from a UX perspective, namely: 这种形式具有一些特性,使其从UX角度来看是理想的,即:

  • content is shown alongside the form, allowing the user to easily reference other posts / comments / tweets. 内容显示在表格旁边,使用户可以轻松参考其他帖子/评论/推文。
  • it encourages contributions by not requiring a user to navigate away from the page. 它通过不需要用户离开页面来鼓励做出贡献。

The obvious approach seems to be to use a {{render}} helper to encapsulate all the form's logic into a separate view and controller. 显而易见的方法似乎是使用{{render}}帮助程序将所有表单逻辑封装到单独的视图和控制器中。 But the issue I've run into with this approach is that there's no clear way to set the form's model. 但是我在使用这种方法时遇到的问题是,没有明确的方法来设置表单的模型。

Ideally the form would be backed by an Ember.ObjectController with a freshly created model as its content. 理想情况下,该表单将由Ember.ObjectController支持,以新创建的模型作为其内容。 However, the {{render}} helper delegates the task of providing a model object to this controller to its parent, which means that I need to replicate the logic of providing an empty model object to this view at every place I use it in my app. 但是, {{render}}助手将为该控制器提供模型对象的任务委托给它的父对象,这意味着我需要在我使用的每个位置复制向该视图提供空模型对象的逻辑。应用程序。

So what's the best practice concerning this scenario? 那么关于这种情况的最佳实践是什么? Is it that the {{render}} helper the right approach and I'm just missing something obvious? {{render}}助手是否是正确的方法,而我只是缺少明显的东西? Or is there a better approach I could be taking? 还是我可以采用更好的方法?

Typically in Ember, routes are used to get models in the right place. 通常在Ember中,使用路线将模型放置在正确的位置。 You could always create a new CommentController (or whatever model is used for the form), and in the route's setupController , find the right model and set it on that controller: 您总是可以创建一个新的CommentController(或用于表单的任何模型),然后在路由的setupController ,找到合适的模型并将其设置在该控制器上:

SomeRoute: {
  ...
  setupController: function() {
    ... // normal setup for this route, possible this._super()
    this.controllerFor('comment', this.store.find('comment', 1));
  }
}

(untested pseudocode) (未经测试的伪代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM