Giter Club home page Giter Club logo

clazzy's Introduction

Clazzy

In short

This example uses Clazzy and shows how to declare classes with inheritance and an interface, and will then call some functions on the newly created object.

a little longer The code below defines a module that needs three resources, the Clazzy function, some ~BaseClass that we want to inherit from and some other class that we want to use as Mixin. The module uses the Clazzy to declare a new Class called ~MyClass, including namespace, inheritance, 2 interfaces and our mixin.

define [
  "clazzy/Clazzy" 
  "some/path/MyBaseClass" 
  "some/path/MyMixin" 
], (Class, MyMixin) ->
  Class "some.other.path.MyClass", MyBaseClass, ["someInterface1", "someInterface2", MyMixin], 
    constructor: () ->
      @.myProperty1 = 1
      @.myProperty2 = 2
      @.myProperty3 = 3
    myFunc1: () ->
      # awesomeness goes here
    myFunc2: () ->
      # awesomeness goes here

Now we can instanciate it and try some of our neat functionality.

myObject = new MyClass()
myObject._fullname # -> ["some.other.path.MyClass", "some.path.MyBaseClass", "BaseClass"]
myObject.declaredClass # -> "some.other.path.MyClass"
myObject.toString() # -> "some.other.path.MyClass"
myObject._implements # ->  "someInterface1", "someInterface2", "some.path.MyMixin"
myObject.is "someInterface2" # -> true
myObject.isnt "a banana" # -> true
myObject.set "myProperty1", 42 # -> property is set
myObject.set "nonExistingProperty", 666 # -> throws Error
myObject.get "myProperty1" # -> returns 42
myObject.get "nonExistingProperty" # -> throws Error
myObject.myFunc1() # -> awesomeness
myObject.watch "myProperty1", (prop, oldValue, newValue) ->
  console.log "Property " + prop + " went from " + oldValue + " to " + newValue
myObject.validate "myProperty1", (prop, oldValue, newValue)-> 
  valid = newValue is "FOO" or newValue is "BAR"
  console.log "Invalid value" if not valid
  return valid
myObject.set "myProperty1", "FOO" # -> logs our message sets the property value
myObject.set "myProperty1", "Bad value" # -> logs "Invalid Value" and does not set the property value

Sweet! But why the setters and getters? Well they let us jack in and observe when the function is called and what was set or get using e.g. dojos aspect module since it is now a function and not a javascript object property.

IoC

Using the IoC module only requiers you to after required the module simply call the get(Interfacename, optionalPropertyObject) function. You also need the mappings to be registered. See the Registrar.

require [
    "clazzy/IoC"
    "clazzy/config/Registrar"
], (IoC, Registrar) ->
    IoC.get("IMyRegisteredInterface", {SomeProperty: "value", SomeOtherProperty: "stuff"})

The IoC module knows the dependencies of a class by reading its __dependencies property if available. this property should be an array containing the names of the interfaces to inject for. The injected instance will be a property on the object with the same name as the interface. So for ´´´ __dependencies: ["IMyFoo"] ´´´ the object will have the property this.IMyFoo This means that if you want more than one instance of a class injected, you have to make a factory.

clazzy's People

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.