【问题标题】:values from model are not updating to the view模型中的值未更新到视图
【发布时间】:2015-06-20 19:03:16
【问题描述】:

我使用 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);

在索引路由中

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

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

【问题讨论】:

  • 这看起来像是setupController 的默认行为,您的方法中是否有更多逻辑?

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


【解决方案1】:

Working demo on JS Bin.您正在使用视图 - 现在已弃用的功能 - 而不是组件,这使代码看起来不太好,并且它不是实现所需行为的理想工具。相反,我将您的方法转换为使用组件。此外,在 Fixtures 中,您定义了 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 模板是用于显示控制器的,因为你的问题根本不清楚。

【讨论】:

  • 将视图称为'App.DisplayView' 而不是'display' 甚至更早被弃用。我同意改用组件的建议。
  • 谢谢。但是这里 this.get('model.width') 是未定义的。 @丹尼尔
  • 甚至没有附加className和value。如何解决这个@Daniel
  • 我在实现中使用的视图,现在是否已弃用?我参考了一些建议我使用 Ember.View 的教程。谢谢你提供的详情。 @丹尼尔
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多