Giter Site home page Giter Site logo

class-to-angular's Introduction

ClassToAngular

ClassToAngular is a an experimental tool to use CoffeeScript classes to create AngularJS directives, controllers and that kind of things.

Instead of doing:

angular.module('ClassApp', []).directive 'bananaSplit', ($timeout)->
  restrict: 'E'
  link: (scope, element, attrs)->
    scope.isApple = 'Is not apple'
    scope.isBanana = 'Is banana'
    scope.isOrange = 'Is orange'
    scope.callTheBananaKing = ->
      $timeout ->
        console.log 'Calling the banana king!'
        console.log 'Because: ', scope.isBanana
      , 1000

It may look fine for now, but start to make a bigger app and your link function gets cluttered like hell. Besides, it doesn't look Coffeescripty at all! Instead you can do:

class BananaSplitDirective
  @restrict: 'E'
  
  @inject: ['$timeout']
  
  # This gets called at the end of the new link function
  @link: (scope, element, attrs)->
    scope.isApple = 'Is not apple'
    
  # This gets assigned to the `scope`
  isBanana: "Yes, it's banana"

  # This replaces the link function
  # But instead of `scope` we can use @/this
  constructor: (element, attrs)->
    @isOrange = "No, it's not orange"
    
  # This also gets assigned to the scope.
  callTheBananaKing: ->
    @$timeout =>
      console.log 'Calling the banana king!'
      console.log 'Because: ', @isBanana
    , 1000

angular.module('ClassApp', []).directive classToDirective(BananaSplitDirective)...

Does it have a big performance impact? I don't think so! But I haven't tested yet, so don't take my word for granted!

The thing is, it shouldn't, since most of the extra processing is made on the startup of the application! If someone has a better and faster idea, please say so!

Live example

Want to see an example? Head to the example folder. Want to see it live. Head to this link

ToDo

  • Make a script for controllers and the other stuff.
  • Make it work like angular.module('ClassApp', []).classDirective BananaSplitDirective which is nicer.
  • Make a bower package
  • Tests

class-to-angular's People

Contributors

zequez avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.