Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout components communication

I have a web app that I want to convert to and SPA using ko components. I wonder how to do a kind of inter component communication.

For example I want a "notification" component in which every single component can send notifications.

I managed to find a solution by sharing an observable array in the main view model:

var VM = function() {
    var self = this;

    this._notifications = ko.observableArray([]);
   this.notifications = {
        addInfo: function(text){
            self._notifications.push(text);
        }
    }
}

and

<comp1 params="{app: $data}"></comp1><br/>
<comp2 params="{app: $data}"></comp2><br/>
<notif params="{app: $data}"></notif>

See here: http://jsfiddle.net/nveron/j4829y7p/

I'm not completely satisfied with this solution, I would rather have kept the notification data in the notify component.

Do you have any idea to deal with that?

like image 518
rbag Avatar asked Oct 08 '14 07:10

rbag


People also ask

What is knockout language?

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.

What is Knockout used for?

Knockout is a JavaScript toolkit that allows you to connect HTML components to any data model. It is based on the MVVM paradigm and allows you to develop rich, responsive display and editor user interfaces with a clean underlying data model. Knockout. js works with a wide range of client and server-side technologies.

What is $data in knockout?

The $data variable is a built-in variable used to refer to the current object being bound. In the example this is the one of the elements in the viewModel. folders array.

What is Ko applyBindings?

The ko. applyBindings method activates Knockout and wires up the view-model to the view. Now that we have a view-model, we can create the bindings. In Knockout.js, you do this by adding data-bind attributes to HTML elements.


1 Answers

You can use ko.subscribable to implement a simple pub/sub message bus:

  • ko.subscribable.subscribe to subscribe to new messages
  • ko.subscribable.notifySubscribers to publish new messages

    More about this in this article by Ryan Niemeyer.

In your case, you can create a global postbox object based on ko.subscribable. Componenents publish messages in it and notification vm subscribes to it.

You can even publish and subscribe to message by topic.

Demo: JSFiddle

postbox:

var postbox = (function() {
    var pb = new ko.subscribable();
    return {    
        subscribe: function(handler, topic) {
            pb.subscribe(handler, null, topic)
        },        
        publish: function(message, topic) {
            pb.notifySubscribers(message, topic);
        }
    };
}()); 

a component:

var comp1 = function(params) {           
    this.addToNotif = function(){
        postbox.publish("comp1 was here");
    }
}

notif:

...
postbox.subscribe(function(message) {
    self.notifs.push(message);
});
like image 145
manji Avatar answered Sep 30 '22 18:09

manji