Skip to content

Instantly share code, notes, and snippets.

@giladmanor
Created June 4, 2013 18:04
Show Gist options
  • Save giladmanor/5708083 to your computer and use it in GitHub Desktop.
Save giladmanor/5708083 to your computer and use it in GitHub Desktop.
Angular Context object: This service serves as an application wide context for passing information and events seamlessly between controllers.
angular.module('myApp.context', []).factory('Context', function($rootScope, $location) {
return {
'contentTypeFilter': "All",
'contentScopeFilter': "pub",
'focusedItemId': -1,
'profileData':{},
'broadcast': function(eventName){
console.log("! Broadcasting: "+ eventName);
$rootScope.$broadcast( eventName );
},
'wrapUp': function(){
return {id:this.focusedItemId, scope:this.contentScopeFilter, content:this.contentTypeFilter};
},
'feed': function(scope){
scope.contentTypeFilter = this.contentTypeFilter;
scope.contentScopeFilter = this.contentScopeFilter;
scope.focusedItemId = this.focusedItemId;
scope.profileData = this.profileData;
}
};
});
function MyCtrl($scope,Context) {
//dump Context onto local $scope:
Context.feed($scope);
//Shake the tree when the context is changed
$scope.userChangedFocusItem = function(selectedItem){
Context.focusedItemId = selectedItem.id;
Context.broadcast("UserChangeFocusItem");
}
// Catch a change in the context and do something in consequence
$scope.$on('ChangeContext',function(){
Context.feed($scope);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment