在我解决问题之前有一些背景知识。我有一些数据(在这种情况下是随机数),我需要能够以多种方式可视化这些数据。我只在小提琴中实现了一个表格和线视图,在产品中我将有更多的方法来可视化数据(饼图、条形图等),并且会有多个部分。
这是小提琴。
我可以正确更改我想要显示的类型,但是每当我更新嵌套视图时,我似乎都无法更新视图。我可能错过了一些非常简单的东西,所以这个问题的标题可能已经加载了。如果是这种情况,我很抱歉,但我会非常感谢任何帮助。
车把:
<script type="text/x-handlebars" data-template-name="index">
{{#each App.Controller.content}}
{{#view App.ChartTypeContainer contentBinding="this"}}
{{#each chartTypesWithSelected}}
<a href="#" {{action switchChartType this target="view"}}>
{{#if selected}}
<strong>{{display}}</strong>
{{else}}
{{display}}
{{/if}}
</a>
{{/each}}
{{/view}}
{{#if currentView}}
{{view currentView}}
{{/if}}
{{/each}}
</script>
<script type="text/x-handlebars" data-template-name="table">
<table>
<thead>
<tr>
{{#each view.data.headings}}
<th>{{name}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each view.data.data}}
<tr>
{{#each values}}
<td>{{this}}</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
</script>
<script type="text/x-handlebars" data-template-name="line">
</script>
js:
App = Em.Application.create({
getRandomData: function(){
// Generate between 1-3 random headings
var headings=[],
headingCount = (Math.floor(Math.random() * 5) + 1),
data=[];
for(var i = 0; i < headingCount; i++){
headings.push({name: 'heading ' + i});
}
// Generate between 5 and 10 rows of random data
for(var i = 0; i< (Math.floor(Math.random() * 10) + 5);i++){
var values = [];
for(var j=0; j< headingCount;j++){
values.push((Math.floor(Math.random() * 100) + 1));
}
data.push({values: values});
}
return {headings: headings, data: data};
},
ready: function(){
Em.View.create({templateName:'index'}).appendTo('body');
}
});
App.chartFactory = Em.Object.create({
create: function(key, data){
switch(key){
case 'table':
return App.TableView.create({data: data || App.getRandomData()});
case 'line':
return App.LineView.create();
default:
return;
}
}
});
/* MODELS */
App.ChartType = Em.Object.extend({
key: '',
display: ''
});
App.Section = Em.Object.extend({
title: '',
chartTypes: [],
chartTypesWithSelected: function(){
var currentSelected = this.get('selectedChartType');
var types = this.get('chartTypes');
var thing = types.map(function(item){
item.set('selected', item.key === currentSelected);
return item;
});
return thing;
}.property('chartTypes.@each', 'selectedChartType'),
data: {},
selectedChartType: '',
selectedChartTypeObserver: function() {
var selectedChartType = this.get('selectedChartType');
alert('changin chart type to: ' + selectedChartType);
App.chartFactory.create(selectedChartType);
}.observes('selectedChartType'),
currentView: null
});
/* VIEWS */
App.ChartTypeContainer = Em.View.extend({
switchChartType: function(chartType) {
this.get('content').set('selectedChartType', chartType.key);
}
})
App.TableView = Em.View.extend({
templateName: 'table',
data: {}
});
App.LineView = Em.View.extend({
templateName:'line',
data: {},
didInsertElement: function(){
var data = App.getRandomData();
var headings = data.headings.map(function(item){
return item.name;
});
var series = data.data.map(function(item){
return {data: item.values};
});
this.$().highcharts({
title: null,
series: series,
xAxis: {categories: headings},
yAxis: {min: 0, max: 100, title: {text: 'Random Numbers'}}
});
}
})
/* CONTROLLER */
App.Controller = Em.Object.create({
content: [
App.Section.create({
title: 'First Section',
chartTypes: [
App.ChartType.create({key: 'table', display: 'Table Display'}),
App.ChartType.create({key: 'line', display: 'Line Display'})
],
selectedChartType: 'table', // CHANGE HERE TO SEE THE OTHER VIEW, use 'line' or 'table'
currentView: App.chartFactory.create('table') // CHANGE HERE TO SEE THE OTHER VIEW, use 'line' or 'table'
})
]
});
更新:
在下一个运行周期中设置新创建的视图Ember.run.next
似乎可以正确产生所需的行为。更新小提琴