Skip to content
Maxime LUCE edited this page Feb 25, 2014 · 1 revision

This module allows better communication between View Models.

Concepts

TODO: Insert some why using messaging concepts here

Usage

Topic Subscription
// vm1.js
define(["komvvm/messenger"], function (messenger) {
    messenger.subscribe("myMessageTopic", function(value) {
        console.log("ViewModel1", value);
    });
});

//vm2.js
define(["komvvm/messenger"], function (messenger) {
    messenger.subscribe("myMessageTopic", function(value) {
        console.log("ViewModel2", value);
    });
});

Topic Publishing

// vm3.js
define(["komvvm/messenger"], function (messenger) {
    messenger.publish("myMessageTopic", "myValue");
});

will output in console

ViewModel1, myValue
ViewModel2, myValue

Methods definition

publish(topic, ...args)
  • topic: string | required | message topic to publish to
  • args: any | optional | any number of arguments to send into message
  • returns: boolean | returns false if any subscription callback returns false;

Publish a message into specified topic.

subscribe(topic, callback, [options])
  • topic: string | required | message topic to subscribe in
  • callback: function | required | callback to execute when a message is published into topic
  • options: SubscribeOptions | optional | see SubscribeOptions

Subscribe given callback to specified topic.

subscribeNext(topic, callback, [options])
  • topic: string | required | message topic to subscribe in
  • callback: function | required | callback to execute when a message is published into topic
  • options: SubscribeOptions | optional | see SubscribeOptions

One time subscribtion with given callback to specified topic.

unsubscribe(topic, callback)
  • topic: string | required | message topic to unsubscribe callback from
  • callback: function | required | callback to unsubscribe from topic

Unsubscribe given callback from specified topic.

SubscribeOptions

priority
  • type: number
  • optional

Specify message priority in subscription queue.

context
  • type: any
  • optional

Specify context to execute callback with.

once
  • type: boolean
  • optional
  • default: false

Set to true to indicate that callback must be called once.

Clone this wiki locally