0

我使用 Ember.View 显示了一个文本框。在模型中,我已经指定了输入的所有细节。

App.Display = DS.Model.extend({
inputText: DS.attr('string'),
width: DS.attr('string'),
height: DS.attr('string'),
className: DS.attr('string')
})
App.Display.FIXTURES = [{
id: '1',
innnerText : 'helo',
width: '197px',
height: '25px',
className: 'DisplayClass'
}]

从模型中,我如何将 className 、 width、height 和 innerText 附加到显示单元。

这是我的显示视图

 <script type="text/x-handlebars" data-template-name="_display">
{{#view 'App.DisplayView'}}{{/view}}
 </script>

 App.DisplayView = Ember.View.extend({
tagName: 'input',

});

 App.DisplayController = Ember.ArrayController.extend({
actions: {

}
});

如何通过控制器将模型数据(即innerText、dimensions、className)填充到视图中。注意:我没有使用任何 this.resource('somename')

在 IndexRoute 我设置了控制器

  setupController: function (controller, model) {
    controller.set('model', model);
    this.controllerFor('Display').set('model', model.displayInput);

在 IndexRoute

App.IndexRoute = Ember.Route.extend({
model: function(){
    return {
        //findall of model name 
        displayInput : this.store.findAll('display')
 }
 }

现在使用模型来设置和获取输入的值

4

1 回答 1

2

JS Bin 的工作演示。您正在使用视图 - 现在已弃用的功能 - 而不是组件,这使代码看起来不是很好,并且它不是实现所需行为的理想工具。相反,我将您的方法转换为使用组件。此外,在您定义的夹具中,innnerText而不是inputText.

所以,让我们从组件开始。代码:

App.DisplayComponentComponent = Ember.Component.extend({
   tagName: 'input',
  attributeBindings: ['style', 'value'],
  style: Ember.computed('model', 'model.{width,height}', function() {
    var ret = '',
        width = this.get('model.width'),
        height = this.get('model.height');

    if (width) {
      ret += 'width: ' + width + ';';
    }

    if (height) {
      ret += 'height: ' + height + ';';
    }

    return ret;
  })
});

组件模板:

<script type="text/x-handlebars" data-template-name="display-component">
</script>

然后,正确的夹具:

App.Display.FIXTURES = [{
id: '1',
inputText : 'helo',
width: '197px',
height: '25px',
className: 'DisplayClass'
}];

你的也有问题model。我认为在模型中为显示控制器初始化模型会更容易setupController

setupController: function (controller, model) {
    controller.set('model', model);
    this.store.findAll('display').then(function(displays) {
        this.controllerFor('display').set('model', display.get('firstObject'));
    });
}

然后,如果您想使用它,请这样做(我正在使用您的示例和_display模板,但我不知道您如何使用它):

<script type="text/x-handlebars" data-template-name="_display">
{{display-component model=model class=model.className value=model.inputText}}
</script>

我必须假设该_display模板用于显示控制器,因为您的问题根本不清楚。

于 2015-06-20T19:23:58.320 回答