0

I have connection to socket.io server in my router.

afterModel: function () {
        var self = this;

        socket.on('message', function (message) {
            // adding message to Ember.DS 
        }
}

Ember appends messages to div, but when scroll is become to show I need to makes Ember scrolls it down. I can do it with jQuery like that

Ember.$('.messages-area').scrollTop(1000000);

But where do I need to bind event listener for this action?

3 Answers 3

1

Depending on how you do this, you can use an action handler - see here: Ember - handling actions in views

A full list of view event handlers is here:

http://emberjs.com/api/classes/Ember.View.html#toc_event-names

If you can utilize one of the pre-defined handlers, it would undoubtedly be cleaner logic, However, it sounds like you probably can't do that as you're not directly inputting the text into the div via user interaction. What you would then need to do, is in your didInsertElement method, have a jQuery loop that checks the contents of the div being updated, and then runs the scroll when the content is updated.

Sign up to request clarification or add additional context in comments.

4 Comments

Problem that I can't bind to on-change event because it works for forms. So one way to make call Ember.$('.messages-area').trigger('msg-received') when new message was received and in didInsertElement add event listener Ember.$('.messages-area').bind('msg-received', function(){}). But is it right way which corresponds Ember philosophy?
I think that would programatically be fine, and does correspond to the Ember philosophy, because you are effectively extending the default event binds. I understand the frustration, in that you want to do your view logic in the view, and your program logic in the controller.
I tried to use observers, it fires on changing model property but before inserting elements to DOM(
Also I tried jquery trigger but it also fires before new message element added to DOM.
0

Ember not encourages event binding, instead it captures user events by event delegation.

In your case, you can place your code at View.

For example, assume i have a application template:

<div id="div1">Click this area</div>
<div id="div2">Click this area</div>

Then you can capture click event in ApplicationView like,

click:function(event){ 
   if(event.target.attr('id')==='div1'){
         //do stuff here
   }
}

Comments

0

I needed to observ chaning of model and after changing to add scroll down into the Ember run loop.

recordsDidChanges: function(){
    Ember.run.schedule('afterRender',this, function(){
        var msgArea = $('.messages-area');
        if (msgArea[0]) {
            msgArea.scrollTop(msgArea[0].scrollHeight);
        }
    });
}.observes('controller.records.@each')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.