Skip to content

Declare the tree on a factory

Lucas Reis edited this page Mar 16, 2015 · 3 revisions

Declare your Baobab tree on a factory:

angular.module('myApp')
  .factory('AppState', function($rootScope) {
  
  var initialState = {
    foos: [1, 2, 3],
    bars: ['a', 'b']
  };

  var config = {
    maxHistory: 10
  };

  var tree = new Baobab(
    initialState,
    config
  );

  // Important: 
  // Trigger angular dirty checking
  // so that the views are rendered
  // every time the tree updates
  tree.on('update', function () {
    setTimeout(function () {
      $rootScope.$apply();
    }, 0);
  });

  return tree;
});