Messaging - Getting Started

Prerequistes

In order to use Messaging, you must start by include necessaries libraries into your page :
  • KnockoutJS
  • UnderscoreJS
  • SPATools.Core

<script type="text/javascript" src="lib/knockout.js" />
<script type="text/javascript" src="lib/underscore.js" />
<script type="text/javascript" src="lib/spatools.core.js" />

Publish to a topic

Publish to a message topic in all your viewmodels you want to be notified.
var vm = (function() {
    var prop1 = ko.observable(),
        prop2 = ko.observable(),

        callback = function(options) { 
            alert(options); 
        },
        init = function() {
            spa.messenger.subscribe("MyMessageTopic", callback);
        };
    
    init();

    return {
        prop1: prop1,
        prop2: prop2
    };
})();

Publish messages

When you publish a message under a message topic, every ViewModel which are subscribed to this topic will be notified.

spa.messenger.publish("MyMessageTopic", "Hello World");

Unsubscribe from a topic

If you want to unsubscribe a callback from a message topic, you have to call the unsubscribe method from spa.messenger with your callback in parameter.

//From vm below
spa.messenger.unsubscribe("MyMessageTopic", callback);

!!More about MVVM Messaging

Last edited Dec 20, 2012 at 3:58 PM by SomaticIT, version 3

Comments

No comments yet.