简体   繁体   English

Ember.js / Ember Data-在模板中呈现hasMany键/值对

[英]Ember.js / Ember Data - Render hasMany key/value pair in template

I have a Document model, which has attributes/properties defined to it using a hasMany relationship. 我有一个Document模型,它具有使用hasMany关系为其定义的属性/属性。 The purpose is to freely be able to define content in different areas of the document like header , body , footer while also creating presentational attributes like color or image . 目的是可以自由定义文档的不同区域(如headerbodyfooter同时还可以创建colorimage等外观属性。

KF.Document = DS.Model.extend
  title: DS.attr 'string'
  documentAttributes: DS.hasMany 'documentAttribute'

KF.DocumentAttribute = DS.Model.extend
  attrKey: DS.attr 'string'
  attrValue: DS.attr 'string'
  document: DS.belongsTo 'document'

Document.documentAttributes returns a DS.ManyArray so in order to render it I could do the following: Document.documentAttributes返回一个DS.ManyArray因此为了呈现它,我可以执行以下操作:

{{#each da in documentAttributes}}
  <p>{{da.attrKey}} - {{da.attrValue}}</p> <!-- returns: "header - this is my header" -->
{{/each}}

The problem is that I want to access the keys directly (using a proxy?) so I can bind the data directly like so: 问题是我想直接访问密钥(使用代理?),因此可以像这样直接绑定数据:

{{textarea value=documentAttributes.header cols="80" rows="6"}}
<img {{ bindAttr src="documentAttributes.imageSrc" }} >
{{textarea value=documentAttributes.footer cols="80" rows="6"}}

How should I approach this? 我应该如何处理?

An approach could be to enhance an em view (for the brave maybe a component as well), or create a proxy, that receives a DocumentAttribute object and defines dynamically a property with name the value of the attrKey and return the value of the attrValue. 一种方法可能是增强em视图(也可能是勇敢的组件),或者创建一个代理,该代理接收DocumentAttribute对象并动态定义一个属性,其名称为attrKey的值,并返回attrValue的值。 You could achieve this with the following code , 您可以使用以下代码实现此目标,

http://emberjs.jsbin.com/ehoxUVi/2/edit http://emberjs.jsbin.com/ehoxUVi/2/edit

js JS

App = Ember.Application.create();

App.Router.map(function() {
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return createProxy(App.DocumentAttribute.create());
  }
});

App.DocumentAttribute = Ember.Object.extend({
  attrKey:"theKey",
  attrValue:"theValue"
});



 function createProxy(documentAttr){
  App.DocumentAttributeProxy = Ember.ObjectProxy.extend({
    createProp: function() {
      _this = this;
      var propName = this.get('attrKey');
      if (!_this.get(propName)) {
        return Ember.defineProperty(_this, propName, Ember.computed(function() {
          return _this.get('attrValue');
        }).property('attrKey'));
      }
    }.observes('content')
  });
  var proxy = App.DocumentAttributeProxy.create();
  proxy.set('content',documentAttr);
  return proxy;
}

HB HB

<script type="text/x-handlebars">
    <h2>Welcome to Ember.js</h2>

    {{outlet}}
  </script>

<script type="text/x-handlebars" data-template-name="index">
    {{attrKey}}
    <br/>
    {{attrValue}}

    <br/>
    {{theKey}}
  </script>

I couldn't get melc's solution to work with the DS.ManyArray returned by the relationship. 我无法获得melc的解决方案来与关系返回的DS.ManyArray一起使用。

But his examples gave me some ideas and I did the following. 但是他的例子给了我一些想法,我做了以下工作。 Basically mapping the items through a "shortcut key" on the controller. 基本上,通过控制器上的“快捷键”映射项目。

KF.DocumentsShowRoute = Ember.Route.extend
  setupController: (controller, model) ->
    controller.set('model', model)
    # make an `Object` to store all the keys to avoid conflicts
    controller.set('attrs', Ember.Object.create())

    # loop through all `DocumentAttributes` in the `DS.ManyArray` returned by the relationship,
    # get the `attrKey` from each item and make a shortcut to the item under `attrs` object
    model.get('documentAttributes').forEach (item, index, enumerable) -> 
      key = item.get('attrKey')
      controller.get('attrs').set(key, item)

The template, where header is the attrKey 模板,其中标头attrKey

{{input value=attrs.header.attrValue}}

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

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