From 7c5fe67b6287159ff0a5efb5a0ce29d052d7016c Mon Sep 17 00:00:00 2001 From: Alfredo Barron Date: Wed, 4 May 2016 09:31:58 -0500 Subject: [PATCH] Update 1.0.5 Solved fix - select2 clear when the model is empty --- bower.json | 2 +- dist/mdr-select2.js | 51 +++++++++++++++++------------------------ dist/mdr-select2.min.js | 2 +- package.json | 2 +- src/mdr-select2.js | 51 +++++++++++++++++------------------------ 5 files changed, 45 insertions(+), 63 deletions(-) diff --git a/bower.json b/bower.json index 2ccf75b..c3c2add 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "mdr-angular-select2", - "version": "1.0.4", + "version": "1.0.5", "description": "Select2 for angular", "main": "dist/mdr-select2.js", "authors": [ diff --git a/dist/mdr-select2.js b/dist/mdr-select2.js index fc652f0..fed6130 100644 --- a/dist/mdr-select2.js +++ b/dist/mdr-select2.js @@ -55,10 +55,7 @@ } else { $scope[options.collection] = data; initialize(); - // Si cambia selected se selecciona el nuevo modelo - if($scope.selected !== undefined){ - selected($scope.selected); - } + selected($scope.selected); } }) .catch(function (error) { @@ -73,32 +70,24 @@ var options = getOptions(); $scope[options.collection] = newValue; initialize(); - // Si cambia selected se selecciona el nuevo modelo - if($scope.selected !== undefined){ - selected($scope.selected); - } + selected($scope.selected); } }); - // Cuando cambia selected se selecciona el nuevo modelo + // Cuando cambia model $scope.$watch('model', function(newValue, oldValue) { - // if(newValue !== undefined && newValue !== null){ - // if(Object.keys(newValue).length === 0){ - // setTimeout(function() { - // $("#selectId_" + $scope.$id).val(null).trigger("change"); - // },0); - // } - // } + // Se inicializa el modelo if(newValue === undefined || newValue === null){ $scope.model = {}; + setTimeout(function() { + $("#selectId_" + $scope.$id).val('').trigger('change.select2'); + },0); } }); - // Cuando cambia selected se selecciona el nuevo modelo + // Cuando cambia selected se manda llamar la funcion selected $scope.$watch('selected', function(newValue, oldValue) { - if(newValue !== undefined){ - selected(newValue); - } + selected(newValue); }); // Se crea el metodo que inicializa el select2 function initialize() @@ -122,16 +111,18 @@ // Selecciona el model(ng-model) y el val en el select2 function selected(value) { - var options = getOptions(); - angular.forEach($scope[options.collection], function(val, key){ - if(value == val[options.track]){ - $scope.model = val; - setTimeout(function() { - $("#selectId_" + $scope.$id).val(value).trigger("change"); - },0); - return false; - } - }); + if(value !== undefined){ + var options = getOptions(); + angular.forEach($scope[options.collection], function(val, key){ + if(value == val[options.track]){ + $scope.model = val; + setTimeout(function() { + $("#selectId_" + $scope.$id).val(value).trigger("change"); + },0); + return false; + } + }); + } } // Se crea el metodo que obtiene el nombre de la coleccion de options function getOptions() diff --git a/dist/mdr-select2.min.js b/dist/mdr-select2.min.js index 325a0de..524a871 100644 --- a/dist/mdr-select2.min.js +++ b/dist/mdr-select2.min.js @@ -1 +1 @@ -!function(){"use strict";angular.module("mdr.select2",[]).directive("mdrSelect2",[function(){return{restrict:"E",controller:"mdrSelect2Ctrl",scope:{url:"@",src:"=",options:"@",model:"=",selected:"=",allowClear:"@",placeholder:"@",disabled:"=",required:"="},template:''}}]).controller("mdrSelect2Ctrl",["$scope","$element","$attrs","mdrSelect2Service",function(e,t,o,l){function c(){setTimeout(function(){$("#selectId_"+e.$id).select2({placeholder:e.placeholder,allowClear:e.allowClear})},0)}function n(t){setTimeout(function(){$("#selectId_"+e.$id).select2({placeholder:t})},0)}function i(t){var o=r();angular.forEach(e[o.collection],function(l,c){return t==l[o.track]?(e.model=l,setTimeout(function(){$("#selectId_"+e.$id).val(t).trigger("change")},0),!1):void 0})}function r(){var t=e.options.split(" "),o=t.indexOf("in"),l=t.indexOf("track"),c=t[l+2].split("."),n={collection:t[o+1],track:c[1]};return n}e.placeholder="Select",e.allowClear=!1,c(),e.$watch("url",function(t,o){if(void 0!==t){var d=r();e[d.collection]=[],n("Loading..."),l.find(t).then(function(t){t.length<=0||void 0===t.length?n("No results found"):(e[d.collection]=t,c(),void 0!==e.selected&&i(e.selected))})["catch"](function(e){n("No results found")})}}),e.$watchCollection("src",function(t,o){if(void 0!==t){var l=r();e[l.collection]=t,c(),void 0!==e.selected&&i(e.selected)}}),e.$watch("model",function(t,o){(void 0===t||null===t)&&(e.model={})}),e.$watch("selected",function(e,t){void 0!==e&&i(e)})}]).factory("mdrSelect2Service",["$http","$q",function(e,t){return{find:function(o){var l=t.defer();return e.get(o).success(function(e){l.resolve(e)}).error(function(e){l.reject(e)}),l.promise}}}])}(); \ No newline at end of file +!function(){"use strict";angular.module("mdr.select2",[]).directive("mdrSelect2",[function(){return{restrict:"E",controller:"mdrSelect2Ctrl",scope:{url:"@",src:"=",options:"@",model:"=",selected:"=",allowClear:"@",placeholder:"@",disabled:"=",required:"="},template:''}}]).controller("mdrSelect2Ctrl",["$scope","$element","$attrs","mdrSelect2Service",function(e,t,l,o){function c(){setTimeout(function(){$("#selectId_"+e.$id).select2({placeholder:e.placeholder,allowClear:e.allowClear})},0)}function n(t){setTimeout(function(){$("#selectId_"+e.$id).select2({placeholder:t})},0)}function i(t){if(void 0!==t){var l=r();angular.forEach(e[l.collection],function(o,c){return t==o[l.track]?(e.model=o,setTimeout(function(){$("#selectId_"+e.$id).val(t).trigger("change")},0),!1):void 0})}}function r(){var t=e.options.split(" "),l=t.indexOf("in"),o=t.indexOf("track"),c=t[o+2].split("."),n={collection:t[l+1],track:c[1]};return n}e.placeholder="Select",e.allowClear=!1,c(),e.$watch("url",function(t,l){if(void 0!==t){var d=r();e[d.collection]=[],n("Loading..."),o.find(t).then(function(t){t.length<=0||void 0===t.length?n("No results found"):(e[d.collection]=t,c(),i(e.selected))})["catch"](function(e){n("No results found")})}}),e.$watchCollection("src",function(t,l){if(void 0!==t){var o=r();e[o.collection]=t,c(),i(e.selected)}}),e.$watch("model",function(t,l){(void 0===t||null===t)&&(e.model={},setTimeout(function(){$("#selectId_"+e.$id).val("").trigger("change.select2")},0))}),e.$watch("selected",function(e,t){i(e)})}]).factory("mdrSelect2Service",["$http","$q",function(e,t){return{find:function(l){var o=t.defer();return e.get(l).success(function(e){o.resolve(e)}).error(function(e){o.reject(e)}),o.promise}}}])}(); \ No newline at end of file diff --git a/package.json b/package.json index af304bb..5b9e1b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mdr-angular-select2", - "version": "1.0.4", + "version": "1.0.5", "description": "Select2 for angular", "main": "index.js", "scripts": { diff --git a/src/mdr-select2.js b/src/mdr-select2.js index fc652f0..fed6130 100644 --- a/src/mdr-select2.js +++ b/src/mdr-select2.js @@ -55,10 +55,7 @@ } else { $scope[options.collection] = data; initialize(); - // Si cambia selected se selecciona el nuevo modelo - if($scope.selected !== undefined){ - selected($scope.selected); - } + selected($scope.selected); } }) .catch(function (error) { @@ -73,32 +70,24 @@ var options = getOptions(); $scope[options.collection] = newValue; initialize(); - // Si cambia selected se selecciona el nuevo modelo - if($scope.selected !== undefined){ - selected($scope.selected); - } + selected($scope.selected); } }); - // Cuando cambia selected se selecciona el nuevo modelo + // Cuando cambia model $scope.$watch('model', function(newValue, oldValue) { - // if(newValue !== undefined && newValue !== null){ - // if(Object.keys(newValue).length === 0){ - // setTimeout(function() { - // $("#selectId_" + $scope.$id).val(null).trigger("change"); - // },0); - // } - // } + // Se inicializa el modelo if(newValue === undefined || newValue === null){ $scope.model = {}; + setTimeout(function() { + $("#selectId_" + $scope.$id).val('').trigger('change.select2'); + },0); } }); - // Cuando cambia selected se selecciona el nuevo modelo + // Cuando cambia selected se manda llamar la funcion selected $scope.$watch('selected', function(newValue, oldValue) { - if(newValue !== undefined){ - selected(newValue); - } + selected(newValue); }); // Se crea el metodo que inicializa el select2 function initialize() @@ -122,16 +111,18 @@ // Selecciona el model(ng-model) y el val en el select2 function selected(value) { - var options = getOptions(); - angular.forEach($scope[options.collection], function(val, key){ - if(value == val[options.track]){ - $scope.model = val; - setTimeout(function() { - $("#selectId_" + $scope.$id).val(value).trigger("change"); - },0); - return false; - } - }); + if(value !== undefined){ + var options = getOptions(); + angular.forEach($scope[options.collection], function(val, key){ + if(value == val[options.track]){ + $scope.model = val; + setTimeout(function() { + $("#selectId_" + $scope.$id).val(value).trigger("change"); + },0); + return false; + } + }); + } } // Se crea el metodo que obtiene el nombre de la coleccion de options function getOptions()