简体   繁体   English

Ember如何知道路由模板何时完成加载并触发回调?

[英]Ember how do I know when a route template is finished loading and fire a callback?

Currently working on a chat app with Ember, which is going fantastic as Ember is really nice to work with. 目前正在与Ember一起开发聊天应用程序,这非常棒,因为Ember非常适合使用。

I currently have a chat window, obviously with many lines of people chatting. 我目前有一个聊天窗口,显然有许多人聊天。

I would like to scroll the chat window down on initial page load, here's an example: 我想在初始页面加载时向下滚动聊天窗口,这是一个例子:

<div class="chat-window">
    {{ chat-message username="John Doe" message="Blah" dispic="unknownUser.jpg" }}
</div>

So how might a bind an event to the entire template being loaded (in this case its index.hbs 那么如何将事件绑定到正在加载的整个模板(在本例中为index.hbs

I know you can do this with components through something like: 我知道你可以通过以下方式对组件执行此操作:

import Ember from 'ember';

export default Ember.Component.extend({
  didInsertElement() {
    this.$('.button-collapse').sideNav();
  }
});

Which works fine, so what would the equivalent be for doing it to a template. 哪个工作正常,那么将其用于模板的等价物是什么。

As far as I know there is only the index.hbs and index.js route file. 据我所知,只有index.hbsindex.js路由文件。

Any information would be great thanks. 任何信息都会非常感谢。

Send an action from the component to the route. 将组件中的操作发送到路径。 Route will retrieve data from server and after data is retrieved route will update the data. Route将从服务器检索数据,并在检索数据后,路由将更新数据。

Route can handle loading state. Route可以处理加载状态。 ( From Guide ) 来自指南

The code will be something like that: 代码将是这样的:

import Ember from 'ember';

export default Ember.Route.extend({
  chatData:[],
  model:function(){
      return this.get('chatData'); 
  },
  actions:{
      loading:function(){
         //do about loading...
      },
      chatDataRequested:function(){
          retrieveData().then((dataFromServer)=>{this.set('chatData',dataFromServer);});
      }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM