EmberJS视图呈现两次

时间:2014-03-24 17:30:44

标签: ember.js fullcalendar ember-app-kit

新的ember并试图找出最佳实践。当我切换到日历模板时,问题是fullCalendar呈现两个日历。

这是控制台输出:

Attempting transition to calendar ember.js?body=1:3499
Transition #3: calendar: calling beforeModel hook ember.js?body=1:3499
Transition #3: calendar: calling deserialize hook ember.js?body=1:3499
Transition #3: calendar: calling afterModel hook ember.js?body=1:3499
Transition #3: Resolved all models on destination route; finalizing transition. ember.js?         body=1:3499
Rendering calendar with <app@view:calendar::ember635> Object {fullName: "view:calendar"}         ember.js?body=1:3499
Transitioned into 'calendar' ember.js?body=1:3499
Transition #3: TRANSITION COMPLETE. 

这是我的代码:

router.es6

var Router = Ember.Router.extend({
  location: 'history'
});

Router.map(function() {
  //...
  this.route('calendar');
  //...
});

export default Router; 

路由/ calendar.es6

export default Ember.Route.extend();

视图/ calendar.es6

var CalendarView = Ember.View.extend({
  didInsertElement: function() {
    $('#calendar').fullCalendar();
  }
});

export default CalendarView;

模板/ calendar.hbs

{{#view "calendar"}}
  <nav>
    <h1>Schedule</h1>
  </nav>
  <article id="schedule">
    <section>
      <div id='calendar'></div>
    </section>
  </article>
{{/view}}

3 个答案:

答案 0 :(得分:1)

不要在视图上使用didInsertElement挂钩,而是尝试将以下内容放在CalendarRoute上:

model: function(){
    Ember.run.scheduleOnce('afterRender', this, function(){
        $('#calendar').fullCalendar();
    });
}

答案 1 :(得分:1)

这是一个更惯用的解决方案。当您使用插件时,您希望手动从DOM中分离它们的事件侦听器,否则您将创建内存泄漏。

var CalendarView = Ember.View.extend({

  renderCalendar: function() {
    var self = this;
    Ember.run.schedule('afterRender', function() {
      self.calendar = $('#calendar').fullCalendar();
    });
  }.on('didInsertElement'),

  removeCalendar: function() {
    // Detach any calendar events
    this.calendar = null;
    delete this.calendar;
  }.on('willDestroyElement')

});

export default CalendarView;

答案 2 :(得分:0)

这个问题实际上最终成了一个重复,并且是一个完整的日历事物,与一个灰烬事物相关。对我有用的是添加jquery-once插件并像这样调用它

$('#calendar').once('calendar').fullCalendar();

SO参考:Fullcalendar: why is the calendar appearing twice on the page?

相关问题