Skip to content

Instantly share code, notes, and snippets.

@davej
Created May 5, 2014 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davej/6d5abc80d6098362749e to your computer and use it in GitHub Desktop.
Save davej/6d5abc80d6098362749e to your computer and use it in GitHub Desktop.
###
# To create a plugin you need to include the `classy-core` module and then pass
# your plugin object into the `class.plugin.controller function`
###
angular.module('classy-yourPlugin', ['classy-core']).classy.plugin.controller
name: 'yourPlugin'
###
# Dependencies placed in the localInject array will be available on `@` (`this`)
# in the plugins init and postInit methods.
# They will not be visible/injected to the controller
###
localInject: ['$timeout']
###
# Configurable options are placed here.
# Classy will check if the user has over-rided any options and make them available
# on `@` (`this`)
###
options:
enabled: true
foo: 'bar'
noOfSecondsToWait: 5
preInit: (classConstructor, classObj, module) ->
###
# * `classConstructor` is the Classy Controller constructor function, this
# is the function that will be initialised by ngController, it is
# available in the init and postInit functions using `klass.constructor`
# * `classObj` is the raw object that is passed into the app.classy.controller()
# * `module` is a reference to the app module
###
console.log(classConstructor.name, classObj.name, @name)
### -> 'classyController', 'TodoCtrl', 'yourPlugin' ###
###
# `@` or (`this`) provides access to local functions, it also provides access
# to the compiled options object (i.e. it will include any setting overrides
# that the user does).
###
@doSomething(@options.foo)
###
# Dependencies from the `localInject` array are not available on `@` (`this`)
# in `preInit` but they are available in `init` and `postInit`
###
console.log(@$timeout)
### -> undefined ###
doSomething: (input) ->
if input is 'bar'
alert('Hello World!')
init: (klass, deps, module) ->
###
# This function will be called after the class has been initialised by Angular
# but before the controller's `init` (klass.init) method is called.
#
# * `klass` is the controller class and all of it's properties, you can also
# access the dependencies from here but it's best to use `deps` instead
# because if named dependencies are used then they may have an unexpected name
# * `deps` is an object map containing all of the controller's dependencies
# * `module` is a reference to the app module
###
###
# If you want to check that the controller has injected the `$scope` then do
# something like the following:
###
if !deps.$scope
throw new Error "You need to inject `$scope` to use this plugin"
###
# Return values are ignored in init *except* when it's a promise.
#
# If a promise is returned then Classy will wait for it to be resolved before
# the klass.init function is called.
###
return waitNoOfSeconds(@options.noOfSecondsToWait)
waitNoOfSeconds: (noOfSeconds) ->
noOfMs = noOfSeconds * 1000
###
# @$timeout is available because it was defined in the `localInject` Array
# and this function was called during the `init` phase
###
return @$timeout ->
true
, noOfMs
postInit: (klass, deps, module) ->
###
# This function is exectuted immediately after klass.init()
#
# Params are the same as @init, return value is ignored.
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment