File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
test/spec/filter/collection Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @ngdoc filter
3+ * @name defaults
4+ * @kind function
5+ *
6+ * @description
7+ * defaultsFilter allows to specify a default fallback value for properties that resolve to undefined.
8+ */
9+
10+ angular . module ( 'a8m.defaults' , [ ] )
11+ . filter ( 'defaults' , [ '$parse' , function ( $parse ) {
12+ return function ( collection , defaults ) {
13+
14+ collection = ( isObject ( collection ) ) ? toArray ( collection ) : collection ;
15+
16+ if ( ! isArray ( collection ) || ! isObject ( defaults ) ) {
17+ return collection ;
18+ }
19+
20+ var keys = deepKeys ( defaults ) ;
21+
22+ collection . forEach ( function ( elm ) {
23+ //loop through all the keys
24+ keys . forEach ( function ( key ) {
25+ var getter = $parse ( key ) ;
26+ var setter = getter . assign ;
27+ //if it's not exist
28+ if ( isUndefined ( getter ( elm ) ) ) {
29+ //get from defaults, and set to the returned object
30+ setter ( elm , getter ( defaults ) )
31+ }
32+ } ) ;
33+ } ) ;
34+
35+ return collection ;
36+ }
37+ } ] ) ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Created by arielmashraki on 10/12/14.
3+ */
You can’t perform that action at this time.
0 commit comments