|
1 | 1 | /** |
2 | 2 | * API Bound Models for AngularJS |
3 | | - * @version v1.1.8 - 2015-02-18 |
| 3 | + * @version v1.1.9 - 2015-05-07 |
4 | 4 | * @link https://github.com/angular-platanus/restmod |
5 | 5 | * @author Ignacio Baixas <[email protected]> |
6 | 6 | * @license MIT License, http://www.opensource.org/licenses/MIT |
@@ -621,22 +621,22 @@ RMModule.factory('RMCollectionApi', ['RMUtils', function(Utils) { |
621 | 621 | * |
622 | 622 | * @description Finds the location of an object in the array. |
623 | 623 | * |
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. |
625 | 625 | * |
626 | 626 | * @param {RecordApi|function} _obj Object to find |
| 627 | + * @param {integer} _fromIdx Index from which to start searching, defaults to 0 |
627 | 628 | * @return {number} Object index or -1 if not found |
628 | 629 | */ |
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); |
635 | 635 | } |
636 | 636 | }; |
637 | 637 |
|
638 | 638 | }]); |
639 | | -RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, $log) { |
| 639 | +RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function($http, $q, $log, Utils) { |
640 | 640 |
|
641 | 641 | var EMPTY_ARRAY = []; |
642 | 642 |
|
@@ -796,6 +796,23 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, |
796 | 796 | return this; |
797 | 797 | }, |
798 | 798 |
|
| 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 | + |
799 | 816 | /** |
800 | 817 | * @memberof CommonApi# |
801 | 818 | * |
@@ -1006,29 +1023,31 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, |
1006 | 1023 |
|
1007 | 1024 | return $http(_options).then(wrapPromise(this, function() { |
1008 | 1025 | if(action && action.canceled) { |
1009 | | - // if request was canceled during request, ignore post request actions. |
1010 | 1026 | this.$status = 'canceled'; |
| 1027 | + this.$dispatch('after-request-cancel', []); |
| 1028 | + return $q.reject(this); |
1011 | 1029 | } else { |
1012 | 1030 | this.$status = 'ok'; |
1013 | 1031 | 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); |
1016 | 1034 | } |
1017 | 1035 | }), wrapPromise(this, function() { |
1018 | 1036 | if(action && action.canceled) { |
1019 | | - // if request was canceled during request, ignore error handling |
1020 | 1037 | this.$status = 'canceled'; |
| 1038 | + this.$dispatch('after-request-cancel', []); |
1021 | 1039 | } else { |
1022 | 1040 | this.$status = 'error'; |
1023 | 1041 | this.$response = this.$last; |
1024 | 1042 |
|
1025 | 1043 | // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests |
1026 | 1044 | // until the error flag is reset by user. |
1027 | 1045 |
|
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); |
1031 | 1048 | } |
| 1049 | + |
| 1050 | + return $q.reject(this); // TODO: this will step over any promise generated in _error!! |
1032 | 1051 | })); |
1033 | 1052 | }); |
1034 | 1053 | }, |
@@ -2722,14 +2741,10 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils', |
2722 | 2741 |
|
2723 | 2742 | // make sure the resource name and plural name are available if posible: |
2724 | 2743 |
|
2725 | | - if(!config.name && _baseUrl) { |
| 2744 | + if(_baseUrl) { |
2726 | 2745 | config.name = inflector.singularize(_baseUrl.replace(NAME_RGX, '$2')); |
2727 | 2746 | } |
2728 | 2747 |
|
2729 | | - if(!config.plural && config.name) { |
2730 | | - config.plural = inflector.pluralize(config.name); |
2731 | | - } |
2732 | | - |
2733 | 2748 | var Collection = Utils.buildArrayType(), |
2734 | 2749 | List = Utils.buildArrayType(), |
2735 | 2750 | Dummy = function(_asCollection) { |
@@ -2919,17 +2934,24 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils', |
2919 | 2934 | * it should be manually set by writing the name and plural properties: |
2920 | 2935 | * |
2921 | 2936 | * ```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 | + * } |
2925 | 2942 | * }); |
2926 | 2943 | * ``` |
2927 | 2944 | * |
2928 | 2945 | * @return {boolean} If true, return plural name |
2929 | 2946 | * @return {string} The base url. |
2930 | 2947 | */ |
2931 | 2948 | 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; |
2933 | 2955 | }, |
2934 | 2956 |
|
2935 | 2957 | /** |
@@ -3897,10 +3919,10 @@ RMModule.factory('DefaultPacker', ['restmod', 'inflector', 'RMPackerCache', func |
3897 | 3919 | meta = this.getProperty('jsonMeta', 'meta'); |
3898 | 3920 |
|
3899 | 3921 | 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); |
3901 | 3923 | } else { |
3902 | 3924 | // 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(); |
3904 | 3926 | } |
3905 | 3927 |
|
3906 | 3928 | if(meta) { |
@@ -4066,6 +4088,25 @@ RMModule.factory('RMUtils', ['$log', function($log) { |
4066 | 4088 | }; |
4067 | 4089 | }, |
4068 | 4090 |
|
| 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 | + |
4069 | 4110 | /** |
4070 | 4111 | * @memberof Utils |
4071 | 4112 | * |
|
0 commit comments