boneskull / angular-tags

Pure AngularJS tagging widget with typeahead support courtesy of ui-bootstrap
MIT License
133 stars 31 forks source link

Problem when the data-model is an empty array #25

Closed jvrdelafuente closed 10 years ago

jvrdelafuente commented 10 years ago

EXAMPLE

view->

<tags model="tags" src="b as b.name for b in baz "></tags>

controller->

$scope.tags=[];
$scope.baz=[{name="name1", extraData="extra1"}.{name="name2", extraData="extra2"}];

When I try to add some tags, with this configuration the result is:

tags-> ["name1","name2"]

The expected result would be:

tags-> [{name="name1", extraData="extra1"}.{name="name2", extraData="extra2"}]

PROBLEM

The problem is related with this section of code:

           // determine what format we're in
           model = scope.model;
           if (angular.isString(model)) {
             pureStrings = true;
           }
           else if (angular.isArray(model)) {
             stringArray = true;
             i = model.length;
             while (i--) {
               if (!angular.isString(model[i])) {
                 stringArray = false;
                 break;
               }
             }
           }

If the model is an empty array, it set stringArray to true.

MY SOLUTION (It could be better, this is a fast solution)

else if (angular.isArray(model)) {
             stringArray = false;
             i = model.length;
             while (i--) {
               if (angular.isString(model[i])) {
                 stringArray = true;
                 break;
               }
             }
           }
                 //Custom solution
boneskull commented 10 years ago

Yeah this is a known issue. If the array is string array by default but it was intended to be an object array, and we start adding objects to it, we'll have a problem.

I'm thinking of just making this a switch and defaulting to objects.

boneskull commented 10 years ago

dupe of #18