我无法理解为什么我无法将数据可视化。如果我查看谷歌应用程序和控制台(App.model.store)他们在那里,但每当我尝试将它们插入Handlebars时,没有任何反应。 更令人困惑的是没有显示错误。
我准备了一个简化版的应用程序发布在这里:
<script type="text/x-handlebars" data-template-name="index">
<header>
<ul>
<li>{{#linkTo "cal"}} Home {{/linkTo}}</li>
<li>{{#linkTo "location"}} location {{/linkTo}}</li>
</ul>
</header>
<section class="content">
{{outlet}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="cal">
<div class="calendar">
Hello!!!
<h2>{{name}}</h2>
</div>
</script>
window.WebCalendar = Ember.Application.create();
WebCalendar.ApplicationAdapter = DS.FixtureAdapter.extend();
WebCalendar.Store = DS.Store.extend({
revision: 12,
adapter: 'DS.FixtureAdapter'
});
/////// ROUTER
WebCalendar.Router.map(function() {
this.resource('index', {path: '/'}, function() {
this.resource("cal", {path: '/'});
this.resource("location", {path: '/location'});
});
});
WebCalendar.CalRoute = Ember.Route.extend({
model: function(){
return this.store.find('cal').toArray();
}
});
////// Model
WebCalendar.Cal = DS.Model.extend({
name: DS.attr('string'),
days_label: DS.attr('string'),
months_label: DS.attr('string'),
days_per_month: DS.attr('number'),
current_date: DS.attr('date')
});
WebCalendar.Cal.FIXTURES = [
{
"id": 1,
"name": "Jhon",
"days_label": ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
"months_label": ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'],
"days_per_month": [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
}
];
我真的不确定我的JSON文件.. 任何帮助都非常感谢!
这里是codepen
P.S。如果您还有任何建议如何在控制台中正确检查余烬,那将是超级!
答案 0 :(得分:2)
进行find()
调用是为了查找所有属于cals的模型,因此无论如何都会返回一个数组,因此不需要toArray()
鉴于正在返回一个数组,您需要遍历该数组以查找名称,因此代码应为:
<script type="text/x-handlebars" data-template-name="cal">
<div class="calendar">
Hello!!!
{{#each cal in model}}
<h2>{{cal.name}}</h2>
{{/each}}
</div>
</script>
WebCalendar.CalRoute = Ember.Route.extend({
model: function(){
return this.store.find('cal');
}
});
同样在你的模型中你的days_label, months_label, days_per_month
被定义为字符串,但在你的json中它们是数组。
Ember.Logger.log();
将为您记录控制台:
前
WebCalendar.CalRoute = Ember.Route.extend({
model: function(){
var cals = this.store.find('cal');
Ember.Logger.log(cals);
return cals
}
});