Last active
August 29, 2015 14:01
-
-
Save nelsonpecora/8760d9133a0596de705c to your computer and use it in GitHub Desktop.
New 100% promise-driven data models. Here's a sample of how the controller changes.
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
// in the old data model, you had to do a bunch of crufty bullshit | |
$scope.messages = Messages.all(); | |
$scope.$onRootScope('messages.updated', function() { | |
$scope.messages = Messages.all(); | |
}); | |
// ew, what's even going on there? Well, it loads the data from the Messages service, | |
// but then has to wait for an event to make sure it actually updates when the data changes. | |
// Like a peasant. | |
// Here's the new syntax: | |
Messages.all().then(function(res) { | |
$scope.messages = res; | |
}); | |
// it asks the Messages service to give it all records, and if they don't exist | |
// it will fetch new records from the server. It then returns a promise that will | |
// always update when the records change. | |
// Oh, you want to save some data? Make a new message? Here: | |
$scope.newMessage = { | |
message: 'This is a new message', | |
email: '[email protected]' | |
}; | |
Messages.save($scope.newMessage); | |
// BAM. It's that easy. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment