Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here is my solution:

MyFactory = ->
  test: -> "test"

class Base
  @$inject: ["MyFactory"]
  constructor: -> return (@MyFactory) => @
  restrict: "E"
  link: (scope, element, attr) =>
    scope.test = @test()

class MyDirective extends Base
  test: -> @MyFactory.test()


angular.module("app", []).factory("MyFactory", MyFactory).directive("myDirective", new MyDirective)

I do feel like it's a little twisted, but could not find anything better, has anyone a better idea?

Sample: http://jsbin.com/eMuPoWU/1/edit?html,js,output

share|improve this question
    
Would you mind posting as javascript? –  Michael Calkins Jan 4 '14 at 1:25

1 Answer 1

up vote 3 down vote accepted
angular.module('auth-service', [
    'base-service'
])

.factory 'AuthService', (BaseService, Restangular) ->

    class AuthService extends BaseService

        constructor: () ->
            super()

        restBase: ->
            Restangular.all('auth')

        singleBase: (options) ->
            Restangular.one('auth', options.entity.id)

        logout: (options)->
            @putSingle(options)

    new AuthService()

This is an example of a way to do a singleton. This is a great gist https://gist.github.com/ProLoser/5645860

share|improve this answer
    
I like that. One suggestion - new AuthService() is easy to forget/miss at the very bottom of a long class declaration. Instead, you could just omit that line and declare the class with new class AuthService extends BaseService. –  Bungle Jan 19 at 3:35
    
Thanks. And you are right. I actually saw that recently for the first time and thought it was a good idea. But after changing a class to put new at the top i didn't like it. Felt weird with class not being first. Just a preference thing though, if i had started off like that it would probably be normal. –  j_walker_dev Jan 19 at 7:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.