-
-
Save katowulf/a8466f4d66a4cea7af7c to your computer and use it in GitHub Desktop.
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
// with AngularFire | |
app.controller('ctrl', function($scope, $firebase) { | |
$scope.list = $firebase(new Firebase(URL)); | |
}); |
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
// without AngularFire | |
app.controller('ctrl', function($scope, $timeout, $q) { | |
$scope.list = {}; | |
var ref = new Firebase(URL); | |
ref.on('child_added', function(snap, prevChild) { | |
// ignoring prevChild, that's an additional 20-30 lines for functionality | |
updateData(snap); | |
}); | |
ref.on('child_removed', function(snap) { | |
delete $scope.list[snap.name()]; | |
}); | |
ref.on('child_moved', function(snap) { | |
// skipped for for brevity, add about 20-30 lines for this functionality | |
}); | |
ref.on('child_changed', function(snap) { | |
updateData(snap); | |
}); | |
$scope.list.$save = function(key, value) { | |
var def = $q.defer(); | |
var childRef = ref; | |
if( key ) { childRef = childRef.child(key); } | |
// there's some additional complexity to getting these promises | |
// to work with the compiler; we'll skip those for this example | |
childRef.set(value, function(err) { if( err ) { def.reject(err); } else { def.resolve(childRef) } ); | |
return def.promise; | |
}; | |
$scope.list.$remove = function(key, value) { | |
var def = $q.defer(); | |
var childRef = ref; | |
if( key ) { childRef = childRef.child(key); } | |
// there's some additional complexity to getting these promises | |
// to work with the compiler; we'll skip those for this example | |
childRef.remove(function(err) { if( err ) { def.reject(err); } else { def.resolve(childRef) } ); | |
return def.promise; | |
}; | |
$scope.list.$update = function(key, value) { | |
var def = $q.defer(); | |
var childRef = ref; | |
if( key ) { childRef = childRef.child(key); } | |
// there's some additional complexity to getting these promises | |
// to work with the compiler; we'll skip those for this example | |
childRef.set(value, function(err) { if( err ) { def.reject(err); } else { def.resolve(childRef) } ); | |
return def.promise; | |
}; | |
$scope.list.$add = function(data) { | |
var def = $q.defer(); | |
var childRef = ref.push(); | |
childRef.set(value, function(err) { if( err ) { def.reject(err); } else { def.resolve(childRef) } ); | |
return def.promise; | |
}; | |
function updateData(snap) { | |
$timeout(function() { | |
var key = snap.name(); | |
var dat = snap.val(); | |
if( !$scope.list.hasOwnProperty(key) ) { | |
$scope.list[key] = { $id: key }; | |
} | |
if( !angular.isObject(dat) ) { dat = { '$value': dat }; } | |
angular.forEach($scope.list, function(v,k) { | |
if( !dat.hasOwnProperty(k) && !k.match(/^$/) ) { | |
delete $scope.list[k]; | |
} | |
}); | |
angular.extend($scope.list[k], dat); | |
$scope.list[key].$priority = snap.getPriority(); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment