From 97ae6b75f48e6c31ccf5ea4a18a0f773c78d1bca Mon Sep 17 00:00:00 2001 From: Arjan Kempes Date: Sat, 7 Oct 2017 01:24:33 +0200 Subject: [PATCH] Changed callback function binding --- README.md | 22 ++++++++++++++++++---- bower.json | 2 +- dist/ng-rateit.js | 17 +++++++++-------- dist/ng-rateit.min.js | 2 +- example/example.js | 10 +++++----- example/index.html | 4 ++-- package.json | 2 +- src/ng-rateit.js | 17 +++++++++-------- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index fc9aacb..162740b 100644 --- a/README.md +++ b/README.md @@ -54,10 +54,10 @@ For more advanced functionality you can add a couple attributes: resetable = "Boolean" star-width = "Integer" star-height = "Integer" - rated = "Function" - reset = "Function" - before-rated = "Function: return promise" - before-reset = "Function: return promise" + rated = "Function(rating)" + reset = "Function(rating)" + before-rated = "Function(newRating): return promise" + before-reset = "Function(rating): return promise" > ``` @@ -98,6 +98,20 @@ You can easily add your own star style via css. You can use the star-width and s ### Release Note: +V4.0.0 + +* BREAKING: The callback function binding has changed form two-way to method binding. This will allow you to pass your own variables to the callback function AND the current rating is passed in the `rating` parameter: +```html + +``` +```js +$scope.myCallback = function (rating, cusotmVar) { + console.log(rating, customVar); +} +``` +To upgrade from v3 to v4, just add `()` after your function name. + + V3.0.0 * BREAKING: The `over` callback is removed. diff --git a/bower.json b/bower.json index 65ce04b..cae95a5 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-rateit", - "version": "3.0.5", + "version": "4.0.0", "homepage": "https://github.com/akempes/angular-rateit", "authors": [ "Arjan Kempes " diff --git a/dist/ng-rateit.js b/dist/ng-rateit.js index dccc74b..c30e8d2 100644 --- a/dist/ng-rateit.js +++ b/dist/ng-rateit.js @@ -45,10 +45,10 @@ angular.module('ngRateIt', ['ng']) starHeight : '=?starHeight', canelWidth : '=?canelWidth', cancelHeight : '=?cancelHeight', - rated : '=?rated', - reset : '=?reset', - beforeRated : '=?beforeRated', - beforeReset : '=?beforeReset' + rated : '&?rated', + reset : '&?reset', + beforeRated : '&?beforeRated', + beforeReset : '&?beforeReset' }, templateUrl: 'ngRateIt/ng-rate-it.html', require: 'ngModel', @@ -102,9 +102,9 @@ angular.module('ngRateIt', ['ng']) $scope.removeRating = function () { if ($scope.resetable() && !$scope.readOnly()) { - $scope.beforeReset().then(function () { + $scope.beforeReset({rating:$scope.ngModel}).then(function () { $scope.ngModel = $scope.min; - $scope.reset(); + $scope.reset({rating:$scope.ngModel}); }); } }; @@ -112,10 +112,11 @@ angular.module('ngRateIt', ['ng']) $scope.setValue = function (index) { if (!$scope.readOnly()) { var tmpValue = angular.copy($scope.min + getValue(index)); - $scope.beforeRated(tmpValue).then(function () { + + $scope.beforeRated({rating:tmpValue}).then(function () { $scope.ngModel = tmpValue; $timeout(function () { - $scope.rated(); + $scope.rated({rating:$scope.ngModel}); }); }); } diff --git a/dist/ng-rateit.min.js b/dist/ng-rateit.min.js index df29119..fc0d4c2 100644 --- a/dist/ng-rateit.min.js +++ b/dist/ng-rateit.min.js @@ -1 +1 @@ -angular.module("ngRateIt",["ng"]).directive("ngRateIt",["$q",function(a){"use strict";var b=function(b,c,d){d.readOnly||(b.readOnly=function(){return!1}),d.resetable||(b.resetable=function(){return!0}),d.beforeRated||(b.beforeRated=function(){var b=a.defer();return b.resolve(),b.promise}),d.rated||(b.rated=function(){}),d.beforeReset||(b.beforeReset=function(){var b=a.defer();return b.resolve(),b.promise}),d.reset||(b.reset=function(){})};return{scope:{ngModel:"=",min:"=?min",max:"=?max",step:"=?step",readOnly:"&?readOnly",pristine:"=?pristine",resetable:"&?resetable",starWidth:"=?starWidth",starHeight:"=?starHeight",canelWidth:"=?canelWidth",cancelHeight:"=?cancelHeight",rated:"=?rated",reset:"=?reset",beforeRated:"=?beforeRated",beforeReset:"=?beforeReset"},templateUrl:"ngRateIt/ng-rate-it.html",require:"ngModel",replace:!0,link:b,controller:"ngRateItController"}}]).controller("ngRateItController",["$scope","$timeout",function(a,b){"use strict";a.isTouch=!!window.hasOwnProperty("ontouchstart")||window.navigator.msMaxTouchPoints>0,a.orgValue=angular.copy(a.ngModel),a.min=a.min||0,a.max=a.max||5,a.step=a.step||.5,a.pristine=a.orgValue===a.ngModel,a.starWidth=a.starWidth||16,a.starPartWidth=a.starWidth*a.step,a.starHeight=a.starHeight||16,a.canelWidth=a.canelWidth||a.starWidth,a.cancelHeight=a.cancelHeight||a.starHeight;var c=a.max-a.min,d=c/a.step,e=a.$watch("ngModel",function(){a.pristine=a.orgValue===a.ngModel}),f=function(a){return(a+1)/d*c};a.getStartParts=function(){return new Array(d)},a.getStarOffset=function(b){var c=1/a.step,d=-(a.starWidth/c)*(b%c);return d},a.isSelected=function(b){return f(b)<=a.ngModel-a.min},a.removeRating=function(){a.resetable()&&!a.readOnly()&&a.beforeReset().then(function(){a.ngModel=a.min,a.reset()})},a.setValue=function(c){if(!a.readOnly()){var d=angular.copy(a.min+f(c));a.beforeRated(d).then(function(){a.ngModel=d,b(function(){a.rated()})})}},a.$on("$destroy",function(){e()})}]).run(["$templateCache",function(a){"use strict";a.put("ngRateIt/ng-rate-it.html",'
")}]); \ No newline at end of file +angular.module("ngRateIt",["ng"]).directive("ngRateIt",["$q",function(a){"use strict";var b=function(b,c,d){d.readOnly||(b.readOnly=function(){return!1}),d.resetable||(b.resetable=function(){return!0}),d.beforeRated||(b.beforeRated=function(){var b=a.defer();return b.resolve(),b.promise}),d.rated||(b.rated=function(){}),d.beforeReset||(b.beforeReset=function(){var b=a.defer();return b.resolve(),b.promise}),d.reset||(b.reset=function(){})};return{scope:{ngModel:"=",min:"=?min",max:"=?max",step:"=?step",readOnly:"&?readOnly",pristine:"=?pristine",resetable:"&?resetable",starWidth:"=?starWidth",starHeight:"=?starHeight",canelWidth:"=?canelWidth",cancelHeight:"=?cancelHeight",rated:"&?rated",reset:"&?reset",beforeRated:"&?beforeRated",beforeReset:"&?beforeReset"},templateUrl:"ngRateIt/ng-rate-it.html",require:"ngModel",replace:!0,link:b,controller:"ngRateItController"}}]).controller("ngRateItController",["$scope","$timeout",function(a,b){"use strict";a.isTouch=!!window.hasOwnProperty("ontouchstart")||window.navigator.msMaxTouchPoints>0,a.orgValue=angular.copy(a.ngModel),a.min=a.min||0,a.max=a.max||5,a.step=a.step||.5,a.pristine=a.orgValue===a.ngModel,a.starWidth=a.starWidth||16,a.starPartWidth=a.starWidth*a.step,a.starHeight=a.starHeight||16,a.canelWidth=a.canelWidth||a.starWidth,a.cancelHeight=a.cancelHeight||a.starHeight;var c=a.max-a.min,d=c/a.step,e=a.$watch("ngModel",function(){a.pristine=a.orgValue===a.ngModel}),f=function(a){return(a+1)/d*c};a.getStartParts=function(){return new Array(d)},a.getStarOffset=function(b){var c=1/a.step,d=-(a.starWidth/c)*(b%c);return d},a.isSelected=function(b){return f(b)<=a.ngModel-a.min},a.removeRating=function(){a.resetable()&&!a.readOnly()&&a.beforeReset({rating:a.ngModel}).then(function(){a.ngModel=a.min,a.reset({rating:a.ngModel})})},a.setValue=function(c){if(!a.readOnly()){var d=angular.copy(a.min+f(c));a.beforeRated({rating:d}).then(function(){a.ngModel=d,b(function(){a.rated({rating:a.ngModel})})})}},a.$on("$destroy",function(){e()})}]).run(["$templateCache",function(a){"use strict";a.put("ngRateIt/ng-rate-it.html",'
")}]); \ No newline at end of file diff --git a/example/example.js b/example/example.js index 291da68..c219321 100644 --- a/example/example.js +++ b/example/example.js @@ -18,9 +18,9 @@ myApp.controller('ExampleController', ['$scope', '$q', '$timeout', function($sco custom: 4, }; - $scope.ratedCallback = function () { - alert('The rated value is: '+$scope.model.callbacks); - console.log('The rated value is: '+$scope.model.callbacks); + $scope.ratedCallback = function (rating) { + alert('The rated value is: '+ rating); + console.log('The rated value is: '+ rating); }; $scope.resetCallback = function () { @@ -38,11 +38,11 @@ myApp.controller('ExampleController', ['$scope', '$q', '$timeout', function($sco return d.promise; }; - $scope.confirmRating = function (newRating) { + $scope.confirmRating = function (newRating, cusotmVar) { var d = $q.defer(); $timeout(function () { - if(confirm('Are you sure about rating us with '+newRating+' stars?')){ + if(confirm('Are you sure about rating us with '+newRating+' '+cusotmVar+'?')){ d.resolve(); }else{ d.reject(); diff --git a/example/index.html b/example/index.html index b6d21af..33a1bea 100644 --- a/example/index.html +++ b/example/index.html @@ -85,9 +85,9 @@

Start height/width

Callbacks

- +

Value: {{model.resetable}}

-
+

Custom image

diff --git a/package.json b/package.json index ed8e1f5..4cd844a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-rateit", - "version": "3.0.5", + "version": "4.0.0", "scripts": { "build": "grunt build", "deploy": "grunt deploy-patch" diff --git a/src/ng-rateit.js b/src/ng-rateit.js index dccc74b..c30e8d2 100644 --- a/src/ng-rateit.js +++ b/src/ng-rateit.js @@ -45,10 +45,10 @@ angular.module('ngRateIt', ['ng']) starHeight : '=?starHeight', canelWidth : '=?canelWidth', cancelHeight : '=?cancelHeight', - rated : '=?rated', - reset : '=?reset', - beforeRated : '=?beforeRated', - beforeReset : '=?beforeReset' + rated : '&?rated', + reset : '&?reset', + beforeRated : '&?beforeRated', + beforeReset : '&?beforeReset' }, templateUrl: 'ngRateIt/ng-rate-it.html', require: 'ngModel', @@ -102,9 +102,9 @@ angular.module('ngRateIt', ['ng']) $scope.removeRating = function () { if ($scope.resetable() && !$scope.readOnly()) { - $scope.beforeReset().then(function () { + $scope.beforeReset({rating:$scope.ngModel}).then(function () { $scope.ngModel = $scope.min; - $scope.reset(); + $scope.reset({rating:$scope.ngModel}); }); } }; @@ -112,10 +112,11 @@ angular.module('ngRateIt', ['ng']) $scope.setValue = function (index) { if (!$scope.readOnly()) { var tmpValue = angular.copy($scope.min + getValue(index)); - $scope.beforeRated(tmpValue).then(function () { + + $scope.beforeRated({rating:tmpValue}).then(function () { $scope.ngModel = tmpValue; $timeout(function () { - $scope.rated(); + $scope.rated({rating:$scope.ngModel}); }); }); }