npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grail

v0.0.13

Published

simple React based isomorphic apps framework

Downloads

8

Readme

Grail - simple React based isomorphic apps framework

With Grail you will need to write frontend code only and you can use ANY backend you like. Grail plays nice with any API - java, python or ruby.

##install

npm install grail --save

##getting started

Grail architecture is really simple. We use react router to handle isomorphic routing and rendering components. Additionally router triggers actions PromisePipe that fill stores for rendered components.

The very basic example is here

Lets start with general app. The app will consist of two routes Home (/) and Items (/items)

We will use react router thus we have

var Router = require('react-router');
var Route = Router.Route;

var routes = <Route path="/" name="home" handler={AppComp} >
    <Route name="items" path="items" handler = {ItemsComp} action={getItems} stores={ItemsStore}/>
  </Route>

We need to build up AppComp and ItemsComp.

var React = require('react');
var grail = require('grail');
var RouteHandler = Router.RouteHandler;
var Link = Router.Link;

var AppComp = React.createClass({
  render: function () {
    return (
      <div className="container">
      	<Link to="home" >Home</Link> - <Link to="items">Show Items</Link>
        <RouteHandler/>
      </div>
    );
  }
});

//render Items component
var ItemsComp = React.createClass({
    mixins: [grail.ContextMixin],
    getInitialState: function(){
    	//get initial data from ItemsStore
      return {
        items: this.context.stores.ItemsStore.get()
      }
    },
    componentDidMount: function() {
    	//listen to ItemsStore changes
      this.context.stores.ItemsStore.on('change', this.change);
    },
    change: function(items){
    	//set state only if mounted
      if(!this.isMounted()) return;
      this.setState({items: items});
    },
  	render: function () {
	    return (
      <ul className="items">
        {this.state.items && this.state.items.map(function(item){
          return <div>{item}</div>
        })}
      </ul>
    );
  }
});

To render Components though we need a ItemsStore to contain Items.

var ItemsStore = grail.BasicStoreFactory('ItemsStore', {
  items: null,
  init: function(context){
    context.actions.on('got:items', this.gotItems.bind(this));
  },
  gotItems: function(items){
    this.items = items;
    this.emit('change', this.items);
  },
  get: function(){
    return this.items;
  }
});

And an action to fill the Store. As Actions we are using PromisePipe

var PromisePipe = require('promise-pipe');

PromisePipe.use('emit', function emit(data, context, eventName){
	context.emit(eventName, data);
	return data;
});


//The Action for /items path. Is taking data from async source data
var getItems = PromisePipe().then(function(data){
	return new Promise(function(resolve, reject){
		setTimeout(function(){
			resolve([1,2,3,4,5,6,7,8,9,10])
		}, 100);
	})
	
}).then(function emit(data, context){
	context.emit('got:items', data);
	return data;
});

And now we need to create and init app with routes:

var app = grail.createApp();
app.useRoutes(routes);

module.exports = app.init();

This will be a client.js and we are exporting an object with middleware property that we can use in our server.

The server will look like:

var express = require('express');
var app = express();

require('node-jsx').install({harmony: true, extension: '.js'})

app.use(express.static(__dirname + '/dist'));
var clientAppMiddleware = require("./client").middleware;
app.use(clientAppMiddleware);

app.listen(3000);
console.log("Server is on 3000 port");