模型中的值未更新到视图

时间:2015-06-20 19:03:17

标签: javascript ember.js ember-model ember.js-view ember-controllers

我使用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附加到人机界面。

这是我的displayView

 <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(&#39; somename&#39;)

在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')
 }
 }

现在使用model来设置和获取输入值

1 个答案:

答案 0 :(得分:2)

Working demo on 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模板适用于显示控制器,因为您的问题根本不清楚。

相关问题