Skip to content

Instantly share code, notes, and snippets.

@nelsonpecora
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nelsonpecora/8760d9133a0596de705c to your computer and use it in GitHub Desktop.
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.
// 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: 'myfriend@gmail.com'
};
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