0

Im wanting to separate the DOM manipulation and interaction (jQuery) lets call this the VIEW from my client side application logic (socket.io) lets call this the controller. The communication needs to be two way:

  • VIEW to CONTROLLER when user interacts with the DOM e.g. clicks a button
  • CONTROLLER to VIEW when server sends an update to be displayed in DOM e.g. new chat message

I'm having trouble communicating between these two JavaScript objects. How do i communicate between the two in a efficient way?? simple example below.

// CONTROLLER LOGIC (socket.io stuff)
function socketController() {}

socketController.prototype = {

    constructor: function() {

        // define socket listners
    this.socket.on('chatMessage', this.chatUpdate.bind(this));
    }

    chatUpdate: function(data) {

        // somehow call a "render" function on the "view" object
        // this.view.renderChatDOM(data.username, data.message);
    }
}

// VIEW LOGIC (jQuery stuff)
function jqueryView() {}

jqueryView.prototype = {

    onDOMReady: function() {

        // bind event handlers
        $('#send-message').bind('click', this.sendMessage.bind(this));          
    }

    sendMessage: function(event) {

            // manipulate DOM
        var $messageNode = $('#message-input'),
                $buttonNode = $('#send-message'),
                message = $messageNode.val();

            $messageNode.val(''); // clear input

            // somehow call the socketController and send data
            // this.socketController.chatSend(message);
    }
}
4
  • This is a really vague question and it's unclear which javascript in your example is client-side vs. server-side... could you clarify? Commented Sep 26, 2012 at 5:46
  • There is no server side code. I'm only concerned with the client side seperation of the: socket.io code from my Dom manipulation jQuery. Commented Sep 26, 2012 at 7:12
  • So essentially the socket.io and application logic in "socketController" needs to communicate updates sent from the server to the "jqueryView" object that updates the DOM. Then when the other use case happens: a user interacts with the DOM the "jqueryView" object needs to communicate changes to the "socketContoller" object so it can send the data to the server. Thus seperating Dom manipulation from application logic... I hope that's clearer Commented Sep 26, 2012 at 7:17
  • I just struggle with how to represent this two way communication. Commented Sep 26, 2012 at 7:22

2 Answers 2

1

Why dont you have a look at a DOM manipulation library that provides two way data binding from the view to controller (or viewmodel). Then you wont have to manage the manual updates, but just change your client side model (or client view) where the framework will take care of keeping layers in sync. http://knockoutjs.com might be a good place to have a look at.

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

3 Comments

This looks good, two way data binding is a pattern I was unaware of. As this app I'm writing (node.js / socket.io) is purely for education purposes I want to steer clear of libraries (I also detest the amount of js libraries and community fragmentation it creates) . If anyone could steer me in the direction of creating two way object data binding (without libs) that would be great!?
BTW.. this is just minimal databinding and cannot be compared to the power of KnockoutJS, AngularJS type of libraries. Since you are doing this for educational purposes, also have a look at boilerplatejs.org that describes a reference architecture for JS only frontends.
0

So in the end Hasith was right. I was indeed looking for a MVC framework in javascript. I have decided to go with backbone.js as it is the least intrusive as far as size, learning curve and resources are concerned.

this solves my problem of seperating application logic (MODEL) from DOM manipulation and presentation (VIEW).

Go backbone.js!!

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.