8. Notifiers - MagnusThor/BobTheBinder GitHub Wiki

##Bob notifiers

The main purpose of the notifiers is to primary simply data-synchronization in MVVM scenarios. The notifiers keep track of model mutations, gathers and assembles messages that can be transported using any kind of transportation-protocol.

When it comes to transportation protocols we strongly recommend you to use the WebSockets API. BobTheBinder includes a predefined, but customization message-format designed to primary be used with real-time communications platforms such as XSockets.NET, Socket.IO or SignalR. Or recommendation is for .NET & Mono developers to aim at XSockets.NET as you will benefit strong from that platform.

##How does it work?

Notifiers is attached to any object on you view model by calling .on(object,string) , each notifier will fire an event depending on what type of change (mutation) that occurs on object specified.

The example below attaches notifier for the item which is a object when one the properties of item mutates the notifier will fire the updated event ( see events below ).

ViewModel (model )

   var ViewModel = (function() {
        var ctor = function (name,age) {

            this.item = {
                name: name || "Robin Hood",
                age: age || 40
            };

        };
        return ctor;
    })();

**Notifier ***

  bob.notifier.on("item").update(
            function(object,propertyName,typeOfMutation,
                oldValue) {
                console.log(object)
  });

##Attach an notifier to a object

###Types of mutation captured (typeOfMutation)

When an object mutates the notifier will flag each mutation as follow.

  • update
  • add ( Applies to collection/arrays)
  • delete ( Applies to collection/arrays)

###.on(object:objectToMonitor,string:name,function:fn) : Bob.notifier

To subscribe to any mutation (add, delete, update )

 bob.on(item,"item", function () {
            console.log("object has mutated");
 });

Where _bob _is an instance of Bob

the fn has the following signature/arguments for updates

function (mutatedObject,propertyModified,typeOfMutation,oldValue)

the fn has the following signature/arguments

function (object, index, typeOfMutation)

.update(function:fn) : Bob.notifier

To subscribe to update mutations.

 bob.on(item,"item").updated(
 function (mutatedObject,propertyModified,
   typeOfMutation,oldValue) {
    console.log("Just interested on the updated mutationType");          
 });

.delete(function:fn): Bob.notifier

To subscribe to delete mutations.

 bob.on("item.myArray").delete(
 function (mutatedObject,index
   typeOfMutation) {
    console.log("deleted an element from item.myArray");          
 });

.add(function:fn) : Bob.notifier

To subscribe to add mutations.

 bob.on("item.myArray").add(
 function (mutatedObject,index,
   typeOfMutation) {
    console.log("added an element to item.myArray");          
 });