Last active
September 3, 2015 20:07
-
-
Save dreadjr/6bbb283cb378e71bd2df to your computer and use it in GitHub Desktop.
oloo tries
This file contains hidden or 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
var FirebaseService = { | |
init: function(fb) { | |
this.fb = fb; | |
}, | |
pathRef: function(args) { | |
for (var i = 0; i < args.length; i++) { | |
if (angular.isArray(args[i])) { | |
args[i] = this.pathRef(args[i]); | |
} | |
else if (typeof args[i] !== 'string') { | |
throw new Error('Argument ' + i + ' to firebaseRef is not a string: ' + args[i]); | |
} | |
} | |
return args.join('/'); | |
}, | |
ref: function() { | |
var ref = this.fb; | |
var args = Array.prototype.slice.call(arguments); | |
if (args.length) { | |
ref = ref.child(this.pathRef(args)); | |
} | |
return ref; | |
}, | |
once: function(ref, on) { | |
return new Promise(function (resolve, reject) { | |
ref.once(on || 'value', function (snap) { | |
resolve(snap); | |
}, function (err) { | |
reject(err); | |
}); | |
}); | |
} | |
} | |
module.exports = FirebaseService; |
This file contains hidden or 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
var FirebaseService = require('./FirebaseService'); | |
// maybe this way? | |
var ProfileService = Object.create(FirebaseService); | |
ProfileService.profileRef = function(id) { | |
return this.ref("users").child(id).child("profile"); | |
}; | |
Profile.getProfile = function(id) { | |
return this.once(this.getProfile(id), 'value'); | |
}; | |
var UserService = Object.create(FirebaseService); | |
UserService.usersRef = function(id) { | |
return this.ref("users").child(id); | |
}; | |
UserService.getUser = function(id) { | |
return this.once(this.usersRef(id), 'value'); | |
}; | |
// what to export? | |
module.exports = { | |
UserService: UserService, | |
ProfileService: ProfileService, | |
// ect. | |
}; |
This file contains hidden or 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
var FirebaseService = require('./FirebaseService'); | |
var UserService = Object.create(FirebaseService); | |
UserService.usersRef = function(id) { | |
return this.ref("users").child(id); | |
}; | |
UserService.getUser = function(id) { | |
return this.once(this.usersRef(id), 'value'); | |
}; | |
// or maybe this way? | |
var ProfileUserService = Object.create(UserService); | |
ProfileUserService.getProfile = function(id) { | |
return this.once(this.usersRef(id).child("profile"), 'value'); | |
}; | |
// what to export? | |
module.exports = { | |
UserService: UserService, | |
ProfileUserService: ProfileUserService, | |
// ect. | |
}; |
This file contains hidden or 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
var FullUserService = Object.create(UserService); | |
FullUserService.ProfileService = Object.create(ProfileService); | |
FullUserService.AnotherService = Object.create(AnotherService); | |
U.ProfileService.getProfile(); | |
// U.getProfile()? |
This file contains hidden or 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
var U = Object.create(ProfileUserService); // or just UserService | |
U.init(fb); | |
u.getUser("id").then(function(user) { | |
console.log('found user') | |
}); | |
u.getProfile("id").then(function(profile) { | |
console.log('found profile') | |
}); | |
//u.getSomethingElse("id").then(function(somethingelse) { | |
// console.log('found somethingelse') | |
//}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment