refactor(delegates): s/getByHandle/$getByHandle for clarity

This commit is contained in:
Andy Joslin
2014-03-24 20:51:14 -06:00
parent 5567b08c4b
commit c6dec3c472
9 changed files with 109 additions and 39 deletions

View File

@@ -10,7 +10,7 @@ angular.module('ionic.ui.scroll')
* {@link ionic.directive:ionScroll} directives).
*
* Methods called directly on the $ionicScrollDelegate service will control all scroll
* views. Use the {@link ionic.service:$ionicScrollDelegate#getByHandle getByHandle}
* views. Use the {@link ionic.service:$ionicScrollDelegate#$getByHandle $getByHandle}
* method to control specific scrollViews.
*
* @usage
@@ -50,10 +50,10 @@ angular.module('ionic.ui.scroll')
* ```js
* function MainCtrl($scope, $ionicScrollDelegate) {
* $scope.scrollMainToTop = function() {
* $ionicScrollDelegate.getByHandle('mainScroll').scrollTop();
* $ionicScrollDelegate.$getByHandle('mainScroll').scrollTop();
* };
* $scope.scrollSmallToTop = function() {
* $ionicScrollDelegate.getByHandle('small').scrollTop();
* $ionicScrollDelegate.$getByHandle('small').scrollTop();
* };
* }
* ```
@@ -125,7 +125,7 @@ angular.module('ionic.ui.scroll')
* ```
* ```js
* function ScrollCtrl($scope, $ionicScrollDelegate) {
* var delegate = $ionicScrollDelegate.getByHandle('myScroll');
* var delegate = $ionicScrollDelegate.$getByHandle('myScroll');
*
* // Put any unique ID here. The point of this is: every time the controller is recreated
* // we want to load the correct remembered scroll values.
@@ -161,12 +161,12 @@ angular.module('ionic.ui.scroll')
'scrollToRememberedPosition'
/**
* @ngdoc method
* @name $ionicScrollDelegate#getByHandle
* @name $ionicScrollDelegate#$getByHandle
* @param {string} handle
* @returns `delegateInstance` A delegate instance that controls only the
* scrollViews with `delegate-handle` matching the given handle.
*
* Example: `$ionicScrollDelegate.getByHandle('my-handle').scrollTop();`
* Example: `$ionicScrollDelegate.$getByHandle('my-handle').scrollTop();`
*/
]))

View File

@@ -7,6 +7,25 @@ angular.module('ionic.ui.navBar', ['ionic.service.view', 'ngSanitize'])
* @module ionic
* @description
* Delegate for controlling the {@link ionic.directive:ionNavBar} directive.
*
* @usage
*
* ```html
* <body ng-controller="MyCtrl">
* <ion-nav-bar>
* <button ng-click="setNavTitle('banana')">
* Set title to banana!
* </button>
* </ion-nav-bar>
* </body>
* ```
* ```js
* function MyCtrl($scope, $ionicNavBarDelegate) {
* $scope.setNavTitle = function(title) {
* $ionicNavBarDelegate.setTitle(title);
* }
* }
* ```
*/
.service('$ionicNavBarDelegate', delegateService([
/**
@@ -20,7 +39,7 @@ angular.module('ionic.ui.navBar', ['ionic.service.view', 'ngSanitize'])
* @ngdoc method
* @name $ionicNavBarDelegate#align
* @description Aligns the title with the buttons in a given direction.
* @param {string=} direction The direction to the align the title text towards.
* @param {string=} direction The direction to the align the title text towards.
* Available: 'left', 'right', 'center'. Default: 'center'.
*/
'align',
@@ -28,7 +47,7 @@ angular.module('ionic.ui.navBar', ['ionic.service.view', 'ngSanitize'])
* @ngdoc method
* @name $ionicNavBarDelegate#showBackButton
* @description
* Set whether the {@link ionic.directive:ionNavBackButton} should be shown
* Set whether the {@link ionic.directive:ionNavBackButton} should be shown
* (if it exists).
* @param {boolean} show Whether to show the back button.
*/
@@ -73,10 +92,12 @@ angular.module('ionic.ui.navBar', ['ionic.service.view', 'ngSanitize'])
'getPreviousTitle'
/**
* @ngdoc method
* @name $ionicNavBarDelegate#getByHandle
* @name $ionicNavBarDelegate#$getByHandle
* @param {string} handle
* @returns `delegateInstance` A delegate instance that controls only the
* navBars with delegate-handle matching the given handle.
*
* Example: `$ionicNavBarDelegate.$getByHandle('myHandle').setTitle('newTitle')`
*/
]))

View File

@@ -29,7 +29,7 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture', 'ionic.service.vie
* Delegate for controlling the {@link ionic.directive:ionSideMenus} directive.
*
* Methods called directly on the $ionicSideMenuDelegate service will control all side
* menus. Use the {@link ionic.service:$ionicSideMenuDelegate#getByHandle getByHandle}
* menus. Use the {@link ionic.service:$ionicSideMenuDelegate#$getByHandle $getByHandle}
* method to control specific ionSideMenus instances.
*
* @usage
@@ -96,13 +96,13 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture', 'ionic.service.vie
'canDragContent',
/**
* @ngdoc method
* @name $ionicSideMenuDelegate#getByHandle
* @name $ionicSideMenuDelegate#$getByHandle
* @param {string} handle
* @returns `delegateInstance` A delegate instance that controls only the
* {@link ionic.directive:ionSideMenus} directives with `delegate-handle` matching
* the given handle.
*
* Example: `$ionicSideMenuDelegate.getByHandle('my-handle').toggleLeft();`
* Example: `$ionicSideMenuDelegate.$getByHandle('my-handle').toggleLeft();`
*/
]))

View File

@@ -11,8 +11,34 @@ angular.module('ionic.ui.slideBox', [])
* Delegate that controls the {@link ionic.directive:ionSlideBox} directive.
*
* Methods called directly on the $ionicSlideBoxDelegate service will control all side
* menus. Use the {@link ionic.service:$ionicSlideBoxDelegate#getByHandle getByHandle}
* menus. Use the {@link ionic.service:$ionicSlideBoxDelegate#$getByHandle $getByHandle}
* method to control specific slide box instances.
*
* @usage
*
* ```html
* <body ng-controller="MyCtrl">
* <ion-slide-box>
* <ion-slide>
* <div class="box blue">
* <button ng-click="nextSlide()">Next slide!</button>
* </div>
* </ion-slide>
* <ion-slide>
* <div class="box red">
* Slide 2!
* </div>
* </ion-slide>
* </ion-slide-box>
* </body>
* ```
* ```js
* function MyCtrl($scope, $ionicSlideBoxDelegate) {
* $scope.nextSlide = function() {
* $ionicSlideBoxDelegate.next();
* }
* }
* ```
*/
.service('$ionicSlideBoxDelegate', delegateService([
/**
@@ -63,13 +89,13 @@ angular.module('ionic.ui.slideBox', [])
'slidesCount'
/**
* @ngdoc method
* @name $ionicSlideBoxDelegate#getByHandle
* @name $ionicSlideBoxDelegate#$getByHandle
* @param {string} handle
* @returns `delegateInstance` A delegate instance that controls only the
* {@link ionic.directive:ionSlideBox} directives with `delegate-handle` matching
* the given handle.
*
* Example: `$ionicSlideBoxDelegate.getByHandle('my-handle').stop();`
* Example: `$ionicSlideBoxDelegate.$getByHandle('my-handle').stop();`
*/
]))

View File

@@ -15,8 +15,31 @@ angular.module('ionic.ui.tabs', ['ionic.service.view'])
* Delegate for controlling the {@link ionic.directive:ionTabs} directive.
*
* Methods called directly on the $ionicTabsDelegate service will control all ionTabs
* directives. Use the {@link ionic.service:$ionicTabsDelegate#getByHandle getByHandle}
* directives. Use the {@link ionic.service:$ionicTabsDelegate#$getByHandle $getByHandle}
* method to control specific ionTabs instances.
*
* @usage
*
* ```html
* <body ng-controller="MyCtrl">
* <ion-tabs>
*
* <ion-tab title="Tab 1">
* Hello tab 1!
* <button ng-click="selectTabWithIndex(1)">Select tab 2!</button>
* </ion-tab>
* <ion-tab title="Tab 2">Hello tab 2!</ion-tab>
*
* </ion-tabs>
* </body>
* ```
* ```js
* function MyCtrl($scope, $ionicTabsDelegate) {
* $scope.selectTabWithIndex = function(index) {
* $ionicTabsDelegate.select(index);
* }
* }
* ```
*/
.service('$ionicTabsDelegate', delegateService([
/**
@@ -34,19 +57,19 @@ angular.module('ionic.ui.tabs', ['ionic.service.view'])
'select',
/**
* @ngdoc method
* @name $ionicTabsDelegate#selectedTabIndex
* @name $ionicTabsDelegate#selectedIndex
* @returns `number` The index of the selected tab, or -1.
*/
'selectedIndex'
/**
* @ngdoc method
* @name $ionicTabsDelegate#getByHandle
* @name $ionicTabsDelegate#$getByHandle
* @param {string} handle
* @returns `delegateInstance` A delegate instance that controls only the
* {@link ionic.directive:ionTabs} directives with `delegate-handle` matching
* the given handle.
*
* Example: `$ionicTabsDelegate.getByHandle('my-handle').select(0);`
* Example: `$ionicTabsDelegate.$getByHandle('my-handle').select(0);`
*/
]))
@@ -55,7 +78,7 @@ angular.module('ionic.ui.tabs', ['ionic.service.view'])
var self = this;
self.tabs = [];
self.selectedTabIndex = function() {
self.selectedIndex = function() {
return self.tabs.indexOf(_selectedTab);
};
self.selectedTab = function() {

View File

@@ -18,7 +18,7 @@ function delegateService(methodNames) {
};
};
this.getByHandle = function(handle) {
this.$getByHandle = function(handle) {
if (!handle) {
return delegate;
}
@@ -29,11 +29,11 @@ function delegateService(methodNames) {
* Creates a new object that will have all the methodNames given,
* and call them on the given the controller instance matching given
* handle.
* The reason we don't just let getByHandle return the controller instance
* The reason we don't just let $getByHandle return the controller instance
* itself is that the controller instance might not exist yet.
*
* We want people to be able to do
* `var instance = $ionicScrollDelegate.getByHandle('foo')` on controller
* `var instance = $ionicScrollDelegate.$getByHandle('foo')` on controller
* instantiation, but on controller instantiation a child directive
* may not have been compiled yet!
*

View File

@@ -23,15 +23,15 @@ describe('tabs', function() {
}));
it('.getTabIndex should return indexOf tab', function() {
expect(ctrl.selectedTabIndex()).toBe(-1);
expect(ctrl.selectedIndex()).toBe(-1);
var tab1 = {}, tab2 = {};
ctrl.add(tab1);
ctrl.add(tab2);
expect(ctrl.selectedTabIndex()).toBe(0);
expect(ctrl.selectedIndex()).toBe(0);
ctrl.select(tab2);
expect(ctrl.selectedTabIndex()).toBe(1);
expect(ctrl.selectedIndex()).toBe(1);
ctrl.deselect(tab2);
expect(ctrl.selectedTabIndex()).toBe(-1);
expect(ctrl.selectedIndex()).toBe(-1);
});
it('.add should add tab and select if empty, & set historyId', inject(function($ionicViewService) {

View File

@@ -19,7 +19,7 @@
</ion-scroll>
<script>
function ScrollCtrl($scope, $ionicScrollDelegate) {
var delegate = $ionicScrollDelegate.getByHandle('myScroll');
var delegate = $ionicScrollDelegate.$getByHandle('myScroll');
delegate.rememberScrollPosition('my-scroll-id');
delegate.scrollToRememberedPosition();

View File

@@ -10,7 +10,7 @@ describe('DelegateFactory', function() {
it('should have properties', function() {
expect(setup()._instances).toEqual([]);
expect(setup()._registerInstance).toEqual(jasmine.any(Function));
expect(setup().getByHandle).toEqual(jasmine.any(Function));
expect(setup().$getByHandle).toEqual(jasmine.any(Function));
});
it('should allow reg & dereg of instance with handle', function() {
@@ -95,12 +95,12 @@ describe('DelegateFactory', function() {
expect(delegate.fn()).toBe('ret2');
});
it('getByHandle should return this for blank handle', function() {
it('$getByHandle should return this for blank handle', function() {
var delegate = setup();
expect(delegate.getByHandle()).toBe(delegate);
expect(delegate.$getByHandle()).toBe(delegate);
});
describe('getByHandle', function() {
describe('$getByHandle', function() {
var delegate, instance1, instance2, instance3;
beforeEach(function() {
delegate = setup(['a']);
@@ -121,13 +121,13 @@ describe('DelegateFactory', function() {
};
});
it('should return an InstanceWithHandle object with fields', function() {
expect(delegate.getByHandle('one').a).toEqual(jasmine.any(Function));
expect(delegate.getByHandle('two').a).toEqual(jasmine.any(Function));
expect(delegate.getByHandle('invalid').a).toEqual(jasmine.any(Function));
expect(delegate.$getByHandle('one').a).toEqual(jasmine.any(Function));
expect(delegate.$getByHandle('two').a).toEqual(jasmine.any(Function));
expect(delegate.$getByHandle('invalid').a).toEqual(jasmine.any(Function));
});
it('should noop & warn if calling for a non-added instance', inject(function($log) {
spyOn($log, 'warn');
expect(delegate.getByHandle('one').a()).toBeUndefined();
expect(delegate.$getByHandle('one').a()).toBeUndefined();
expect($log.warn).toHaveBeenCalled();
}));
@@ -135,13 +135,13 @@ describe('DelegateFactory', function() {
delegate._registerInstance(instance1, '1');
delegate._registerInstance(instance2, '2');
var result = delegate.getByHandle('1').a(1,2,3);
var result = delegate.$getByHandle('1').a(1,2,3);
expect(instance1.a).toHaveBeenCalledWith(1,2,3);
expect(instance2.a).not.toHaveBeenCalled();
expect(result).toBe('a1');
instance1.a.reset();
var result = delegate.getByHandle('2').a(2,3,4);
var result = delegate.$getByHandle('2').a(2,3,4);
expect(instance2.a).toHaveBeenCalledWith(2,3,4);
expect(instance1.a).not.toHaveBeenCalled();
expect(result).toBe('a2');
@@ -152,7 +152,7 @@ describe('DelegateFactory', function() {
delegate._registerInstance(instance2, '1');
delegate._registerInstance(instance3, 'other');
var delegateInstance = delegate.getByHandle('1');
var delegateInstance = delegate.$getByHandle('1');
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).not.toHaveBeenCalled();