Skip to content

Commit

Permalink
added getter id in base model for returning firebase $key
Browse files Browse the repository at this point in the history
  • Loading branch information
pkrawat1 committed Nov 30, 2016
1 parent fa84d6b commit d0056c8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/app/effects/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as pin from '../actions/pin';
import { AppState } from '../reducers/index';
import { LoginSuccessAction, LogoutAction } from '../actions/user-auth';
import { PinDataService } from '../services/pin-data';
import { Pin } from '../models/pin';

@Injectable()
export class PinEffects {
Expand Down
4 changes: 4 additions & 0 deletions src/app/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export class Base {
constructor(attributes?) {
Object.assign(this, attributes);
}

get id():string {
return this.$key
}
}
4 changes: 2 additions & 2 deletions src/app/reducers/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export function reducer(state = initialState, action: pin.Actions): State {
switch(action.type) {
case pin.ActionTypes.GET_PINS_SUCESS: {
const pins = action.payload;
const newPins = pins.filter(pin => !state.entities[pin.$key])
const newPins = pins.filter(pin => !state.entities[pin.id])

const newPinIds = newPins.map(pin => pin.$key);
const newPinEntities = newPins.reduce((entities: { [id: string]: Pin }, pin: Pin) => {
return Object.assign(entities, {
[pin.$key]: pin
[pin.id]: pin
});
}, {});

Expand Down
3 changes: 2 additions & 1 deletion src/app/services/pin-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class PinDataService {
};

getPins() {
return this.db.list('pins').map((pins: Pin[]) => pins);
return this.db.list('pins')
.map(pins => pins.map(pin => new Pin(pin)));
}
}

0 comments on commit d0056c8

Please sign in to comment.