Skip to content

Commit 4d4bd98

Browse files
committed
Merge pull request #1 from ishi/add-observe-to-src-attribute
Added $observe expression on 'ngSrc'
2 parents a160aee + 8b426d3 commit 4d4bd98

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/angular-wurfl-image-tailor.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ angular.module('angular-wurfl-image-tailor', [])
2020
}
2121
};
2222
})
23-
.directive('imgWit', function (witUrls) {
23+
.directive('imgWit', ['witUrls', function (witUrls) {
2424
return {
2525
restrict: 'E',
2626
replace: false,
@@ -33,21 +33,20 @@ angular.module('angular-wurfl-image-tailor', [])
3333
}
3434
},
3535
link: function (scope, element, attributes) {
36-
var wit_link_pieces = [witUrls.get()];
37-
var src;
38-
if (attributes['ngSrc']) {
39-
src = attributes['ngSrc'];
40-
} else {
41-
src = attributes['src'];
42-
}
43-
if (!src) return;
44-
angular.forEach(attributes['$attr'], function(attr) {
45-
if (attr != 'src' && attr != 'ng-src') {
46-
wit_link_pieces.push(attr + '_' + attributes[attr]);
47-
}
36+
var srcAName = attributes['ngSrc'] ? 'ngSrc' : 'src';
37+
38+
if(!attributes[srcAName]) return;
39+
40+
attributes.$observe(srcAName, function (src) {
41+
var wit_link_pieces = [witUrls.get()];
42+
angular.forEach(attributes['$attr'], function (attr) {
43+
if (attr != 'src' && attr != 'ng-src') {
44+
wit_link_pieces.push(attr + '_' + attributes[attr]);
45+
}
46+
});
47+
wit_link_pieces.push(src);
48+
scope.wit_link = wit_link_pieces.join('/');
4849
});
49-
wit_link_pieces.push(src);
50-
scope.wit_link = wit_link_pieces.join('/');
5150
}
5251
}
53-
});
52+
}]);

test/unit/directiveSpec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,15 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
5858
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit.wurfl.io/w_200/http://test.com/image.jpg" src="//wit.wurfl.io/w_200/http://test.com/image.jpg"></div>');
5959
});
6060

61+
it('should change when ngSrc directive variable change', function () {
62+
scope.myUrl = sce.trustAsResourceUrl('http://test.com/image.jpg');
63+
var elt = angular.element('<img-wit ng-src="{{myUrl}}"></img-wit>');
64+
compile(elt)(scope);
65+
scope.$digest();
66+
scope.myUrl = sce.trustAsResourceUrl('http://test.com/image2.jpg');
67+
scope.$digest();
68+
69+
expect(elt.html()).to.be.a('string');
70+
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit1.wurfl.io/http://test.com/image2.jpg" src="//wit1.wurfl.io/http://test.com/image2.jpg"></div>');
71+
});
6172
});

0 commit comments

Comments
 (0)