Skip to content

Instantly share code, notes, and snippets.

@zshannon
Last active August 29, 2015 14:06
Show Gist options
  • Save zshannon/9ae367d203bd71c3660a to your computer and use it in GitHub Desktop.
Save zshannon/9ae367d203bd71c3660a to your computer and use it in GitHub Desktop.
AngularJS Tips
Angular apps are little separate kingdoms. You can't get into them from outside, and it's really frowned upon to use jQuery within them to access the DOM.
The loading order is somewhat important. I set it up like this: [jQuery, jQueryUI, Bootstrap, Underscore, Angular Library, *any Angular Plugins*, Angular Application init file, *rest of angular files*].
# Factory is angular for Model in MVC-speak
# @dispatched gets the application we setup in _init_app.coffee; 'Team' is the name of this model;
# the array param has the first n elements as the dependencies that become params for the last element,
# which defines and returns the model
@dispatched.factory 'Team', ['railsResourceFactory', 'railsSerializer', (railsResourceFactory, railsSerializer) ->
# initialize the object
Team = railsResourceFactory # using RailsResource for now, but plan to switch to [Angular-Data](http://angular-data.pseudobry.com/)
url: (context) -> # this gets called to generate every request URL
# if we have an instance, use its ID for Show/Update/Delete/etc.
if context[@idAttribute]?
"/teams/#{context[@idAttribute]}.json"
# otherwise we're working on the collection for Index/Create/Search/etc.
else
"/teams.json#{window.location.search}" # window.location.search is the query params like `?page=3&filter=pasta`
# team is the root node for JSON serialization from the API
# like: {'team':{name:'new orleans saints'}}
name: 'team'
extensions: ['snapshots'] # snapshots let us do undo
# I can add functions to the model here, too
# for example:
Team.prototype.helloName = (name) ->
"Hello, #{name}!"
Team
]
@montasaurus
Copy link

1-team.coffee mentions a _init_app.coffee . Is that where you're setting the @dispatched variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment