Skip to content

Commit 6e0c8cb

Browse files
committed
Added: $observe expression on allowed attributes
1 parent 0958dc0 commit 6e0c8cb

4 files changed

Lines changed: 32 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `angular-wurfl-image-tailor` will be documented in this file
44

5+
## NEXT RELEASE
6+
7+
### Added
8+
- $observe expression on all allowed attributes
9+
510
## 0.9.3 - 2015-02-27
611

712
### Fixed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
* Add the img-wit directive
1010
* Enable out of the box support for the [WIT URLs](http://wurfl.io/documentation/wit-getting-started.php) to download more images in parallel.
11-
* Allow use of the [ngSrc directive](https://docs.angularjs.org/api/ng/directive/ngSrc)
1211

1312
## Usage
1413

@@ -30,9 +29,9 @@
3029

3130
`<img-wit src="http://yourserver.com/image.png"></img-wit>`
3231

33-
or if you want to use the ngSrc directive:
32+
or if you want to use interpolation:
3433

35-
`<img-wit ng-src="{{myUrl}}"></img-wit>`
34+
`<img-wit src="{{myUrl}}"></img-wit>`
3635

3736
where {{myUrl}} is the url of the [trusted image](https://docs.angularjs.org/api/ng/service/$sce) to load.
3837

src/angular-wurfl-image-tailor.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* angular-wurfl-image-tailor v0.9.3
2+
* angular-wurfl-image-tailor
33
* Authors: Luca Corbo (https://github.com/lucor)
44
* (c) 2014 - 2015 ScientiaMobile, Inc.
55
* License: MIT
@@ -24,30 +24,37 @@ angular.module('angular-wurfl-image-tailor', [])
2424
return {
2525
restrict: 'E',
2626
replace: false,
27-
scope:{},
28-
template: function(element, attributes){
29-
if ('ngSrc' in attributes) {
30-
return '<div class="wit"><img ng-src="{{wit_link}}"/></div>';
31-
} else {
32-
return '<div class="wit"><img src="{{wit_link}}"/></div>';
33-
}
27+
scope: {
28+
src: '@',
29+
w: '@',
30+
h: '@',
31+
pc: '@',
32+
m: '@',
33+
f: '@',
34+
r: '@'
35+
},
36+
template: function (element, attributes) {
37+
return '<div class="wit"><img ng-src="{{wit_link}}"/></div>';
3438
},
3539
link: function (scope, element, attributes) {
36-
var srcAName = 'ngSrc' in attributes ? 'ngSrc' : 'src';
3740
scope.wit_link = '';
3841

39-
attributes.$observe(srcAName, function (src) {
40-
if(!src) {
41-
scope.wit_link = '';
42+
var allowedAttributes = ['w', 'h', 'pc', 'm', 'f', 'r'];
43+
44+
scope.$watchCollection('[src, w, h, pc, m, f, r]', function (values, oldValues) {
45+
46+
var src = values.shift();
47+
if (!src) {
4248
return;
4349
}
4450

4551
var wit_link_pieces = [witUrls.get()];
46-
angular.forEach(attributes['$attr'], function (attr) {
47-
if (attr != 'src' && attr != 'ng-src') {
48-
wit_link_pieces.push(attr + '_' + attributes[attr]);
52+
angular.forEach(allowedAttributes, function (attribute, index) {
53+
if (values[index]) {
54+
wit_link_pieces.push(attribute + '_' + values[index]);
4955
}
5056
});
57+
5158
wit_link_pieces.push(src);
5259
scope.wit_link = wit_link_pieces.join('/');
5360
});

test/unit/directiveSpec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
1818
scope.$digest();
1919

2020
expect(elt.html()).to.be.a('string');
21-
expect(elt.html()).to.be.equal('<div class="wit"><img src=""></div>');
21+
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src=""></div>');
2222
});
2323

2424
it('should work as an element with image source', function () {
@@ -27,7 +27,7 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
2727
scope.$digest();
2828

2929
expect(elt.html()).to.be.a('string');
30-
expect(elt.html()).to.be.equal('<div class="wit"><img src="//wit.wurfl.io/http://test.com/image.jpg"></div>');
30+
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit.wurfl.io/http://test.com/image.jpg" src="//wit.wurfl.io/http://test.com/image.jpg"></div>');
3131
});
3232

3333
it('should work as an element with image source and param attribute', function () {
@@ -36,7 +36,7 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
3636
scope.$digest();
3737

3838
expect(elt.html()).to.be.a('string');
39-
expect(elt.html()).to.be.equal('<div class="wit"><img src="//wit.wurfl.io/w_200/http://test.com/image.jpg"></div>');
39+
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>');
4040
});
4141

4242
it('should work as an element with ngSrc directive', function () {

0 commit comments

Comments
 (0)