【发布时间】:2014-09-14 10:45:51
【问题描述】:
在我的 ember 应用程序中,我使用异步加载的数据(使用 ember 数据和其余适配器)与把手一起显示。加载并渲染数据后,我想操纵这些数据的呈现方式(计算它们所在的位置)。这也需要在视口调整大小时完成。
目前我正在尝试使用中建议的方法来解决这个问题 How to catch whether array was inserted in handlebar in Ember? 和 Run jquery at the end of Ember.CollectionView rendering 。 我什至尝试使用 schedule 而不是 scheduleOnce 导致只调用一次我的显示更新方法,并且仍然没有呈现异步加载的数据,并且在加载和呈现数据后该方法不会再次调用。
export default Ember.View.extend({
templateName: 'calendar',
didInsertElement: function() {
this._super();
this.$('#calender_button_new').click(function(){
document.location.href= "index.html#/add-appointment/select-employee";
});
this.$('#calender_button_home').click(function(){
document.location.href= "index.html#/home";
});
},
updateCalendar: function() {
console.log(this.$('.appointment').length);
var view = this;
this.$('.appointment').each(function(index, item){
var id = $(item).attr('data-appointment');
view.controller.store.find('appointment', id).then(function(appointment){
var beginning_base = Math.floor(appointment.get('beginning'));
var end_base = Math.floor(appointment.get('end'));
// get cell where appointment starts, each cell can be identified by the attributes data-time and data-employee
var beginning_block = $('td[data-employee="' + appointment.get('doneBy').get('id') + '"][data-time="' + beginning_base + '"]');
var end_block = $('td[data-employee="' + appointment.get('doneBy').get('id') + '"][data-time="' + end_base + '"]');
// calculate exact position in the cell when the appointment starts, one cell covers an hour but the appointment might not start at full hour
var beginning_offset = beginning_block.outerHeight()*(appointment.get('beginning')-beginning_base);
var end_offset = end_block.outerHeight()*(appointment.get('end')-end_base);
$(item).css({top:beginning_block.offset().top+beginning_offset-59,
left:beginning_block.offset().left,
width: beginning_block.outerWidth(),
height:end_block.offset().top-beginning_block.offset().top+end_offset,
background:'red'});
});
});
},
init: function() {
this._super();
$(window).bind('resize', $.proxy(this.updateCalendar, this));
},
willDestroy: function() {
this._super();
$(window).unbind('resize');
}
});
有了这种方法,就不必寻找要兑现的承诺 (?)。另一方面,这是我能想到为什么这不起作用的唯一原因。 (注意:在手动调整窗口大小时一切正常。只是初始渲染给我带来了麻烦。) 我实际上必须听什么事件才能使其正常工作?
编辑
也许我应该提到我正在使用 ArrayController 并且视图是数组元素的容器。 结构如下:
-Calendar (ArrayController, View with afterRender event)
-Day (Item of Calendar, does not have a view - uses calendar view/template to be displayed)
calendar.hbs-template的相关部分是:
{{#each}}
{{#each appointment in appointments}}
<div class="appointment" {{bind-attr data-appointment="appointment.id"}}>{{appointment.title}}</div>
{{/each}}
{{/each}}
Calendar 的模型只是请求天数(CalendarRoute 的一部分)
export default Ember.Route.extend({
model: function() {
return this.store.find('Day');
},
});
日模型本身:
export default DS.Model.extend({
date: DS.attr('string'),
appointments: DS.hasMany('Appointment', {
async: true,
inverse: null
}),
employees: DS.hasMany('Employee', {
async: true,
inverse: null
}),
opening: DS.attr('number'),
closing: DS.attr('number')
});
预约模式
export default DS.Model.extend({
title: DS.attr('string'),
comment: DS.attr('string'),
doneBy: DS.belongsTo('Employee'),
beginning: DS.attr('number'),
end: DS.attr('number')
});
日模型由控制器支持,但不包含任何相关内容(仅一个计算字段,用于根据日模型的开始和结束时间创建时间段)。
编辑 2
我更新了所有源代码。如果您想知道这是什么样子,请访问http://arshaw.com/fullcalendar/(周视图)。而不是不同的日子,我将员工放在 y 轴上。没有拖放计划,因此我只使用整小时(而不是 30 分钟)的 tiemslots。
【问题讨论】:
-
你会展示你的模板,以及支持模板的模型(可能还有路由和控制器)
-
我编辑了我的第一篇文章。如上所述,也许我应该说我使用 ArrayController...