-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat(rating): plug into ngModel
controller
#1546
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,22 @@ angular.module('ui.bootstrap.rating', []) | |
}) | ||
|
||
.controller('RatingController', ['$scope', '$attrs', '$parse', 'ratingConfig', function($scope, $attrs, $parse, ratingConfig) { | ||
var ngModelCtrl = {$setViewValue: angular.noop}; | ||
|
||
this.maxRange = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max; | ||
this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn; | ||
this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff; | ||
|
||
this.init = function(ngModelCtrl_) { | ||
ngModelCtrl = ngModelCtrl_; | ||
ngModelCtrl.$render = this.render; | ||
|
||
$scope.range = this.buildTemplateObjects( | ||
angular.isDefined($attrs.ratingStates) ? $scope.$parent.$eval($attrs.ratingStates) : new Array(this.maxRange) | ||
); | ||
}; | ||
|
||
this.createRateObjects = function(states) { | ||
this.buildTemplateObjects = function(states) { | ||
var defaultOptions = { | ||
stateOn: this.stateOn, | ||
stateOff: this.stateOff | ||
|
@@ -24,12 +34,10 @@ angular.module('ui.bootstrap.rating', []) | |
return states; | ||
}; | ||
|
||
// Get objects used in template | ||
$scope.range = angular.isDefined($attrs.ratingStates) ? this.createRateObjects(angular.copy($scope.$parent.$eval($attrs.ratingStates))): this.createRateObjects(new Array(this.maxRange)); | ||
|
||
$scope.rate = function(value) { | ||
if ( $scope.value !== value && !$scope.readonly ) { | ||
$scope.value = value; | ||
if ( !$scope.readonly ) { | ||
ngModelCtrl.$setViewValue(value); | ||
ngModelCtrl.$render(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering if this call was necessary as render is called when viewValue differs from modelValue, and during mouseLeave, render is called. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argh, you're right, I'm misreading everything tonight heh. Although it's unlikely that rate() can be called before enter() (mouseenter usually occurs before click). It's safer to have this here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, 4-eyes principle :-) |
||
} | ||
}; | ||
|
||
|
@@ -41,13 +49,13 @@ angular.module('ui.bootstrap.rating', []) | |
}; | ||
|
||
$scope.reset = function() { | ||
$scope.val = angular.copy($scope.value); | ||
$scope.val = ngModelCtrl.$viewValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just call ctrl.render or ngModelCtrl.$render()? (can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can, but IMO this way we tightly couple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Okay, your argument makes sense. |
||
$scope.onLeave(); | ||
}; | ||
|
||
$scope.$watch('value', function(value) { | ||
$scope.val = value; | ||
}); | ||
this.render = function() { | ||
$scope.val = ngModelCtrl.$viewValue; | ||
}; | ||
|
||
$scope.readonly = false; | ||
if ($attrs.readonly) { | ||
|
@@ -60,13 +68,20 @@ angular.module('ui.bootstrap.rating', []) | |
.directive('rating', function() { | ||
return { | ||
restrict: 'EA', | ||
require: ['rating', 'ngModel'], | ||
scope: { | ||
value: '=', | ||
onHover: '&', | ||
onLeave: '&' | ||
}, | ||
controller: 'RatingController', | ||
templateUrl: 'template/rating/rating.html', | ||
replace: true | ||
replace: true, | ||
link: function(scope, element, attrs, ctrls) { | ||
var ratingCtrl = ctrls[0], ngModelCtrl = ctrls[1]; | ||
|
||
if ( ngModelCtrl ) { | ||
ratingCtrl.init( ngModelCtrl ); | ||
} | ||
} | ||
}; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thinking this doesn't need to be a public function, does it? Users can't override this either because it's called during initialization and never again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, you are right. Probably need to move the
$scope.range = ...
into the init function. I think it's useful to allow override of this function.