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到人機界面。

這是我的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,尺寸的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') 
} 
} 

我們用模型來設置和獲取輸入的值

+0

這看起來像'setupController'的默認行爲,你有更多的方法邏輯? – locks

回答

2

Working demo on JS Bin.您現在正在使用視圖 - 不推薦使用的功能 - 而不是組件,這會使代碼看起來不是很好,也不是t理想的工具來實現你想要的行爲。相反,我將您的方法轉換爲使用組件。此外,在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模板是用於顯示控制器,因爲你的問題根本不清楚。

+0

引用該視圖作爲「App.DisplayView」而不是「display」已被棄用,甚至更早。我同意使用組件的建議。 – locks

+0

謝謝。但是這裏this.get('model.width')是未定義的。 @Daniel – vbharath

+0

即使沒有添加className和value。如何解決此問題@丹尼爾 – vbharath