【问题标题】:Ember. Using built-in views inside your own view余烬。在您自己的视图中使用内置视图
【发布时间】:2015-06-12 11:53:58
【问题描述】:

我有一个模型,其中包含有关选择、输入等信息。我找到了将它连接到我的视图的方法。

export default Em.View.extend({
  didInsertElement: function(){
    var content = this.get('content'),
    self = this;
    content.content[0].default.forEach(function(item,i){
      if (item.tag == 'select') {
        self.$().append('<select></select>');
        item.content.forEach(function(item){
          self.$('select:last-child').append('<option>'+item.option+'</option>');
        });
      }
      else if (item.tag == 'input') {
        self.$().append('<input type='+item.type+'>');
      }
    });
  }
});

有没有办法不使用:

self.$('select:last-child').append('<option>'+item.option+'</option>');

还有一些

Ember.compile('{{Ember.select content=options valueBinding=example}}');

谢谢!

附:抱歉,如果您觉得我的做法很愚蠢,我刚开始学习 Ember,可能我仍然以 jquery 的方式思考。对不起我的英语!

【问题讨论】:

  • 视图的实际用途是什么?它在表面上提供了哪些功能?是的,您的代码过度依赖 jQuery。
  • 任务是从 JSON 文件构建表单,其中部分表单可以更改,只需编辑 JSON 即可删除。
  • 只需在项目中添加一个isSelectisInput,然后使用普通的if helpers。

标签: javascript ember.js view


【解决方案1】:

我的解决办法是:

{{#each item in defaultArray}}
    {{#if item.isSelect}}
    {{view "select" content=item.content optionLabelPath="content.option" prompt=""}}
    {{/if}}
    {{#if item.isRadio}}
    {{item.label}}
    {{#each item in item.content}}
     <input type="checkbox" class="radio" {{bind-attr name="item.name" id="item.id" value="item.option"}}>
     <label {{bind-attr for="item.id"}}>{{item.option}}</label>
    {{/each}}
    {{/if}}
{{/each}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2014-05-15
    • 2012-08-10
    • 2021-02-15
    • 2010-12-06
    相关资源
    最近更新 更多