Skip to content

Commit

Permalink
1.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
iobaixas committed May 7, 2015
1 parent ddad7e4 commit cc0e788
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 75 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,18 @@
<a name="1.1.9"></a>
### 1.1.9 (2015-05-07)


#### Bug Fixes

* **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))
* **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))

#### Features

* **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))
* **ModelApi:** makes identity do the pluralizing if plural is not defined ([d2f0c0c3](http://github.com/angular-platanus/restmod/commit/d2f0c0c3174743cd66a87382957396329be1859c))


<a name="1.1.8"></a>
### 1.1.8 (2015-02-18)

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "angular-restmod",
"version": "1.1.8",
"version": "1.1.9",
"authors": [
"Ignacio Baixas <ignacio@platan.us>"
],
Expand Down
95 changes: 68 additions & 27 deletions dist/angular-restmod-bundle.js
@@ -1,6 +1,6 @@
/**
* API Bound Models for AngularJS
* @version v1.1.8 - 2015-02-18
* @version v1.1.9 - 2015-05-07
* @link https://github.com/angular-platanus/restmod
* @author Ignacio Baixas <ignacio@platan.us>
* @license MIT License, http://www.opensource.org/licenses/MIT
Expand Down Expand Up @@ -621,22 +621,22 @@ RMModule.factory('RMCollectionApi', ['RMUtils', function(Utils) {
*
* @description Finds the location of an object in the array.
*
* If a function is provided then the index of the first item for which the function returns true is returned.
* If a function is provided then the index of the first item for which the function returns true.
*
* @param {RecordApi|function} _obj Object to find
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
* @return {number} Object index or -1 if not found
*/
$indexOf: function(_obj) {
var accept = typeof _obj === 'function' ? _obj : false;
for(var i = 0, l = this.length; i < l; i++) {
if(accept ? accept(this[i]) : this[i] === _obj) return i;
}
return -1;
$indexOf: function(_obj, _fromIdx) {
var accept = typeof _obj !== 'function' ?
function(e) { return e === _obj; } : _obj;

return Utils.indexWhere(this, accept, _fromIdx);
}
};

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

var EMPTY_ARRAY = [];

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

/**
* @memberof CommonApi#
*
* @description Unregisters an instance hook registered with `$on`
*
* @param {string} _hook Hook name
* @param {function} _fun Original callback
* @return {CommonApi} self
*/
$off: function(_hook, _fun) {
if(this.$$cb && this.$$cb[_hook]) {
var idx = Utils.indexWhere(this.$$cb[_hook], function(e) { return e === _fun; });
if(idx !== -1) this.$$cb[_hook].splice(idx, 1);
}
return this;
},

/**
* @memberof CommonApi#
*
Expand Down Expand Up @@ -1006,29 +1023,31 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,

return $http(_options).then(wrapPromise(this, function() {
if(action && action.canceled) {
// if request was canceled during request, ignore post request actions.
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
return $q.reject(this);
} else {
this.$status = 'ok';
this.$response = this.$last;
this.$dispatch('after-request', [this.$last]);
if(_success) _success.call(this, this.$last);
this.$dispatch('after-request', [this.$response]);
if(_success) _success.call(this, this.$response);
}
}), wrapPromise(this, function() {
if(action && action.canceled) {
// if request was canceled during request, ignore error handling
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
} else {
this.$status = 'error';
this.$response = this.$last;

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

this.$dispatch('after-request-error', [this.$last]);
if(_error) _error.call(this, this.$last);
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
this.$dispatch('after-request-error', [this.$response]);
if(_error) _error.call(this, this.$response);
}

return $q.reject(this); // TODO: this will step over any promise generated in _error!!
}));
});
},
Expand Down Expand Up @@ -2722,14 +2741,10 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',

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

if(!config.name && _baseUrl) {
if(_baseUrl) {
config.name = inflector.singularize(_baseUrl.replace(NAME_RGX, '$2'));
}

if(!config.plural && config.name) {
config.plural = inflector.pluralize(config.name);
}

var Collection = Utils.buildArrayType(),
List = Utils.buildArrayType(),
Dummy = function(_asCollection) {
Expand Down Expand Up @@ -2919,17 +2934,24 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
* it should be manually set by writing the name and plural properties:
*
* ```javascript
* restmod.model(null, {
* __name__: 'resource',
* __plural__: 'resourciness' // set only if inflector cant properly gess the name.
* restmod.model().mix{
* $config: {
* name: 'resource',
* plural: 'resourciness' // set only if inflector cant properly gess the name.
* }
* });
* ```
*
* @return {boolean} If true, return plural name
* @return {string} The base url.
*/
identity: function(_plural) {
return _plural ? config.plural : config.name;
if(!_plural) return config.name;
if(_plural) {
if(config.plural) return config.plural;
if(config.name) return inflector.pluralize(config.name);
}
return null;
},

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

if(_resource.$isCollection) {
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.getProperty('plural');
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.identity(true);
} else {
// TODO: use plural for single resource option.
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.getProperty('name');
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.identity();
}

if(meta) {
Expand Down Expand Up @@ -4066,6 +4088,25 @@ RMModule.factory('RMUtils', ['$log', function($log) {
};
},

/**
* @memberof Utils
*
* @description
*
* Finds the location of a matching object in an array.
*
* @param {array} _array target array
* @param {function} _accept matching function
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
* @return {number} Object index or -1 if not found
*/
indexWhere: function(_array, _accept, _fromIdx) {
for(var i = _fromIdx || 0, l = _array.length; i < l; i++) {
if(_accept(_array[i])) return i;
}
return -1;
},

/**
* @memberof Utils
*
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-restmod-bundle.min.js

Large diffs are not rendered by default.

95 changes: 68 additions & 27 deletions dist/angular-restmod.js
@@ -1,6 +1,6 @@
/**
* API Bound Models for AngularJS
* @version v1.1.8 - 2015-02-18
* @version v1.1.9 - 2015-05-07
* @link https://github.com/angular-platanus/restmod
* @author Ignacio Baixas <ignacio@platan.us>
* @license MIT License, http://www.opensource.org/licenses/MIT
Expand Down Expand Up @@ -383,22 +383,22 @@ RMModule.factory('RMCollectionApi', ['RMUtils', function(Utils) {
*
* @description Finds the location of an object in the array.
*
* If a function is provided then the index of the first item for which the function returns true is returned.
* If a function is provided then the index of the first item for which the function returns true.
*
* @param {RecordApi|function} _obj Object to find
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
* @return {number} Object index or -1 if not found
*/
$indexOf: function(_obj) {
var accept = typeof _obj === 'function' ? _obj : false;
for(var i = 0, l = this.length; i < l; i++) {
if(accept ? accept(this[i]) : this[i] === _obj) return i;
}
return -1;
$indexOf: function(_obj, _fromIdx) {
var accept = typeof _obj !== 'function' ?
function(e) { return e === _obj; } : _obj;

return Utils.indexWhere(this, accept, _fromIdx);
}
};

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

var EMPTY_ARRAY = [];

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

/**
* @memberof CommonApi#
*
* @description Unregisters an instance hook registered with `$on`
*
* @param {string} _hook Hook name
* @param {function} _fun Original callback
* @return {CommonApi} self
*/
$off: function(_hook, _fun) {
if(this.$$cb && this.$$cb[_hook]) {
var idx = Utils.indexWhere(this.$$cb[_hook], function(e) { return e === _fun; });
if(idx !== -1) this.$$cb[_hook].splice(idx, 1);
}
return this;
},

/**
* @memberof CommonApi#
*
Expand Down Expand Up @@ -768,29 +785,31 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,

return $http(_options).then(wrapPromise(this, function() {
if(action && action.canceled) {
// if request was canceled during request, ignore post request actions.
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
return $q.reject(this);
} else {
this.$status = 'ok';
this.$response = this.$last;
this.$dispatch('after-request', [this.$last]);
if(_success) _success.call(this, this.$last);
this.$dispatch('after-request', [this.$response]);
if(_success) _success.call(this, this.$response);
}
}), wrapPromise(this, function() {
if(action && action.canceled) {
// if request was canceled during request, ignore error handling
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
} else {
this.$status = 'error';
this.$response = this.$last;

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

this.$dispatch('after-request-error', [this.$last]);
if(_error) _error.call(this, this.$last);
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
this.$dispatch('after-request-error', [this.$response]);
if(_error) _error.call(this, this.$response);
}

return $q.reject(this); // TODO: this will step over any promise generated in _error!!
}));
});
},
Expand Down Expand Up @@ -2484,14 +2503,10 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',

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

if(!config.name && _baseUrl) {
if(_baseUrl) {
config.name = inflector.singularize(_baseUrl.replace(NAME_RGX, '$2'));
}

if(!config.plural && config.name) {
config.plural = inflector.pluralize(config.name);
}

var Collection = Utils.buildArrayType(),
List = Utils.buildArrayType(),
Dummy = function(_asCollection) {
Expand Down Expand Up @@ -2681,17 +2696,24 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
* it should be manually set by writing the name and plural properties:
*
* ```javascript
* restmod.model(null, {
* __name__: 'resource',
* __plural__: 'resourciness' // set only if inflector cant properly gess the name.
* restmod.model().mix{
* $config: {
* name: 'resource',
* plural: 'resourciness' // set only if inflector cant properly gess the name.
* }
* });
* ```
*
* @return {boolean} If true, return plural name
* @return {string} The base url.
*/
identity: function(_plural) {
return _plural ? config.plural : config.name;
if(!_plural) return config.name;
if(_plural) {
if(config.plural) return config.plural;
if(config.name) return inflector.pluralize(config.name);
}
return null;
},

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

if(_resource.$isCollection) {
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.getProperty('plural');
name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.identity(true);
} else {
// TODO: use plural for single resource option.
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.getProperty('name');
name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.identity();
}

if(meta) {
Expand Down Expand Up @@ -3828,6 +3850,25 @@ RMModule.factory('RMUtils', ['$log', function($log) {
};
},

/**
* @memberof Utils
*
* @description
*
* Finds the location of a matching object in an array.
*
* @param {array} _array target array
* @param {function} _accept matching function
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
* @return {number} Object index or -1 if not found
*/
indexWhere: function(_array, _accept, _fromIdx) {
for(var i = _fromIdx || 0, l = _array.length; i < l; i++) {
if(_accept(_array[i])) return i;
}
return -1;
},

/**
* @memberof Utils
*
Expand Down

0 comments on commit cc0e788

Please sign in to comment.