GithubHelp home page GithubHelp logo

cornerjs's Introduction

CornerJS

Easy to use directives engine, that gives you a super-ability just to put into the far corner stuff you want to forget about:

  • social buttons
  • animations
  • intervals and timeouts
  • on-element-appear actions
  • custom scrolls

It's totally not about data-binding and models. It's about throwing away all the pieces of code that are not connected with anything else and depends only on their appearance in DOM tree.

It works in IE9+, latest FF, Safari, Chrome, Opera, Android 4+(no information about 3.0, not working in 2.3) etc. Feel free to report about browser support, especially I'll appreciate information about iOS support. IE and some other browsers are working over polyfill by Polymer brothers.

How it works?

directive('foo', function(){alert('bar')});

Aaaand yup. That's it. You can now append

<div class='foo'>

or

<div foo>

or

<div data-foo>

or even

<div directive-foo="baz">

and even an amazing

<foo bar="baz"/>

and you'll get access to all the properties

Syntax

expanded syntax:

directive('directive-name', {
    load: function(node, attribute){},
    unload: function(node, attribute){},
    alter: function(node, attribute){},
})

short syntax:

directive('directive-name', function(node, attribute){});

is equal to

directive('directive-name', {
        load: function(node, attribute){}
    })

Load is called on event appearance in DOM tree, unload - on removal, alter - on attribute change. 'template_url' has priority over 'template', overwrite is condition if content should be replaced.

if you initialise only {alter: } function, it automatically gets bind to load also. This is done to implement easily all the actions to be done on both load and alter, such as include directive

Local scope contains following by default: { directive: {<...original directive content; you can alter methods on-the-fly, but actually only alter and unload changes will be reasonable...>}, node: original node }

You can extend it easily to use for your own purposes:

directive('directive-name', { load: function(node, attribute){ this.interval = setInterval(function(){console.log('hi!')}, 100) }, unload: function(node, attribute){ clearInterval(this.interval) } })

node will contain also some new properties: each one for directive named in the same way(local scope will be there, so you will be able to access easily to scope properties, e.g. carousel control directive will be able to access carousel container directive), also 'directives' and 'directive_aliases'. You should ignore them, they are mostly for technical purposes.

Yes, it's not good to use global scope elements, but all other solutions I've saw were with dramatical memory usage or high CPU load, so for now it's the only way.

Attributes usage

It has some magic also.

<div directive-foo="baz">

returns as attribute

'baz'

But if you'll input

<div directive-foo="baz: 'baz'">

it will bring you

 {baz: 'baz'}

It is based on window.eval, so you are able to do every piece of code possible. First it alledge that curvy braces are omitted, then if eval fails forgets about it and attempts a second try with direct content eval(). If even this fails original string is passed.

Mechanics details

attribute directive has priority over the class directive. If you have

...well, you dig that pit for yourself by your own hands. Actually you can do it as logic is bright and clear:

  • first it passes through class list
  • next it passes through attributes list

pass order inside array is also simple: first goes non-prefixed value, then all the prefixes value. I strongly recommend to leave config in the beginning as-is or at least leave "data" prefix at the beginning.

jQuery

If you are using jQuery just use $('#selector')[0] to get access to original node

##Current bugs IE 9 and 10 do not support attribute removal from the node. It is allegedly connected with mutationobserver polyfill, and is uncommon situation(usually it is done by hands rather by manipulating DOM), so it should be just taken in mind that it is recommended to remove node, not attribute directive. Otherwise you can use class and tag directives.

Plans for future

  • Speed tests

cornerjs's People

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.