简体   繁体   English

Ember.js:将参数传递给渲染助手

[英]Ember.js: Passing arguments to render helper

Say I have two objects in my application: 1) Topic, 2) Categories. 假设我的应用程序中有两个对象:1)主题,2)类别。

Topic has a route /topic/:topic_id Topic的路由为/topic/:topic_id

Categories doesn't have a URL route. Categories没有URL路由。 It contains the list of all available categories that topics can be in. 它包含主题可以进入的所有可用类别的列表。

On the page for Topic , I would like to show a list of all categories, while highlighting the category that the topic is a part of (the 'active' category) 在“ Topic ”页面Topic ,我想显示所有类别的列表,同时突出显示该主题所属的类别(“活动”类别)


Original Solution 原始解决方案

I had originally solved this with a component as follows (a lot of code removed for brevity): 我最初使用以下组件解决了此问题(为简洁起见,删除了许多代码):

Component Template: 组件模板:

(Goes trough each category and lists them. If the current one is active, it says so.) (进入每个类别并列出它们。如果当前类别处于活动状态,则显示为。)

<script type="text/x-handlebars" id="components/category-list">
  {{#each categories}}
    {{#if this.active}} Active -> {{/if}}
    {{this.name}}
    <br />
  {{/each}}
</script>

Component Object: 组件对象:

(Goes through the models via the CategoriesController, marks one category as the current active category for that topic via a "current" parameter passed in from the {{category-list}} component helper) (通过CategoriesController遍历模型,通过从{{category-list}}组件帮助器传入的“当前”参数将一个类别标记为该主题的当前活动类别)

App.CategoryListComponent = Ember.Component.extend({
  tagName: '',
  didInsertElement: function () {
    var categories = App.CategoriesController;
    var current = this.get('current').get('id');

    categories.get('content').forEach(function (c) {
      if (c.id === current) {
        c.active = true;
      }
    });

    this.set('categories', categories);
  }.observes('current')
});

Displaying the Category List: 显示类别列表:

In the Topic view ('category' is the property of Topic that says what category the topic is in): 在“主题”视图中(“ category”是Topic的属性,用于说明主题所在的类别):

{{category-list current=category}}

This all works, but I have a feeling that it is not the correct way to go about solving the problem. 所有这些都有效,但是我感觉这不是解决问题的正确方法。 Components strike me as something that should be very reusable, and this really only is just an encapsulated part of one view. 组件给我的印象是应该非常可重用的东西,而这实际上仅仅是一个视图的封装部分。


Second Attempt 第二次尝试

Now I am trying to use the category list as a view. 现在,我尝试使用类别列表作为视图。 So now instead of just a Component, I have a CategoriesRoute (not hooked up in the router though), a CategoriesController, and a CagetoriesView. 因此,现在我不仅有一个组件,而且还有一个CategoriesRoute(虽然没有连接到路由器中),一个CategoriesController和一个CagetoriesView。

When the Topic resource loads up, it sets up the categories controller with the loaded model data: 加载主题资源时,它将使用加载的模型数据设置类别控制器:

App.TopicRoute = Ember.Route.extend({
  model: function (params) {
    return Em.RSVP.hash({
      topic: this.store.find('topic', params.topic_id),
      categories: this.store.find('category')
    });
  },

  setupController: function (controller, context) {
    this._super(controller, context.topic);
    this.controllerFor('categories').set('model', context.categories);
  }
});

The CategoriesController is just a standard array controller: CategoriesController只是一个标准的数组控制器:

App.CategoriesController = Ember.ArrayController.extend({});

The CategoriesView is also very simple: CategoriesView也非常简单:

App.CategoriesView = Ember.View.extend({
  templateName: 'categories',
  tagName: ''
});

And finally the categories template: 最后是类别模板:

<script type="text/x-handlebars" id="components/category-list">
  {{#each categories}}
    {{this.name}}
    <br />
  {{/each}}
</script>

I am rendering the list with the {{render}} helper in the Topic template: 我正在主题模板中使用{{render}}帮助器来呈现列表:

{{render "categories" }}

The Problem: 问题:

The above seems to work fine in terms of showing the list of categories. 在显示类别列表方面,上述方法似乎工作良好。 However, I can't figure out how to tell the view which category is active (the category in which the topic is in). 但是,我不知道如何告诉视图哪个类别是活动的(主题所在的类别)。

I looked around on the internet, and it looks like the {{render}} helper used to allow for an optional options hash (which is how I solved the problem with a Component in my first attempt), but it seems to have been removed in some recent version of Ember. 我在Internet上四处张望,看起来好像{{render}}助手用来允许可选的选项哈希(这是我第一次尝试解决Component问题的方式),但是它似乎已被删除。在一些最新版本的Ember中。

I found a StackOverflow entry that mentioned making my own {{render}} helper, but it looks like that is to dynamically change models, or something of that nature. 我找到了一个StackOverflow条目,其中提到了制作自己的{{render}}助手,但看起来是要动态更改模型或类似性质的东西。 I'd like to keep the models backing for the categories view, I just need to be able to tell it which category is active via the Topic.category property. 我想为模型提供类别视图的支持,我只需要能够通过Topic.category属性告诉它哪个类别处于活动状态。

Or, was my first attempt the better solution? 还是我的第一次尝试是更好的解决方案? I am very new to Ember, so I'm not sure what would be the best strategy here. 我对Ember并不陌生,所以我不确定什么是最好的策略。 My instinct tells me I should use my second attempt rather than the first, but I am not positive. 我的直觉告诉我,我应该使用第二次尝试而不是第一次尝试,但是我并不乐观。

Any help is very appreciated. 任何帮助都非常感谢。

You are right in saying that components must be re-usable everywhere and should not be tied to any particular controller. 您说的很对,组件必须在任何地方都可以重用,并且不应与任何特定的控制器绑定。 I would use a view for this. 我会为此使用一个视图。 The way I would do is, I would have a 我要做的就是

App.CategoriesController = Em.ArrayController.extend({ 
  itemController: 'category',

  //some other stuff here

});

for categories and then have an itemController called 类别,然后有一个itemController称为

App.CategoryController = Em.ObjectController.extend({ 
  setActiveState: function() {
   if (this.get('parentController.current.id') === this.get('content.id')) {
     this.set('active', true);
    }
  }

});

and in the template, you could say 在模板中,您可以说

{{#each category in categories}}
  {{category.name}}
{{/each}}

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

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