0.18.7 • Published 9 years ago

shimjs v0.18.7

Weekly downloads
5
License
ISC
Repository
github
Last release
9 years ago

ShimJS

the two similar api calls in the render chain should reuse data rather than running the api call again

module.exports = function (html, render, query, cb) {
	var user_get_params = {
		token:              query.token,
		requesting_user_id: query.user_id,
		user_id:            query.user_id
	};
	var chain = [['collection', 'navbar', 'user/get', user_get_params],
		         ['collection', 'title'],
		         ['collection', 'insert'],
		         ['collection', 'entry', 'collection/entry/get', {}],
		         ['collection', 'entry', 'collection/entry/get', {}]];
	render(html, chain, function (err, html) {
		if (err) return cb(err);
		return cb(null, html);
	});
};

ShimJS is an HTML/API templating system similar to AngularJS and ReactJS.

There are two major concerns with building a complicated website:

  1. Loading a page that has been prerendered with all appropriate data from the server.

  2. Updating a loaded with page with new data via pagination (next 10 results), addition (add an entry to a list), removal (remove an entry from a list), or update (following and unfollowing another user).

Project Setup

A ShimJS project is made up of two distinct parts that are shimmed together:

  1. The API folder which contains a directory tree representing the API, with files named fn.js representing API calls (containing the API logic). For example if our API directory was ./api_directory/:

    api_directory/
    	user/
    		get/
    			fn.js
    		update/
    			fn.js
    	collection/
    		get/
    			fn.js
    		add/
    			fn.js
    		remove/
    			fn.js

    The above directory would result in 5 available API calls:

    user/get
    user/update
    collection/get
    collection/add
    collection/remove
  2. The HTML folder which contains a directory tree representing the HTML page structure. The HTML structure does not have to match or resemble the API structure at all. For example if our HTML directory was ./html_directory/:

    html_directory/
    	index.html
    	user/
    		profile.html
    		profile_edit.html
    		profile_card.html
    	collection/
    		display.html

    The above directory would result in 5 available HTML pages:

    index
    user/profile
    user/profile_edit
    user/profile_card
    collection/display

The Include Tag

Including one page in another and populating it with API data is done using include tags in HTML pages, and calling the render function.

  1. Including a file with no data.

    Syntax:

    <include id="header" file="header">

    Example:

    index.html

    <html>
    	<include id="header" file="header">
    	<body>This is my Page</body>
    </html>

    header.html

    <head>This is my header</head>

    rendered

    <html>
    	<head>This is my header</head>
    	<body>This is my Page</body>
    </html>
  2. Including a file with an object.

    Syntax:

    <include id="profile" file="profile">

    Example:

    index.html

    <html>
    	<include id="profile" file="profile">
    </html>

    profile.html

    <img src="@(user.avatar)"><b>@(user.username)</b>
    <a href="user.html?user_id=@(user.user_id)&action=view">

    object

    {
    	"user": {
    		"avatar":  "http://img.com/myavatar",
    		"username": "Willy",
    		"user_id":  12345
    	}
    }

    rendered

    <html>
    	<img src="http://img.com/myavatar"><b>Willy</b>
    	<a href="user.html?user_id=12345&action=view">
    </html>
  3. Conditionally including a file.

    Syntax:

    <include id="profile" file="profile" if="user.show">

    Example:

    index.html

    <html>
    	<include id="profile" file="profile" if="user.show">
    </html>

    profile.html

    <img src="@(user.avatar)"><b>@(user.username)</b>
    <a href="user.html?user_id=@(user.user_id)&action=view">

    object

    {
    	"user": {
    		"avatar":  "http://img.com/myavatar",
    		"username": "Willy",
    		"user_id":  12345,
    		"show":     false
    	}
    }

    rendered

    <html>
    	<include id="profile">
    </html>
  4. Including an array of objects using the same file.

    Syntax:

    <include id="profile" file="profile" for="users" each="user">

    Example:

    index.html

    <html>
    	<include id="profile" file="profile" for="users" each="user">
    </html>

    profile.html

    <div class="card">
    	<img src="@(user.avatar)"><b>@(user.username)</b>
    	<a href="user.html?user_id=@(user.user_id)&action=view">
    </div>

    object

    {
    	"users": [
    		{
    			"avatar":  "http://img.com/myavatar",
    			"username": "Willy",
    			"user_id":  12345
    		},
    		{
    			"avatar":  "http://img.com/hisavatar",
    			"username": "Jackson",
    			"user_id":  12346
    		},
    		{
    			"avatar":  "http://img.com/heravatar",
    			"username": "April",
    			"user_id":  12347
    		},
    	]
    }

    rendered

    <html>
    	<div class="card">
    		<img src="http://img.com/myavatar"><b>Willy</b>
    		<a href="user.html?user_id=12345&action=view">
    	</div>
    	<div class="card">
    		<img src="http://img.com/hisavatar"><b>Jackson</b>
    		<a href="user.html?user_id=12346&action=view">
    	</div>
    	<div class="card">
    		<img src="http://img.com/heravatar"><b>April</b>
    		<a href="user.html?user_id=12347&action=view">
    	</div>
    </html>

todo:

  • make a true and false for conditional inclusion
  • conditional iterative inclusion?
  • recursive injection
  • what if we want to inject a json object itself and not the value of one of its keys
  • for in just a base array object
  • render_append
  • render_prepend
0.18.7

9 years ago

0.18.6

9 years ago

0.18.5

9 years ago

0.18.4

9 years ago

0.18.3

9 years ago

0.18.2

9 years ago

0.18.1

9 years ago

0.18.0

9 years ago

0.17.0

9 years ago

0.16.0

9 years ago

0.15.0

9 years ago

0.14.2

9 years ago

0.14.1

9 years ago

0.14.0

9 years ago

0.13.9

9 years ago

0.13.8

9 years ago

0.13.7

9 years ago

0.13.6

9 years ago

0.13.5

9 years ago

0.13.4

9 years ago

0.13.3

9 years ago

0.13.2

9 years ago

0.13.1

9 years ago

0.13.0

9 years ago

0.12.5

9 years ago

0.12.4

9 years ago

0.12.3

9 years ago

0.12.2

9 years ago

0.12.1

9 years ago

0.12.0

9 years ago

0.11.0

9 years ago

0.10.0

9 years ago

0.9.0

9 years ago

0.8.7

9 years ago

0.8.6

9 years ago

0.8.5

9 years ago

0.8.4

9 years ago

0.8.3

10 years ago

0.8.2

10 years ago

0.8.1

10 years ago

0.8.0

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.3

10 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.6

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.0.1

10 years ago