itemController for hasMany [英] itemController for hasMany

查看:96
本文介绍了itemController for hasMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决以下问题:如何将一个itemController分配给hasMany关系中的子元素?

I've been struggling with the following question for a while: How do I assign an itemController to child elements in a hasMany relationship?

我的用例如下:我有一个Ember.View(ProjectView),其中我使用Google Maps API操纵地图上的区域。我有一个区域模型和项目模型有很多区域。

My use case is the following: I have an Ember.View (ProjectView) in which I manipulate areas on the map using the Google Maps API. I have a model for the Area and the Project model "hasMany" areas.

我的应用程序中没有任何保存按钮等,而是将更改同步到发生更改时的后端(使用去抖动功能)。为了避免错误的inFlight错误,我使用修改版本的Ember.AutoSaving插件 https:// github .com / gaslight / ember-autosaving ,它缓冲我的更改,并在模型准备就绪时与模型同步。然而,为了使用这个,我需要使用这个Mixin应用一个itemController到我的hasMany关系的每个区域。我该怎么做?

I do not have any save buttons or the like in my app, but rather sync changes to the backend when a change occurs (using a debounce function). In order to avoid nasty inFlight errors, I am using a modified version of the Ember.AutoSaving plugin https://github.com/gaslight/ember-autosaving, which buffers my changes and synchronizes them with the model when it's ready. However, in order to use this, I need to apply an itemController using this Mixin to every Area in my hasMany relation. How do I go about this?

推荐答案

Handlebars {{each}} 帮助器有一个 itemController 选项。当指定此选项时,每个对象将被控制器实例包装。所以这样的东西应该可以工作:

The Handlebars {{each}} helper has an itemController option. When this option is specified each object will be wrapped by a controller instance. So something like this should work:

//from your project template
{{#each area in areas itemController="area"}}
  <pre>
    area is an AreaController: {{area}}
    area.content is a reference to the model: {{area.content}}
{{/each}}

请参阅[handlebars {{each}} API文档]( http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_each )了解更多详情

See the [handlebars {{each}} API docs] (http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_each) for more details

编辑:选项2

作为使用 {{each}} 帮助器,使用 ArrayController 来表示集合,并将其设置为 itemController 属性。例如:

As an alternative to using the {{each}} helper, use an ArrayController to represent the collection and set it's itemController property. For example:

App.AreasController = Ember.ArrayController.extend({
  itemController: 'area'
});

App.AreaController = Ember.ObjectController.extend( Ember.AutoSaving, {
  bufferedFields: ['title', 'body'],
  instaSaveFields: ['postedAt', 'category'],
  titleLength: function() {
    return this.get('title').length;
  }.property('title')
});

// In your project route:
setupController: function(controller, model) {
  this.controllerFor('areas').set('content', model.areas);
}

现在区域控制器将每个项目包装在AreaController代理中。

Now the areas controller will wrap each item in an AreaController proxy.

请参阅 Ember ArrayController API文档

这篇关于itemController for hasMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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