gpt4 book ai didi

javascript - 模型中的值未更新到 View

转载 作者:行者123 更新时间:2023-12-02 16:03:52 26 4
gpt4 key购买 nike

我已经使用 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 和 insideText 附加到显示单元。

这是我的显示 View

 <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: {

}
});

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

在 IndexRoute 中我设置了 Controller

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

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

最佳答案

Working demo on JS Bin.您正在使用 View (现在已弃用的功能)而不是组件,这使得代码看起来不太好,并且它不是实现您想要的行为的理想工具。相反,我将您的方法转换为使用组件。此外,在 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'
}];

您的模型也存在问题。我认为在 setupController 模型中初始化显示 Controller 的模型会更容易。

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 模板用于显示 Controller ,因为您的问题根本不清楚。

关于javascript - 模型中的值未更新到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30957802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com