Created
June 4, 2013 18:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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