From 541c4e1a2c696e9169553d686d33d626ef1733fb Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Thu, 11 Sep 2014 12:46:44 -0700
Subject: [PATCH] Allow piwik-siteselector angularjs directive to be used w/
 ng-model for databinding.

---
 .../siteselector/siteselector-directive.js       | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/plugins/CoreHome/angularjs/siteselector/siteselector-directive.js b/plugins/CoreHome/angularjs/siteselector/siteselector-directive.js
index a5ebd0f061..7936cbfc96 100644
--- a/plugins/CoreHome/angularjs/siteselector/siteselector-directive.js
+++ b/plugins/CoreHome/angularjs/siteselector/siteselector-directive.js
@@ -45,18 +45,24 @@ angular.module('piwikApp').directive('piwikSiteselector', function($document, pi
             allSitesText: '@',
             allSitesLocation: '@'
         },
+        require: "?ngModel",
         templateUrl: 'plugins/CoreHome/angularjs/siteselector/siteselector.html?cb=' + piwik.cacheBuster,
         controller: 'SiteSelectorController',
         compile: function (element, attrs) {
 
             for (var index in defaults) {
-               if (!attrs[index]) { attrs[index] = defaults[index]; }
+               if (attrs[index] === undefined) {
+                   attrs[index] = defaults[index];
+               }
             }
 
-            return function (scope, element, attrs) {
+            return function (scope, element, attrs, ngModel) {
+                if (ngModel) {
+                    scope.selectedSite.id = ngModel.$viewValue;
+                }
 
                 // selectedSite.id|.name + model is hard-coded but actually the directive should not know about this
-                scope.selectedSite.id   = attrs.siteid;
+                scope.selectedSite.id   = scope.selectedSite.id || attrs.siteid;
                 scope.selectedSite.name = attrs.sitename;
 
                 if (!attrs.siteid || !attrs.sitename) {
@@ -67,6 +73,10 @@ angular.module('piwikApp').directive('piwikSiteselector', function($document, pi
                     if (newValue != oldValue) {
                         element.attr('siteid', newValue);
                         element.trigger('change', scope.selectedSite);
+
+                        if (ngModel) {
+                            ngModel.$setViewValue(newValue);
+                        }
                     }
                 });
 
-- 
GitLab