0

Before we can use AngularJS i have to solve the following problem

Maintaining application state across single page view flips and multi-page flips

I have started a view state service and am trying to integrate it into the application. If i call all the stuff manually, it works great. My problem is i want to have my viewstate object update automatically so the developer doesn't have to do anything, they just include a few things and it just works.

Im trying to follow angulars source code, but functional programs are so hard to debug its being a pain. Is there a document (other than this one http://docs.angularjs.org/api/ng.$rootScope.Scope , which is pretty much useless), that explains the event system within angular? specifically I want to be able to pull a list of events that are registered in the system.

here's my design im trying to pull off.

any time a field is typed in, a button is clicked, a column is sorted, i want an event to be fired that triggers my viewstate service. That view state service records the event in sorta like a delta V type layout.

When a directive is initialized, I want an event to fire that again triggers the View State service, that service looks at its internal delta storage and if that directive exists, it plays back the deltas to return the directive back to the state it was in before.

When a person navigates outside of Angular (in my case jumping from angular to a legacy struts app) the view state service automatically stores the delta store to a redis database.

if the user back buttons back into the angular app, the angular initializing hits the redis database and pulls down any deltas that exist.

I have all this working in a manual layout, but i need it to be automated.

At this moment in time, I could truly care less how to USE the event system, right now I need to know how to watch it work. I need to be able to click a button and see the internal events that are fired that make angular do what it does. How do i pull that off? How do Angular developers debug the event system when writing the thing?

Community
  • 1
  • 1
scphantm
  • 4,293
  • 8
  • 43
  • 77
  • You mentioned not being interested in implementation details, but it sounds like adding $watch statements to your directives would help you achieve your design. – tenthfloor Jan 31 '14 at 18:20
  • I looked at that and had that on my notebook as a possibility, but I can't get any visibility to the internal systems to know if that will work or not. Im not sure if watcher can capture all events or if i have to specify which event. Im really need a way to see what events are registered. – scphantm Jan 31 '14 at 19:01

1 Answers1

0

How about something like this? You could use angular's $watch function to, well ... watch for changes on your scope's properties and save them accordingly.

var app = angular.module('app',[])

.directive('aDirective',[ 'redisService', function(redis){

    return {
        restrict: 'A', // or otherwise
        templateUrl: "aDirectiveTemplate.html",
        controller : ['$scope', function($scope){
            // initialization
            var savedState = redis.hasData('someKey');
            if(savedState){
                $scope.model = savedState;
            }else{
                $scope.model = {
                    prop1: 1,
                    prop2: 2
                }
            }
        }],
        link: function(scope, elem, attrs){
            // set up watcher when directive is linked.
            // observes changes to properties assigned to scope & fires callback.
            scope.$watch(function(){
                redis.saveDelta(scope.model);
            });
        }
    }
}]);

// ...
tenthfloor
  • 160
  • 9