|
# Here's an example Controller. In Angular, Controllers and Controller/View hybrids in MVC-speak |
|
# Every property of $scope is available as a variable in the View, including the functions |
|
# The init syntax is similar to factory, except here we're depending on the Team model from team.coffee |
|
@dispatched.controller('Teams', ['Team', '$scope', '$rootScope', (Team, $scope, $rootScope) -> |
|
# initialize the scope |
|
$scope.page = 1 |
|
$scope.teams = [] |
|
$scope.hasMore = true |
|
$scope.loadingMore = false |
|
|
|
# this gets called by our inifinite scrolling library |
|
$scope.loadMore = -> |
|
return true if ! $scope.hasMore || $scope.loadingMore |
|
$scope.loadingMore = true |
|
# NProgress is a library that shows the loading bar thing at the top of the page |
|
# It's loaded as an external JS thing available globally (not related to angular) |
|
NProgress.start() |
|
# Team is from team.coffee. Query is a native function to RailsResource. Just calls index. |
|
Team.query({page: $scope.page}).then (teams) -># it returns a promise . teams will be an array from the API |
|
# I read somewhere this is a faster way to merge arrays in JS. Dunno. |
|
for team in teams |
|
$scope.teams.push team |
|
# normal loading loop stuff |
|
$scope.page++ |
|
$scope.hasMore = teams.length >= 25 |
|
$scope.loadingMore = false |
|
NProgress.done() |
|
|
|
# this gets called from the HTML view |
|
# the HTML renderer already fetches page 1, so we load it here via JSON from the DOM |
|
$scope.init = (teams) -> |
|
for team in teams |
|
$scope.teams.push new Team(team) |
|
$scope.page++ |
|
$scope.hasMore = teams.length >= 25 |
|
# We use the Observer Pattern here to receive notes from other controllers |
|
# There's another controller that presents a form to edit Teams that uses these |
|
$rootScope.$on 'Teams.addedTeam', (event, team) -> |
|
$scope.teams.push team |
|
# these two are used by another controller not in this gist |
|
$rootScope.$on 'Teams.addedMember', (event, team, member) -> |
|
console.log 'addedMember', team, member |
|
team.members.push member |
|
$rootScope.$on 'Teams.removedMember', (event, team, member) -> |
|
console.log 'removedMember', team, member |
|
team.members.splice _.indexOf(member), 1 |
|
|
|
# functions on $scope are available in the View |
|
$scope.newTeam = () -> |
|
team = new Team() |
|
# this is observed by that other controller |
|
$rootScope.$emit 'Teams.presentForm', team |
|
true |
|
|
|
$scope.editTeam = (team) -> |
|
$rootScope.$emit 'Teams.presentForm', team |
|
true |
|
|
|
$scope.deleteTeam = (team, $index) -> |
|
return false unless team.id? |
|
NProgress.start() |
|
team.delete().then -># wait for railsResource to finish, then update the UI |
|
$scope.teams.splice $index, 1 |
|
NProgress.done() |
|
true |
|
|
|
]) |
1-team.coffee mentions a _init_app.coffee . Is that where you're setting the @dispatched variable?