{"id":15722973,"url":"https://github.com/gfarrell/jave","last_synced_at":"2026-05-01T09:32:25.060Z","repository":{"id":7837954,"uuid":"9209469","full_name":"gfarrell/Jave","owner":"gfarrell","description":"Jave (j-behave -\u003e bejave -\u003e jave) applies behaviour filters to bind javascript and DOM without additional scripting on page, thus decoupling layout and script.","archived":false,"fork":false,"pushed_at":"2015-01-31T17:33:27.000Z","size":252,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T20:02:01.753Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gfarrell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-04T02:26:53.000Z","updated_at":"2015-01-31T17:32:43.000Z","dependencies_parsed_at":"2022-07-19T12:59:11.747Z","dependency_job_id":null,"html_url":"https://github.com/gfarrell/Jave","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gfarrell%2FJave","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gfarrell%2FJave/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gfarrell%2FJave/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gfarrell%2FJave/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gfarrell","download_url":"https://codeload.github.com/gfarrell/Jave/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246395591,"owners_count":20770243,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T22:09:52.343Z","updated_at":"2026-05-01T09:32:25.024Z","avatar_url":"https://github.com/gfarrell.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Jave, a jQuery Behaviour API\n============================\n\nMany libraries have plugins that are designed to automatically be applied to all elements of a certain attribute (normally a class). This sort of hooking is often done in a variety of ways, which makes having a lot of libraries doing this a bit inefficient (not to mention non-standard).\n\nJave aims to fix this by introducing a simple API for defining behaviours, which hook into a specific element attribute. These are then applied to all the elements at once after the DOM has loaded.\n\nIn short: Jave applies behaviour filters to bind javascript and DOM without additional scripting on page, thus decoupling layout and script.\n\nUsage\n-----\n\n### Configuration\n\nJave's module definition expects \"jquery\" to be defined and loadable. In order for Jave to run smoothly, you should add the following line into your `main.js` (or whatever you call it) application root.\n\n    $.ready(function() { $.jave.init(); });  // run on DOM ready\n\nThis will run Jave when the DOM has loaded, which will automatically apply any defined behaviours to your elements. Jave takes some options:\n\n    $.jave.init(options);\n\n* **selector** (string) defaults to \"[data-behaviour]\" *the selector to fetch elements with*\n* **auto** (bool) defaults to true *whether or not to run Jave automatically*\n* **root** (element) defaults to document.body *the root element to search in*\n\nIf **auto** is set to false, then you will have to run `$.jave.run($root);` manually, optionally specifying a root element (otherwise Jave will use the configured default).\n\n### Defining behaviours\n\nA behaviour is basically a function that normally triggers the instantiation and attachment of other classes (but can contain anything really). Behaviours are defined using `$.jave.define`, for example:\n\n    $.jave.define('my-behaviour', function($el, api) {\n        // do some stuff here\n    });\n\nThe first argument, the behaviour name, is the data-behaviour attribute that this behaviour will be associated with. The second argument is the behaviour itself.\n\nAll behaviours should have the function signature `function($el, api)`. The first argument is just the jQuery'd element, but the second argument is an instance of the JaveAPI applied to this element.\n\n### Running Jave manually\n\nIf you have added elements to the DOM and want to add behaviours to them (e.g. for externally loaded HTML) then you can easily run Jave again on either the whole `document.body` (which won't be as fast) or on the parent element. Jave stores a list of which behaviours have been applied to each element, so you don't have to worry about something getting duplicated.\n\n### The JaveAPI object\n\nAn instance of JaveAPI is attached to a particular element/behaviour combination and is used for retrieving specific values from the element.\n\nBehaviours can have options defined in the HTML tag's attributes. These should take the form `data-my-behaviour-options`, as a serialised JSON object, or `data-my-behaviour-opt1`, which take individual options.\n\nThe JaveAPI can then be used to retrieve the values of these options, and to define default options if those aren't present. For example:\n\n    api.get('option_name'); // retrieves the value of data-my-behaviour-option_name\n    api.getAs('option_name', 'boolean'); // retrieves the value as a boolean\n\n    api.get('option_name', 'hello'); // default value is hello\n    api.getAs('option_name', 'boolean', true); // default value is true\n\n### A working example\n\nI have a validation class that I wish to apply to all inputs of a certain type. Each input can have specific validation criteria.\n\nIn HTML:\n\n    \u003cform action=\"/myform\"\u003e\n        \u003cinput type=\"text\" name=\"email\" data-behaviour=\"validate\" data-validate-type=\"email\" data-validate-required=\"true\" /\u003e\n        \u003cinput type=\"password\" name=\"password\" /\u003e\n\n        \u003cbutton type=\"submit\"\u003eSubmit\u003c/button\u003e\n    \u003c/form\u003e\n    \nAlternatively, all the options can be represented as a JSON string:\n\n\t\u003cinput type=\"text\" name=\"email\" data-behaviour=\"validate\" data-validate-options=\"{\\\"type\\\": \\\"email\\\",  \\\"required\\\": true}\" /\u003e\n\nMy behaviour definition:\n\n    $.jave.define('validate', function($el, api) {\n        var v = new Validator($el, {\n            type:     api.get('type'),\n            required: api.getAs('required', 'boolean', false)\n        });\n        // ...\n    });\n\nNow, when the DOM is ready, my *validate* filter will be applied to all elements with the attribute `data-behaviour=\"validate\"`.\n\n\nPrerequisites\n-------------\n\nJave is defined as an AMD module, with [http://www.requirejs.org](require.js) in mind. You can remove the module definition to have it as a simple class file, but I would recommend leaving it as is and just getting to grips with require.js. Jave requires a JSON library of your choice (as long as `JSON.parse()` exists).\n\nIt also requires jQuery, of course.\n\nInspiration\n-----------\n\nJave was inspired by Anutron's Mootools-Behavior.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgfarrell%2Fjave","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgfarrell%2Fjave","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgfarrell%2Fjave/lists"}