Skip to content

Instantly share code, notes, and snippets.

@davemo
Last active December 16, 2015 07:48
Show Gist options
  • Save davemo/5401166 to your computer and use it in GitHub Desktop.
Save davemo/5401166 to your computer and use it in GitHub Desktop.
An example of routes that depend on pre-fetched data prior to rendering, the template, controllers that handle form submission, and relevant service abstraction in AngularJS.
app.config ($routeProvider) ->
$routeProvider.when '/settings',
templateUrl: 'settings.html'
controller: 'SettingsController'
resolve:
settings: (SettingsService) -> SettingsService.get()
<form name="settingsForm" ng-submit="saveSettings()">
<label>Email</label>
<input ng-model="settings.email" type="email" required/>
<button type="submit">Save Settings</button>
</form>
app.controller 'SettingsController', ($scope, settings, SettingsService) ->
$scope.settings = settings.data
$scope.saveSettings = ->
SettingsService.put($scope.settings)
app.factory 'SettingsService', ($http) ->
url = "/settings"
get: -> $http.get(url)
put: (settings) -> $http.put(url, settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment