Giter VIP home page Giter VIP logo

pbjs's Introduction

pbjs

About

pbjs is small and lightweight object orientated framework (Production size: ~10kb).

Compared to other DOM based alternatives, like jQuery, Zepto or QuoJS, it has a focus on cross-browser/device compatibility and performance. Through the modular design it can be extended with various plugins.

Concept

The usage of pbjs is quite easy, especially if you are familiar with other frameworks. Every method mentioned in the docs is a part of the global 'PB'-object. Some of the methods needs to be used on a wrapped element, but most general tasks could be used solely.

Since a reference to itself will always be returned - fluent programming via method chaining is possible.

In contrast to most AJAX libraries, pbjs was created for developing interactive web applications instead of effects or widgets.

pbjs focuses on efficient coding to improve the performance.

Following this principle it forces the developer to use best practice solutions in JavaScript and an appropriate coding style.

A few ways to achieve this:

  • usage of native functions instead of custom regex matches: the general element selector will be an element ID
  • caring about bottlenecks, especially the DOM: everything will be cached internaly to simplify the re-usage
  • dont repeat yourself (DRY): using clear function names to avoid multiple declarations for the same purpose
  • the future right now: instead of custom calls for general tasks, ES5 polyfills are included
  • supporting OOP approaches: ease the paradigm through the included class construct & inheritance
  • don't wait for it: the framework is defined in the CommonJS way for async module loading
  • understand what you do: find a method in the modular structured source

Of course it works quite well with common design patterns.

Usage

By default elements used by the pbjs will be cached. A garbage collector will free the memory when they are removed from the DOM trough emptying the html. To compensate the cost of rarely used object it is neccessary to create new ones or keep them alive (see PB.Request).

Wrapper (Selector & Create)

The 'PB'-object can use input for different purposes:

  1. Element ID - selector

     // Retrieve single element    
     PB('element_id')
    
  2. DOM node selector (regular & cached)

     // Select single element
     PB(document.body)
    
  3. creating elements

     // Create an element     
     PB('<h1>Peanut Butter and Jelly Sandwich!</h1>').appendTo( [Object PBDom] )
    

Request

To do a HTTP-Request, a new Request-Object has to be created and configured with data.

// Create & configure a new request object
var req = new PB.Request({

    url: '42.nl',
    data:{ 
        foo: 'bar'
    },
    method: 'POST'        
});

Afterwards a callback can be declared, which should be triggered on the result of the sending.

// Attach listener & callback
req.on('success', function ( res ) {
    
    // represents the response as an object
    console.log( res.responseJSON );
    
}).send();

Collections

Collections in pbjs are used to select a set of elements. Methods which are supposed to output multiple values returns a collection.

// Store collection, crawling is heavy :)
var collection = PB('element_id').find('div.peanut);

Beside normal Array methods, invokations can be used for mutations as well.

// Invoke collection with PB.Dom method
collection.invoke('setStyle', { color: 'brown', backgroundColor: 'purple });

Style

There are two ways in pbjs to support CSS - static and transitional. Both work prefix free, allowing to focus on the input rather than cross browser issues.

Note that css properties are given in camel case

  1. setStyle / getStyle

     // Receive the current style
     PB('element_id').getStyle('opacity');
     
     //  Adding style properties (including CSS3)
     PB('element_id').setStyle('transform', 'rotate(30deg)');
    
  2. morph (Transitions)

     // Change the style over a time period
     login.morph({                     
        opacity: current/2        
     }, 1.2 /* 1.2 seconds */, function( element ){        
        element.remove();
     });
    

Classes

Classes are usefull for creating complex structures and maintaining large applications. Through the abstraction layer of a class - it's very easy to create new objects which inherits from another one.

// Create an 'Animal' class
var Animal = PB.Class({
    
    // Class constructor
    construct: function () {
        
        console.log( 'I am an animal' );
    }        
});

// Create an 'Ape' class, which extends the the former 'Animal'
var Ape = PB.Class( Animal, {
    
    construct: function () {            
               
        console.log( 'I am an Ape' );
    }        
});
    
new Ape; 
    
// Result
> I'am an Ape

It's not just possible to access the inherited properties of the parent - but also get the result of an executed function directly.

var Goat = PB.Class( Animal, {
    
    construct: function () {            
        
        this.parent();
        console.log( 'I am a Goat' );
    }        
});       

new Goat;

// Result
> I'am an Animal
> I'am a Goat

CSS Selector

The principle of efficient coding doesn't exclude DOM crawling with CSS selectors - it just enforces the developer to re-think about his workflow. In cases where you need use classes (prefix 'js-' !), you first select a parent element and then use the method '.find('.className')'. As a result it returns a collection of 'PB(elements)'.

// Using CSS selector
var entries = PB(document).find('.js-entries');

Compatibility

  • IE7+
  • Firefox 3+
  • Safari 4+
  • Opera
  • Chrome
  • Mobile (Tested with Android, iPhone, Nokia, BlackBerry Tablet OS)

License

This project is under the MIT License.

Powered by Pluxbox

Copyright 2011-2012, Niek Saarberg

pbjs's People

Contributors

saartje87 avatar pbjs avatar jonkoops avatar thecas avatar dennislaupman avatar autarc avatar

Stargazers

 avatar robindrost avatar Bram Hammer avatar Rick van 't Wout Hofland avatar RVD avatar  avatar Allen Taylor avatar  avatar

Watchers

 avatar robindrost avatar James Cloos avatar  avatar Rick van 't Wout Hofland avatar  avatar Job Schipper avatar Youri Westerman avatar  avatar

Forkers

rvddesign

pbjs's Issues

Custom events

Like element.on('myCustomEvent', function(){});

element.emit('myCustomEvent');

PB.type( var )

Should return the correct data type, like array, object, string, date, etc..

PB.type( var ) === 'string'

IE9, Event.prototype bug

Event.prototype.stop = function () {};

el.on('click', function ( e ) {
    e.stop(); // Fails on IE9 (in some cases, not yet sure that, mootools conflict?)
});

Replace

Maybe its logical that we turn PB('

blep
').replace(element); into PB(element).replace('
blep
'); ? its just a suggestion ;-)

'once()' - binding scope

There could be a wrong scope selection at the "once" event binding.

 $('playlist-start-time').once('keyup', function(e){               
      time = this.value;            
 });

Doesn't work, but "time = e.currentTarget.value" works.

.body.querySelectorAll

pbjs : 1474
if( body.querySelectorAll ) {

Unable to find multiple links with class in rb2 rss wiki.

Temp fix:
if( document.body.querySelectorAll ) {

Cleanup/add PB.browser/PB.supports

First of all, we need to resee the current code/object naming :)

PB.browser = {

    isIE: boolean,
    isChrome: boolean,
    version: float
}

PB.supports = {

    touch: boolean,
    multitouch: boolean,
}

morph() in morph() bug

// show popup
overlay.setStyle({overlay.setStyle( { 

    opacity: 0 
}).show().morph({ opacity:0.4} ).show().morph( { 

    opacity: 0.4 
}, function()function () {

    // show content
    content.setStyle({content.setStyle( {
        opacity     : 0,
        zIndex      : 100,
        position    : 'relative'
    }).show();} ).show();

    content.morph({content.morph( { 
        opacity: 1 
    }, function()function () {


        // do align
        if(doAlign)if( doAlign ) {

            this.alignPopup(content);
        }

        // callback
        if( callback ) {

            callback();                 
        }
    }.bind(this), 0.3);0.3 );
}.bind(this), 0.2);0.2 );

Feature: Multiple "on"-actions at once

Multiple "on"-actions at once by using a space or pipeline for dividing them. I'd guess underneath example would explain enough! ;)

This bit of code:

element.on('click', showmenu);
element.on('mouseenter', showmenu);
element.on('mouseleave', hidemenu);

Should look like:

element.on('click mouseenter', showmenu);
element.on('mouseleave', hidemenu);

Smaller code base

For example we could do the jquery trick with width/innerWidth/ouserWidth/scrollWidth

['Height', 'Width'].forEach(function () {

    PB.overwrite(Dom.prototype, {});
});

PB.Class

Todo:

  • Optimization
  • Make first argument 'parent/super' class if 2 arguments
  • Auto use parent constructor if no constructor is set
  • Properly gonna rename constructor to construct

Should we remove hooks from Dom.getStyle ??

This are currently the hooks

var hooks = {

    'border': 'borderLeftWidth borderLeftStyle borderLeftColor',
    'borderColor': 'borderLeftColor',
    'borderWidth': 'borderLeftWidth',
    'borderStyle': 'borderLeftStyle',
    'padding': 'paddingTop paddingRight paddingBottom paddingLeft',
    'margin': 'marginTop marginRight marginBottom marginLeft',
    'borderRadius': 'borderRadiusTopleft'
}

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.