-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
149 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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# | ||
* | ||
|
@@ -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 + ''); | ||
}, | ||
|
||
/** | ||
|
@@ -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; | ||
}, | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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. | ||
|
||
|
@@ -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; | ||
}, | ||
|
@@ -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; | ||
|
@@ -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' }); | ||
|
@@ -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' }); | ||
|
@@ -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() {} | ||
}); | ||
} | ||
} | ||
|
@@ -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]; | ||
|
@@ -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)); | ||
} | ||
|
||
|
@@ -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); | ||
|
@@ -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 | ||
|
@@ -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)) { | ||
|
@@ -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) { | ||
|
@@ -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; | ||
|
@@ -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)) { | ||
|
@@ -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) { | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.