GithubHelp home page GithubHelp logo

doc22940 / angular.cqrs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kaba-cceac/angular.cqrs

0.0 1.0 0.0 737 KB

AngularJS module to provide CQRS integration on clientside

Perl 0.02% JavaScript 83.95% CSS 15.58% PHP 0.45%

angular.cqrs's Introduction

Introduction

Project goal is to simplify the usage of the CQRS Pattern under AngularJS by providing needed infrastructure. This module is inspired by http://jamuhl.github.io/backbone.CQRS/.

Tested with AngularJS 1.2.18

Status

Branch Status
master Build Status
develop Build Status

Docu

API documentation

Usage

To use CQRS on the clientside we will need to push events to the browser. You can achieve this via websockets, flash, long polling or any other technique around.

Configuration

In order to use angular.CQRS, add it to the list of dependencies of your module.

var module = angular.module('basicExample', ['ngCQRS']);

angular.CQRS will send queries to the server via HTTP GET to fetch data. You can configure how the URLS for these GET requests should be composed.

module.config(function (CQRSProvider) { 

  CQRSProvider.setUrlFactory(function (viewModelName, parameters) {
    return 'https://www.my-backend.com/api/' + viewModelName + CQRSProvider.toUrlGETParameterString(parameters);
  });

});

Wire up commands and events to/from sever

In order to connect angular.CQRS to your websocket / long polling solution, wire up commands and events.

var mySocket = io();

// pass in events from your socket
mySocket.on('events', function (data) {
  CQRS.eventReceived(data);
});

// pass commands to your socket
CQRS.onCommand(function (data) {
  mySocket.emit('commands', data);
});

In order to update your view data on an incoming server event, we use denormalization functions.

// Tell angular.CQRS how to denormalize (or merge) "moved" events on the aggregate type "person" for the "profile" ModelView.
DenormalizationService.registerDenormalizerFunction({
      viewModelName: 'myProfile',
      aggregateType: 'person',
      eventName: 'move'
    }, function (oldProfile, eventPayload) {
      if (eventPayload.id === oldProfile.person.id) {
          oldProfile.person.address = eventPayload.address;
      }

    return oldProfile;
});

Usage in your controllers and services

module.controller('MainController', function ($scope, StoreService, CQRS) {

    // create a store instance for your current controller scope.
    // this is needed in order to correctly cleanup any event handlers after the angular scope is destroyed.
    var store = StoreService.createForController($scope);

    // send a query to the server, requesting the view 'profile'
    // angular.CQRS will invoke your callback on the first response and on every subsequent update from the server
    // the profileData you get will be denormalized by the denormalization function you registered
    store.for('profile').do(function (profileData) {
        $scope.profile = profileData;
    });

    $scope.onChangeProfile = function () {

        // Send a "move" command for the aggregate "person" to the server
       CQRS.sendCommand({
         command: 'move',
         aggregateType: 'person',
         payload: {
           street: 'new Streetname',
           id: profile.person.id
         }
       });
    };

});

angular.cqrs's People

Contributors

xeronimus avatar mminder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.