Skip to content

Commit cc0e788

Browse files
committed
1.1.9
1 parent ddad7e4 commit cc0e788

21 files changed

+172
-75
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
<a name="1.1.9"></a>
2+
### 1.1.9 (2015-05-07)
3+
4+
5+
#### Bug Fixes
6+
7+
* **CommonApi:** makes canceled request promises resolve to error ([0b8f21c5](http://github.com/angular-platanus/restmod/commit/0b8f21c51adfc101485498c65822311b7384583a), closes [#288](http://github.com/angular-platanus/restmod/issues/288))
8+
* **DefaultPacker:** fixes plural name not being infered from name. ([ddad7e44](http://github.com/angular-platanus/restmod/commit/ddad7e4491433e35dae59757ba5776ead1668a03), closes [#224](http://github.com/angular-platanus/restmod/issues/224))
9+
10+
#### Features
11+
12+
* **CommonApi:** adds $off method to unregister callbacks ([59fd3b84](http://github.com/angular-platanus/restmod/commit/59fd3b84bafd2bce159a4a254c24592cb799af3a), closes [#257](http://github.com/angular-platanus/restmod/issues/257))
13+
* **ModelApi:** makes identity do the pluralizing if plural is not defined ([d2f0c0c3](http://github.com/angular-platanus/restmod/commit/d2f0c0c3174743cd66a87382957396329be1859c))
14+
15+
116
<a name="1.1.8"></a>
217
### 1.1.8 (2015-02-18)
318

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-restmod",
3-
"version": "1.1.8",
3+
"version": "1.1.9",
44
"authors": [
55
"Ignacio Baixas <[email protected]>"
66
],

dist/angular-restmod-bundle.js

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* API Bound Models for AngularJS
3-
* @version v1.1.8 - 2015-02-18
3+
* @version v1.1.9 - 2015-05-07
44
* @link https://github.com/angular-platanus/restmod
55
* @author Ignacio Baixas <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -621,22 +621,22 @@ RMModule.factory('RMCollectionApi', ['RMUtils', function(Utils) {
621621
*
622622
* @description Finds the location of an object in the array.
623623
*
624-
* If a function is provided then the index of the first item for which the function returns true is returned.
624+
* If a function is provided then the index of the first item for which the function returns true.
625625
*
626626
* @param {RecordApi|function} _obj Object to find
627+
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
627628
* @return {number} Object index or -1 if not found
628629
*/
629-
$indexOf: function(_obj) {
630-
var accept = typeof _obj === 'function' ? _obj : false;
631-
for(var i = 0, l = this.length; i < l; i++) {
632-
if(accept ? accept(this[i]) : this[i] === _obj) return i;
633-
}
634-
return -1;
630+
$indexOf: function(_obj, _fromIdx) {
631+
var accept = typeof _obj !== 'function' ?
632+
function(e) { return e === _obj; } : _obj;
633+
634+
return Utils.indexWhere(this, accept, _fromIdx);
635635
}
636636
};
637637

638638
}]);
639-
RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, $log) {
639+
RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function($http, $q, $log, Utils) {
640640

641641
var EMPTY_ARRAY = [];
642642

@@ -796,6 +796,23 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
796796
return this;
797797
},
798798

799+
/**
800+
* @memberof CommonApi#
801+
*
802+
* @description Unregisters an instance hook registered with `$on`
803+
*
804+
* @param {string} _hook Hook name
805+
* @param {function} _fun Original callback
806+
* @return {CommonApi} self
807+
*/
808+
$off: function(_hook, _fun) {
809+
if(this.$$cb && this.$$cb[_hook]) {
810+
var idx = Utils.indexWhere(this.$$cb[_hook], function(e) { return e === _fun; });
811+
if(idx !== -1) this.$$cb[_hook].splice(idx, 1);
812+
}
813+
return this;
814+
},
815+
799816
/**
800817
* @memberof CommonApi#
801818
*
@@ -1006,29 +1023,31 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
10061023

10071024
return $http(_options).then(wrapPromise(this, function() {
10081025
if(action && action.canceled) {
1009-
// if request was canceled during request, ignore post request actions.
10101026
this.$status = 'canceled';
1027+
this.$dispatch('after-request-cancel', []);
1028+
return $q.reject(this);
10111029
} else {
10121030
this.$status = 'ok';
10131031
this.$response = this.$last;
1014-
this.$dispatch('after-request', [this.$last]);
1015-
if(_success) _success.call(this, this.$last);
1032+
this.$dispatch('after-request', [this.$response]);
1033+
if(_success) _success.call(this, this.$response);
10161034
}
10171035
}), wrapPromise(this, function() {
10181036
if(action && action.canceled) {
1019-
// if request was canceled during request, ignore error handling
10201037
this.$status = 'canceled';
1038+
this.$dispatch('after-request-cancel', []);
10211039
} else {
10221040
this.$status = 'error';
10231041
this.$response = this.$last;
10241042

10251043
// IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
10261044
// until the error flag is reset by user.
10271045

1028-
this.$dispatch('after-request-error', [this.$last]);
1029-
if(_error) _error.call(this, this.$last);
1030-
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
1046+
this.$dispatch('after-request-error', [this.$response]);
1047+
if(_error) _error.call(this, this.$response);
10311048
}
1049+
1050+
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
10321051
}));
10331052
});
10341053
},
@@ -2722,14 +2741,10 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
27222741

27232742
// make sure the resource name and plural name are available if posible:
27242743

2725-
if(!config.name && _baseUrl) {
2744+
if(_baseUrl) {
27262745
config.name = inflector.singularize(_baseUrl.replace(NAME_RGX, '$2'));
27272746
}
27282747

2729-
if(!config.plural && config.name) {
2730-
config.plural = inflector.pluralize(config.name);
2731-
}
2732-
27332748
var Collection = Utils.buildArrayType(),
27342749
List = Utils.buildArrayType(),
27352750
Dummy = function(_asCollection) {
@@ -2919,17 +2934,24 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
29192934
* it should be manually set by writing the name and plural properties:
29202935
*
29212936
* ```javascript
2922-
* restmod.model(null, {
2923-
* __name__: 'resource',
2924-
* __plural__: 'resourciness' // set only if inflector cant properly gess the name.
2937+
* restmod.model().mix{
2938+
* $config: {
2939+
* name: 'resource',
2940+
* plural: 'resourciness' // set only if inflector cant properly gess the name.
2941+
* }
29252942
* });
29262943
* ```
29272944
*
29282945
* @return {boolean} If true, return plural name
29292946
* @return {string} The base url.
29302947
*/
29312948
identity: function(_plural) {
2932-
return _plural ? config.plural : config.name;
2949+
if(!_plural) return config.name;
2950+
if(_plural) {
2951+
if(config.plural) return config.plural;
2952+
if(config.name) return inflector.pluralize(config.name);
2953+
}
2954+
return null;
29332955
},
29342956

29352957
/**
@@ -3897,10 +3919,10 @@ RMModule.factory('DefaultPacker', ['restmod', 'inflector', 'RMPackerCache', func
38973919
meta = this.getProperty('jsonMeta', 'meta');
38983920

38993921
if(_resource.$isCollection) {
3900-
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.getProperty('plural');
3922+
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.identity(true);
39013923
} else {
39023924
// TODO: use plural for single resource option.
3903-
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.getProperty('name');
3925+
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.identity();
39043926
}
39053927

39063928
if(meta) {
@@ -4066,6 +4088,25 @@ RMModule.factory('RMUtils', ['$log', function($log) {
40664088
};
40674089
},
40684090

4091+
/**
4092+
* @memberof Utils
4093+
*
4094+
* @description
4095+
*
4096+
* Finds the location of a matching object in an array.
4097+
*
4098+
* @param {array} _array target array
4099+
* @param {function} _accept matching function
4100+
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
4101+
* @return {number} Object index or -1 if not found
4102+
*/
4103+
indexWhere: function(_array, _accept, _fromIdx) {
4104+
for(var i = _fromIdx || 0, l = _array.length; i < l; i++) {
4105+
if(_accept(_array[i])) return i;
4106+
}
4107+
return -1;
4108+
},
4109+
40694110
/**
40704111
* @memberof Utils
40714112
*

dist/angular-restmod-bundle.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-restmod.js

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* API Bound Models for AngularJS
3-
* @version v1.1.8 - 2015-02-18
3+
* @version v1.1.9 - 2015-05-07
44
* @link https://github.com/angular-platanus/restmod
55
* @author Ignacio Baixas <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -383,22 +383,22 @@ RMModule.factory('RMCollectionApi', ['RMUtils', function(Utils) {
383383
*
384384
* @description Finds the location of an object in the array.
385385
*
386-
* If a function is provided then the index of the first item for which the function returns true is returned.
386+
* If a function is provided then the index of the first item for which the function returns true.
387387
*
388388
* @param {RecordApi|function} _obj Object to find
389+
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
389390
* @return {number} Object index or -1 if not found
390391
*/
391-
$indexOf: function(_obj) {
392-
var accept = typeof _obj === 'function' ? _obj : false;
393-
for(var i = 0, l = this.length; i < l; i++) {
394-
if(accept ? accept(this[i]) : this[i] === _obj) return i;
395-
}
396-
return -1;
392+
$indexOf: function(_obj, _fromIdx) {
393+
var accept = typeof _obj !== 'function' ?
394+
function(e) { return e === _obj; } : _obj;
395+
396+
return Utils.indexWhere(this, accept, _fromIdx);
397397
}
398398
};
399399

400400
}]);
401-
RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, $log) {
401+
RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function($http, $q, $log, Utils) {
402402

403403
var EMPTY_ARRAY = [];
404404

@@ -558,6 +558,23 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
558558
return this;
559559
},
560560

561+
/**
562+
* @memberof CommonApi#
563+
*
564+
* @description Unregisters an instance hook registered with `$on`
565+
*
566+
* @param {string} _hook Hook name
567+
* @param {function} _fun Original callback
568+
* @return {CommonApi} self
569+
*/
570+
$off: function(_hook, _fun) {
571+
if(this.$$cb && this.$$cb[_hook]) {
572+
var idx = Utils.indexWhere(this.$$cb[_hook], function(e) { return e === _fun; });
573+
if(idx !== -1) this.$$cb[_hook].splice(idx, 1);
574+
}
575+
return this;
576+
},
577+
561578
/**
562579
* @memberof CommonApi#
563580
*
@@ -768,29 +785,31 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
768785

769786
return $http(_options).then(wrapPromise(this, function() {
770787
if(action && action.canceled) {
771-
// if request was canceled during request, ignore post request actions.
772788
this.$status = 'canceled';
789+
this.$dispatch('after-request-cancel', []);
790+
return $q.reject(this);
773791
} else {
774792
this.$status = 'ok';
775793
this.$response = this.$last;
776-
this.$dispatch('after-request', [this.$last]);
777-
if(_success) _success.call(this, this.$last);
794+
this.$dispatch('after-request', [this.$response]);
795+
if(_success) _success.call(this, this.$response);
778796
}
779797
}), wrapPromise(this, function() {
780798
if(action && action.canceled) {
781-
// if request was canceled during request, ignore error handling
782799
this.$status = 'canceled';
800+
this.$dispatch('after-request-cancel', []);
783801
} else {
784802
this.$status = 'error';
785803
this.$response = this.$last;
786804

787805
// IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
788806
// until the error flag is reset by user.
789807

790-
this.$dispatch('after-request-error', [this.$last]);
791-
if(_error) _error.call(this, this.$last);
792-
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
808+
this.$dispatch('after-request-error', [this.$response]);
809+
if(_error) _error.call(this, this.$response);
793810
}
811+
812+
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
794813
}));
795814
});
796815
},
@@ -2484,14 +2503,10 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
24842503

24852504
// make sure the resource name and plural name are available if posible:
24862505

2487-
if(!config.name && _baseUrl) {
2506+
if(_baseUrl) {
24882507
config.name = inflector.singularize(_baseUrl.replace(NAME_RGX, '$2'));
24892508
}
24902509

2491-
if(!config.plural && config.name) {
2492-
config.plural = inflector.pluralize(config.name);
2493-
}
2494-
24952510
var Collection = Utils.buildArrayType(),
24962511
List = Utils.buildArrayType(),
24972512
Dummy = function(_asCollection) {
@@ -2681,17 +2696,24 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
26812696
* it should be manually set by writing the name and plural properties:
26822697
*
26832698
* ```javascript
2684-
* restmod.model(null, {
2685-
* __name__: 'resource',
2686-
* __plural__: 'resourciness' // set only if inflector cant properly gess the name.
2699+
* restmod.model().mix{
2700+
* $config: {
2701+
* name: 'resource',
2702+
* plural: 'resourciness' // set only if inflector cant properly gess the name.
2703+
* }
26872704
* });
26882705
* ```
26892706
*
26902707
* @return {boolean} If true, return plural name
26912708
* @return {string} The base url.
26922709
*/
26932710
identity: function(_plural) {
2694-
return _plural ? config.plural : config.name;
2711+
if(!_plural) return config.name;
2712+
if(_plural) {
2713+
if(config.plural) return config.plural;
2714+
if(config.name) return inflector.pluralize(config.name);
2715+
}
2716+
return null;
26952717
},
26962718

26972719
/**
@@ -3659,10 +3681,10 @@ RMModule.factory('DefaultPacker', ['restmod', 'inflector', 'RMPackerCache', func
36593681
meta = this.getProperty('jsonMeta', 'meta');
36603682

36613683
if(_resource.$isCollection) {
3662-
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.getProperty('plural');
3684+
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.identity(true);
36633685
} else {
36643686
// TODO: use plural for single resource option.
3665-
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.getProperty('name');
3687+
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.identity();
36663688
}
36673689

36683690
if(meta) {
@@ -3828,6 +3850,25 @@ RMModule.factory('RMUtils', ['$log', function($log) {
38283850
};
38293851
},
38303852

3853+
/**
3854+
* @memberof Utils
3855+
*
3856+
* @description
3857+
*
3858+
* Finds the location of a matching object in an array.
3859+
*
3860+
* @param {array} _array target array
3861+
* @param {function} _accept matching function
3862+
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
3863+
* @return {number} Object index or -1 if not found
3864+
*/
3865+
indexWhere: function(_array, _accept, _fromIdx) {
3866+
for(var i = _fromIdx || 0, l = _array.length; i < l; i++) {
3867+
if(_accept(_array[i])) return i;
3868+
}
3869+
return -1;
3870+
},
3871+
38313872
/**
38323873
* @memberof Utils
38333874
*

0 commit comments

Comments
 (0)