EntityContext and EntitySet scoped events in JayData 1.2

19 September 2012

Notification, events, EntityContext

As of version 1.2 JayData provides EntityContext and EntitySet scoped events regarding operations happening inside the environment. This way you can be notified about any data change the user is about to submit (or has submitted) and these events also let you to interact with the save/update cycles by letting you mutate the entity to be saved or cancel the operation before it gets actually submitted.

 

Context scoped notification events

On the context scope you can subscribe to general notification events that fire after one or more entity has been created, altered or deleted. These events allow you to subscribe as many listeners as you want but do not provide “peeking” (before event notifications) and also do not let you mutate/cancel the operation. Use the context level events for synchronization scenarios or for general logging, etc notifications. Context scoped events do not have a return value.

 

The following example creates a product, then modifies it just to delete it at the end – all the way with notifications provided.

 

    //this example uses the Q deferred library!
    $data.service('/Northwind.svc', function (contextFactory, contextType) {
        context = contextFactory();
        context.addEventListener("added", function () {
            console.log("Item added", arguments);
        });
        context.addEventListener("updated", function () {
            console.log("Item updated", arguments);
        });
        context.addEventListener("deleted", function () {
            console.log("Item deleted", arguments);
        });

        var product = new context.Products.elementType({
            Product_Name: "New and Cool product"
        });
        

        context.Products.add(product);
        context
            .saveChanges()
            .then(function () {
                context.attach(product);
                product.Product_Name = "New product name";
                return context.saveChanges();
            })
            .then(function () {
                context.remove(product);
                return context.saveChanges();
            })
            .then(function () {
                console.log("product lifecycle finished");
            });
    });

 

Meanwhile on the protocol:

CropperCapture[59]

The result:

CropperCapture[58]

 

 

Set scoped interactive events

Set scoped events can participate in the entity lifecycle (rather then merely observing it) by altering data or cancelling the event. This comes at the cost of multiple event listeners on the same event. One and only one event handler of an event class can be defined per EntitySet instance.  Also, while context scoped event arrive one per entity, set scoped event arrive aggregated if multiple entities of the given type is changed. In the handler you have to iterate through the items array argument to access individual entities.

 

Set scoped event names follow the <<“before” | “after” + operation>> naming scheme, giving us 8 possible event classes: “beforeCreate”, “afterCreate”, “beforeRead”, “afterRead”, “beforeUpdate”,”afterUpdate”, “beforeDelete”, “afterDelete”.

Share this

categories: JayData examples

Leave a comment

comments powered by Disqus