Javascript 主干子视图的回调?
我一直在谷歌上寻找这个问题,我不能想出一个明确的解决方案 我有一个chatView,它呈现消息的collectionView messagesView。我需要等到所有消息都被提取并呈现之后,才能运行一个函数滚动到消息列表的底部 这是我的代码的精简版本Javascript 主干子视图的回调?,javascript,jquery,backbone.js,marionette,Javascript,Jquery,Backbone.js,Marionette,我一直在谷歌上寻找这个问题,我不能想出一个明确的解决方案 我有一个chatView,它呈现消息的collectionView messagesView。我需要等到所有消息都被提取并呈现之后,才能运行一个函数滚动到消息列表的底部 这是我的代码的精简版本 var socket = io.connect(), Marionette = require('backbone.marionette'), MessagesCollection =
var socket = io.connect(),
Marionette = require('backbone.marionette'),
MessagesCollection = require('../collections/messages'),
MessagesView = require('./messages'),
MessageModel = require('../models/message');
module.exports = ChatView = Marionette.CompositeView.extend({
className: 'chat col-sm-9',
template: require('../../templates/chat.hbs'),
events: {
'focusin input.message': 'userTyping',
'focusout input.message': 'userNotTyping',
'submit #chat-form': 'sendMessage'
},
initialize: function() {
var self = this;
/* Make an instance of the collection for the specific chat */
window.messages = new MessagesCollection([], { chat_id: this.options.chatRoom });
messages.fetch({
success: function(models) {
App.data.messages = messages;
_.delay(function() {
self.scrollChat();
}, 1000);
}
});
// The view for a collection of messages
window.messagesView = new MessagesView({ collection: messages });
/* Create an instance of a specific room for client to admin chat */
socket.emit('createRoom', this.options.chatRoom);
/* Resize the chat when the window changes */
$(window).on("resize", this.scrollChat);
},
onRender: function() {
/* Render all the messages inside the chatbox */
var $chatbox = this.$el.find('.message-content');
$chatbox.append(messagesView.render().$el);
window.App.core.vent.trigger('app:log', 'Chat View: chat view was rendered!');
},
scrollChat: function() {
var $headerNav = $('.navbar').outerHeight();
var $typeMessage = $('.type-message').outerHeight();
var $extraHeight = $headerNav + $typeMessage;
$('.chat').height($(window).height() - $headerNav);
$('.message-content').height($(window).height() - $extraHeight);
var pane = $('.message-content');
pane.jScrollPane({
autoReinitialise: true
});
window.paneApi = pane.data('jsp');
paneApi.scrollTo(0, $('.collection').height());
if($(window).width() < 768) {
paneApi.destroy();
$('.message-content').scrollTop(500);
} else {
paneApi.reinitialise();
}
}
});
也许我应该移动一些部分,但是我需要获取由this.options.chatRoom定义的特定消息列表,因此我需要在上面的视图中获取这些消息
问题是我必须在fetch方法中设置一个0.delay来模拟消息的呈现,我们知道在成功回调中,消息已被检索,但视图尚未呈现,这意味着scrollChat函数无法正确计算
我想问的是,我应该如何等待整个列表被呈现给DOM,然后再执行
我能找到的最好的方法是使用木偶区域,然后使用onShow,但我必须重新考虑一些代码,而不是先问这个问题
希望我说的很清楚,这更麻烦,我想正确地滚动聊天。哦,是的,CollectionView没有onShow方法 准确地触发。但是CollectionView应该有一个onRender函数,在加载子对象后调用该函数 试着在这里添加这一行
this.listenTo(window.messagesView, "collection:rendered", self.scrollChat);
您可以尝试将scrollChat功能添加到MessagesView onShow功能中。然后仅在检索数据后创建messages视图。否则,您甚至可以添加backbone.wreqr并从messagesView向该视图发送一条消息,然后调用ScrollChat。感谢您的输入,我认为不会触发onShow,因为它不是一个区域,我尝试了。。。当你说发送消息,你的意思是触发或字面上用我的sendMessage功能发送消息。如果是后者,我不知道如何在消息视图中创建消息,因为它不包含我的输入元素。您使用哪个版本的木偶?老实说,我不太确定如何检查,我已经有一段时间没有更新它了。为什么有新的功能,我可以很容易地更新它。是的,很好的尝试,我尝试了一些类似的早期,不幸的是,它没有被触发。我甚至尝试使用sub-pub并在collectionView中触发事件。似乎唯一有效的方法是在itemView中的sub-pub,但每次消息lol都会调用它。我可能不得不暂时接受延迟,然后继续,随着时间的推移,我可能会想出一些办法。我仍然对以下想法持开放态度: