Coder Social home page Coder Social logo

dancejs's Introduction

DanceJS

DanceJS ๐Ÿ’ƒ is alternative pattern to handle flow of function, by main concept is what happen next. This pattern also aim to make your JS code is easy to read and organize your mind for coding.

How to install

include dance.js in your code

<script src="dance.js">

then let rooooll!!! ๐ŸŽค ๐ŸŽถ

How to use

var dance = new Dance

// Define notation name "Gangnam_style"
// and set step "Cross_hands", "Raise_left_leg", "Raise_right_leg" and "End"

dance.step('Gangnam_style', {

    'Cross_hands': (next) => {
        ...
        
        next('Raise_left_leg')
    },

    'Raise_left_leg': (next, data) => {
        ...

        next('Raise_right_leg', {
            foo: bar
        })
    },

    'Raise_right_leg': (next, data) => {
        ...

        next('End')
    },

    'End': (next) => {
        next('Cross_hands')
    }
})

// Dont forgot to call .letRoll
// First step that defined will run at first, in this case "Cross_hands" will run.

dance.letRoll('Gangnam_style')

// IF we need to specify which step will run first jusf add second parameter of letRoll()

dance.letRoll('Gangnam_style', 'Raise_left_leg')

Example

Making function to load user and process user data

dance.step('Get_user', {

    'Fetch_from_API': (next) => {

        request.get('http://user.api/users')
            .then(res => {
                next('Filter', res.users)
            })
            .catch(() => {
                next('Display_not_found')
            })
    },

    'Filter': (next, data) => {
        
        let userWhoIsActive = data.filter(user => {
            return user.is_active
        })

        next('Display_to_list', userWhoIsActive)
    },

    'Display_to_list': (next, data) => {

        document.getElementById('ul').innerHTML = data.map(user => `<li>${user.name}</li>`)
        
        next('Push_notify_when_success')
    },

    'Display_not_found': () => {

        document.getElementById('output').innerText = 'Sorry, user not found'
    }
})

// We can push extra step by define same notation name,
// then new step will push to previous defined notation

dance.step('Get_user', {
    'Push_notify_when_success': () => {
        alert('Done')
    }
})

// Dont forgot to call .letRoll

dance.letRoll('Get_user')

see more example, at /demo

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.