scotch / angular-brunch-seed

AngularJS + Brunch
Other
228 stars 78 forks source link

restructure controllers file to be DRYer #5

Closed jonahkagan closed 11 years ago

jonahkagan commented 11 years ago

Right now we have something like:

mod.MyCtrl1 = [                                                                                                                             
  '$scope'                                                                                                                                  

(s) ->                                                                                                                                      
  s.Title = "MyCtrl1"                                                                                                                       
] 

If we want to associate a controller with a route, we want to write something like:

$routeProvider
  .when('/view1',
    templateUrl: '/partials/app/partial1.html'
    controller: 'MyCtrl1'
  )

When I tried it, I couldn't get it to work unless I set the name attribute on the scope of the controller (as opposed to Title. With a little restructuring, however, you only need to name the controller once. It looks like this:


angular.module('app.controllers', [])

.controller('MyCtrl1', [
  '$scope'

(s) ->
  s.Title = "MyCtrl1"
])

Let me know what you think of this change and I'll submit a pull request.

kylefinley commented 11 years ago

I wasn't aware that specifying the controller in routes didn't work with is technique. The more traditional syntax, probably will be less confusing as well, at the cost of added typing.

This does bring up another question. How do you feel about having all of the controllers in one file?

scripts /
  controllers.coffee

vs

scripts /
   controllers /
      user.coffee
jonahkagan commented 11 years ago

I'm just starting to hit this problem as my controllers file gets larger. However, I've decided to leave them all in one file as motivation to factor out more functionality into services.

(big generalization warning) I think anybody who gets to the point where they'll want their controllers in different files will have a good enough understanding of things by that point to do it themselves.

Jonah

On Sun, Aug 19, 2012 at 12:53 PM, Kyle Finley notifications@github.comwrote:

I wasn't aware that specifying the controller in routes didn't work with is technique. The more traditional syntax, probably will be less confusing as well, at the cost of added typing.

This does bring up another question. How do you feel about having all of the controllers in one file?

scripts / controllers.coffee

vs

scripts / controllers / user.coffee

— Reply to this email directly or view it on GitHubhttps://github.com/scotch/angular-brunch-seed/issues/5#issuecomment-7854661.

kylefinley commented 11 years ago

Your probably right.

Since we're removing the mod = {} from controllers.coffee I think it should be removed from the services.coffee and directives.coffee, too in the name of consistency. If you have alright started on this, please do send a pull request, otherwise I can make the change.

kylefinley commented 11 years ago

Oh, and if you do send a pull request please add your name to the CONTRIBUTORS.md and AUTHORS.md files.

kylefinley commented 11 years ago

Fixed with 631317113978d4fa0caa704031102d4841ac81ec @jonahkagan thank you.