Sha256: a99177d5bb9cd11ab7ff985ea6a2a4798c0b8baf8b63454d5956212e1eb4a141

Contents?: true

Size: 1.65 KB

Versions: 23

Compression:

Stored size: 1.65 KB

Contents

/** 
 * JSON Schema link handler
 * Copyright (c) 2007 Kris Zyp SitePen (www.sitepen.com)
 * Licensed under the MIT (MIT-LICENSE.txt) license.
 */
({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory();}}).
define([], function(){
var exports = {};
exports.cacheLinks = true;
exports.getLink = function(relation, instance, schema){
	// gets the URI of the link for the given relation based on the instance and schema
	// for example:
	// getLink(
	// 		"brother", 
	// 		{"brother_id":33}, 
	// 		{links:[{rel:"brother", href:"Brother/{brother_id}"}]}) ->
	//	"Brother/33"
	var links = schema.__linkTemplates; 
	if(!links){
		links = {};
		var schemaLinks = schema.links;
		if(schemaLinks && schemaLinks instanceof Array){
			schemaLinks.forEach(function(link){
	/*			// TODO: allow for multiple same-name relations
				if(links[link.rel]){
					if(!(links[link.rel] instanceof Array)){
						links[link.rel] = [links[link.rel]];
					}
				}*/
				links[link.rel] = link.href;
			});
		}
		if(exports.cacheLinks){
			schema.__linkTemplates = links;
		}
	}
	var linkTemplate = links[relation];
	return linkTemplate && exports.substitute(linkTemplate, instance);
};

exports.substitute = function(linkTemplate, instance){
	return linkTemplate.replace(/\{([^\}]*)\}/g, function(t, property){
			var value = instance[decodeURIComponent(property)];
			if(value instanceof Array){
				// the value is an array, it should produce a URI like /Table/(4,5,8) and store.get() should handle that as an array of values
				return '(' + value.join(',') + ')';
			}
			return value;
		});
};
return exports;
});

Version data entries

23 entries across 20 versions & 3 rubygems

Version Path
tck-lambdas-0.3.10 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.9 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.8 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.7 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.6 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.5 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.4 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.3 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.2 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.1 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.0 lib/tck/lambdas/chistacojs/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.3.0 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.2.0 lib/tck/lambdas/chistacojs/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.2.0 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.1.6 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
tck-lambdas-0.1.6 lib/tck/lambdas/chistacojs/node_modules/request/node_modules/http-signature/node_modules/jsprim/node_modules/json-schema/lib/links.js
brwy_rails-0.0.6 test/dummy/node_modules/fsevents/node_modules/json-schema/lib/links.js
brwy_rails-0.0.5 test/dummy/node_modules/fsevents/node_modules/json-schema/lib/links.js
blackboard-3.1.9 lib/site_template/node_modules/json-schema/lib/links.js
brwy_rails-0.0.4 test/dummy/node_modules/fsevents/node_modules/json-schema/lib/links.js