Skip to content

Commit

Permalink
1.1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
iobaixas committed Oct 24, 2015
1 parent 28636f6 commit 9f6e180
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 101 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
<a name="1.1.10"></a>
### 1.1.10 (2015-10-24)


#### Bug Fixes

* **CommonApi:** makes canceled request promises resolve to error ([afcde0b4](http://github.com/angular-platanus/restmod/commit/afcde0b4c19c6a05cc7b09378e05b4462fb2cfc6), closes [#288](http://github.com/angular-platanus/restmod/issues/288))
* **DefaultPacker:** fixes plural name not being infered from name. ([e4c02a13](http://github.com/angular-platanus/restmod/commit/e4c02a13447d3862bbfcf7eb7d1ee17e20bb22cd), closes [#224](http://github.com/angular-platanus/restmod/issues/224))
* **Relations:** fixes hasMany and hasOne failing when inline property is null. ([05ab5479](http://github.com/angular-platanus/restmod/commit/05ab5479b9aed15fc5d42f09c6bad43b7f6239aa))
* **Serializer:** fixes decoder being called on mapped properties even if the parent object was nu ([e8041b8d](http://github.com/angular-platanus/restmod/commit/e8041b8d9e5ba9aeb6833afce67bd1b7267df91a))


#### Features

* **CommonApi:** adds $off method to unregister callbacks ([591db81b](http://github.com/angular-platanus/restmod/commit/591db81b223c57b7b94b306c629aa3ab22d34f6e), closes [#257](http://github.com/angular-platanus/restmod/issues/257))
* **ModelApi:** makes identity do the pluralizing if plural is not defined ([b94b349b](http://github.com/angular-platanus/restmod/commit/b94b349b2d8038fb68af9b51f1274a7dc14a320a))
* **RecordApi:**
* adds $isNew method ([104f3c25](http://github.com/angular-platanus/restmod/commit/104f3c25ea32ee1444c2f79f2736804f3ee382a6))
* makes $decode throw if encoded value is null or not an object. ([28636f69](http://github.com/angular-platanus/restmod/commit/28636f69783724a1ea5f50aad07029f0281ab21c))
* **Serializer:**
* adds dynamic mask support ([51905cb5](http://github.com/angular-platanus/restmod/commit/51905cb5c8ebd226d15c323fc2961590810cb7de))
* mask now is passed to decoder/encoder as the second argument ([9db02baf](http://github.com/angular-platanus/restmod/commit/9db02bafc68a89c0f5d7a9e294875301b3ce01ce))


<a name="1.1.9"></a>
### 1.1.9 (2015-05-07)

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-restmod",
"version": "1.1.9",
"version": "1.1.10",
"authors": [
"Ignacio Baixas <[email protected]>"
],
Expand Down
92 changes: 52 additions & 40 deletions dist/angular-restmod-bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* API Bound Models for AngularJS
* @version v1.1.9 - 2015-05-07
* @version v1.1.10 - 2015-10-24
* @link https://github.com/angular-platanus/restmod
* @author Ignacio Baixas <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
Expand Down Expand Up @@ -1402,6 +1402,17 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
this.$dispatch('after-init');
},

/**
* @memberof RecordApi#
*
* @description Checks whether a record is new or not
*
* @return {boolean} True if record is new.
*/
$isNew: function() {
return this.$pk === undefined || this.$pk === null;
},

/**
* @memberof RecordApi#
*
Expand All @@ -1412,7 +1423,7 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
* @return {string} The resource partial url
*/
$buildUrl: function(_scope) {
return (this.$pk === undefined || this.$pk === null) ? null : Utils.joinUrl(_scope.$url(), this.$pk + '');
return this.$isNew() ? null : Utils.joinUrl(_scope.$url(), this.$pk + '');
},

/**
Expand Down Expand Up @@ -1465,9 +1476,12 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
* @return {RecordApi} this
*/
$decode: function(_raw, _mask) {

Utils.assert(_raw && typeof _raw == 'object', 'Record $decode expected an object');

// IDEA: let user override serializer
this.$type.decode(this, _raw, _mask || Utils.READ_MASK);
if(this.$pk === undefined || this.$pk === null) this.$pk = this.$type.inferKey(_raw); // TODO: warn if key changes
if(this.$isNew()) this.$pk = this.$type.inferKey(_raw); // TODO: warn if key changes
this.$dispatch('after-feed', [_raw]);
return this;
},
Expand Down Expand Up @@ -1600,8 +1614,9 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
.$dispatch('before-update', [request, !!_patch])
.$dispatch('before-save', [request])
.$send(request, function(_response) {
if(_response.data) this.$unwrap(_response.data);

this
.$unwrap(_response.data)
.$dispatch('after-update', [_response, !!_patch])
.$dispatch('after-save', [_response]);
}, function(_response) {
Expand All @@ -1619,7 +1634,7 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
.$dispatch('before-save', [request])
.$dispatch('before-create', [request])
.$send(request, function(_response) {
this.$unwrap(_response.data);
if(_response.data) this.$unwrap(_response.data);

// reveal item (if not yet positioned)
if(this.$scope.$isCollection && this.$position === undefined && !this.$preventReveal) {
Expand Down Expand Up @@ -2020,15 +2035,14 @@ RMModule.factory('RMBuilder', ['$injector', 'inflector', '$log', 'RMUtils', func
*/
function Builder(_baseDsl) {

var mappings = {
init: ['attrDefault'],
mask: ['attrMask'],
ignore: ['attrMask'],
map: ['attrMap', 'force'],
decode: ['attrDecoder', 'param', 'chain'],
encode: ['attrEncoder', 'param', 'chain'],
'volatile': ['attrVolatile']
};
var mappings = [
{ fun: 'attrDefault', sign: ['init'] },
{ fun: 'attrMask', sign: ['ignore'] },
{ fun: 'attrMap', sign: ['map', 'force'] },
{ fun: 'attrDecoder', sign: ['decode', 'param', 'chain'] },
{ fun: 'attrEncoder', sign: ['encode', 'param', 'chain'] },
{ fun: 'attrVolatile', sign: ['volatile'] }
];

// DSL core functions.

Expand Down Expand Up @@ -2113,10 +2127,7 @@ RMModule.factory('RMBuilder', ['$injector', 'inflector', '$log', 'RMUtils', func
extend: function(_name, _fun, _mapping) {
if(typeof _name === 'string') {
this[_name] = Utils.override(this[name], _fun);
if(_mapping) {
mappings[_mapping[0]] = _mapping;
_mapping[0] = _name;
}
if(_mapping) mappings.push({ fun: _name, sign: _mapping });
} else Utils.extendOverriden(this, _name);
return this;
},
Expand All @@ -2142,18 +2153,15 @@ RMModule.factory('RMBuilder', ['$injector', 'inflector', '$log', 'RMUtils', func
* @return {BuilderApi} self
*/
attribute: function(_name, _description) {
var key, map, args, i;
for(key in _description) {
if(_description.hasOwnProperty(key)) {
map = mappings[key];
if(map) {
args = [_name, _description[key]];
for(i = 1; i < map.length; i++) {
args.push(_description[map[i]]);
}
args.push(_description);
this[map[0]].apply(this, args);
var i = 0, map;
while((map = mappings[i++])) {
if(_description.hasOwnProperty(map.sign[0])) {
var args = [_name];
for(var j = 0; j < map.sign.length; j++) {
args.push(_description[map.sign[j]]);
}
args.push(_description);
this[map.fun].apply(this, args);
}
}
return this;
Expand Down Expand Up @@ -2436,7 +2444,7 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
if(_source || _url) this.attrMap(_attr, _source || _url);

this.attrDecoder(_attr, function(_raw) {
this[_attr].$reset().$decode(_raw);
this[_attr].$reset().$decode(_raw || []);
})
.attrMask(_attr, Utils.WRITE_MASK)
.attrMeta(_attr, { relation: 'has_many' });
Expand Down Expand Up @@ -2497,7 +2505,7 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
if(_source || _url) this.attrMap(_attr, _source || _url);

this.attrDecoder(_attr, function(_raw) {
this[_attr].$decode(_raw);
this[_attr].$decode(_raw || {}); // TODO: null _raw should clear the object properties
})
.attrMask(_attr, Utils.WRITE_MASK)
.attrMeta(_attr, { relation: 'has_one' });
Expand Down Expand Up @@ -3084,7 +3092,8 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils',
for(i = 0; (tmp = computes[i]); i++) {
Object.defineProperty(self, tmp[0], {
enumerable: true,
get: tmp[1]
get: tmp[1],
set: function() {}
});
}
}
Expand Down Expand Up @@ -3499,6 +3508,8 @@ RMModule.factory('RMPackerCache', [function() {
RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils', function($injector, inflector, $filter, Utils) {

function extract(_from, _path) {
if(_from === null && _path.length > 1) return undefined;

var node;
for(var i = 0; _from && (node = _path[i]); i++) {
_from = _from[node];
Expand Down Expand Up @@ -3526,9 +3537,10 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'
mappings = {},
vol = {};

function isMasked(_name, _mask) {
function isMasked(_name, _mask, _ctx) {
if(typeof _mask === 'function') return _mask(_name);
var mask = masks[_name];
if(typeof mask === 'function') mask = mask.call(_ctx); // dynamic mask
return (mask && (mask === true || mask.indexOf(_mask) !== -1));
}

Expand All @@ -3541,7 +3553,7 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'
if(maps) {
for(i = 0, l = maps.length; i < l; i++) {
fullName = prefix + maps[i].path;
if(isMasked(fullName, _mask)) continue;
if(isMasked(fullName, _mask, _ctx)) continue;

if(maps[i].map) {
value = extract(_from, maps[i].map);
Expand Down Expand Up @@ -3577,7 +3589,7 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'

fullName = prefix + decodedName;
// prevent masked or already mapped properties to be set
if(mapped[fullName] || isMasked(fullName, _mask)) continue;
if(mapped[fullName] || isMasked(fullName, _mask, _ctx)) continue;

value = decodeProp(_from[key], fullName, _mask, _ctx);
if(value !== undefined) _to[decodedName] = value; // ignore value if filter returns undefined
Expand All @@ -3589,7 +3601,7 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'
var filter = decoders[_name], result = _value;

if(filter) {
result = filter.call(_ctx, _value);
result = filter.call(_ctx, _value, _mask);
} else if(typeof _value === 'object') {
// IDEA: make extended decoding/encoding optional, could be a little taxing for some apps
if(isArray(_value)) {
Expand All @@ -3615,7 +3627,7 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'
if(_from.hasOwnProperty(key) && key[0] !== '$') {
fullName = prefix + key;
// prevent masked or already mapped properties to be copied
if(mapped[fullName] || isMasked(fullName, _mask)) continue;
if(mapped[fullName] || isMasked(fullName, _mask, _ctx)) continue;

value = encodeProp(_from[key], fullName, _mask, _ctx);
if(value !== undefined) {
Expand All @@ -3632,7 +3644,7 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'
if(maps) {
for(var i = 0, l = maps.length; i < l; i++) {
fullName = prefix + maps[i].path;
if(isMasked(fullName, _mask)) continue;
if(isMasked(fullName, _mask, _ctx)) continue;

value = _from[maps[i].path];
if(!maps[i].forced && value === undefined) continue;
Expand All @@ -3653,7 +3665,7 @@ RMModule.factory('RMSerializer', ['$injector', 'inflector', '$filter', 'RMUtils'
var filter = encoders[_name], result = _value;

if(filter) {
result = filter.call(_ctx, _value);
result = filter.call(_ctx, _value, _mask);
} else if(_value !== null && typeof _value === 'object' && typeof _value.toJSON !== 'function') {
// IDEA: make deep decoding/encoding optional, could be a little taxing for some apps
if(isArray(_value)) {
Expand Down Expand Up @@ -3915,7 +3927,7 @@ RMModule.factory('DefaultPacker', ['restmod', 'inflector', 'RMPackerCache', func
return restmod.mixin(function() {
this.define('Model.unpack', function(_resource, _raw) {
var name = null,
links = this.getProperty('jsonLinks', 'linked'),
links = this.getProperty('jsonLinks', 'included'),
meta = this.getProperty('jsonMeta', 'meta');

if(_resource.$isCollection) {
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-restmod-bundle.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 9f6e180

Please sign in to comment.