Skip to content

Instantly share code, notes, and snippets.

@chadwithuhc
Created June 6, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadwithuhc/296185ed7972088805c89fac48d3d9c4 to your computer and use it in GitHub Desktop.
Save chadwithuhc/296185ed7972088805c89fac48d3d9c4 to your computer and use it in GitHub Desktop.
(function() {
'use strict'
angular.module('app')
.service('houseService', function () {
this.houses = [
{id: 1, name: 'Spacious two bedroom', address: '10 Main St'},
]
this.seedHouses = function() {
console.log('localStorage', window.localStorage)
if (!window.localStorage.getItem('houses')) {
window.localStorage.setItem('houses', JSON.stringify(this.houses))
console.log('Created fake houses', JSON.parse(window.localStorage.houses))
}
}
this.seedHouses()
this.getHouses = function() {
return JSON.parse(window.localStorage.getItem('houses'))
}
this.setHouses = function(houses) {
window.localStorage.setItem('houses', JSON.stringify(houses))
return true
}
this.addHouse = function (house) {
let houses = this.getHouses()
houses.push(house)
house.id = houses.length
this.setHouses(houses)
return house
}
this.findById = function (id) {
let houses = this.getHouses()
return houses.find(house => house.id == id)
}
})
}());
// (function() {
// 'use strict'
//
// angular.module('app')
// .service('houseService', function () {
// this.houses = [
// {id: 1, name: 'Spacious two bedroom', address: '10 Main St'},
// ]
//
// this.addHouse = function (house) {
// this.houses.push(house)
// house.id = this.houses.length
// return house
// }
//
// this.findById = function (id) {
// return this.houses.find(house => house.id == id)
// }
// })
//
// }());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment