JavaScript

AngularJS… a selection-change directive… an early implementation

having run into some issue implementing a change handler on the select control without setting the model property… I decided to see how it could be to implement a selectionHandler directive…

I started out with this implementation which obviously is far from best practice and far from ideal yet… next step will be to find the best practice to implement a tag for an entire “combobox”…

HTML

<select ng-datasource="candidateContacts" ng-selection-change="selectContact">
	<option ng-repeat="candidateContact in candidateContacts" ng-selected="candidateContact.id == contact.item.id">
		{{candidateContact.name}}
	</option>
</select>

JavaScript

.directive( "ngSelectionChange", function( $compile ) {
		return function( $scope, $element, $attrs ) {

			jQuery( $element ).change( function( e ) {

				var target = jQuery( e.target );

				var datasourceKey = target.attr( "ng-datasource" );
				var datasource = $scope.$eval( datasourceKey);

				var actionKey = target.attr( "ng-selection-change" );
				var action = $scope.$eval( actionKey );

				var selectedIndex = target.prop( "selectedIndex" );
				var selectedItem = datasource[ selectedIndex ];

				action.call( $scope, selectedItem );
			});
		}
	})

UPDATE
I just stumbled upon the concept of using accessor’s in the directive scopes, that will help me get rid of the redundancy in the declaration of the datasource/iterator…
http://jsfiddle.net/ADukg/10/

UPDATE
I came across this directive when browsing the recently updated documentation system, this will do fine so there is no need to implement a ComboBox since the functionality is covered by the native select directive… http://docs.angularjs.org/api/angular.module.ng.$compileProvider.directive.select

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s