Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Did you ever have a feeling that your (or someone else's plugin) has too heavy interface.
Ever felt ashamed for something like
<script src="/scripts/jquery.js"/>
<script src="/scripts/jquery.myplugin.js"/>
<div class="my-plugin-target"/>
<script>
$(function(){
$('.my-plugin-target').myplugin();
})
Ever wanted something a bit simplier?
What if I say that it could be like
<script src="/scripts/myplugin.js"/>
<div class="my-plugin-target"/>
and nothing more?
I'm serious. No jquery for simple plugins. No awaits of the DOMContentLoaded. You need your own element to load, why should you care about all the content? Why do you ever give your users JS api? You've created isolated visual plugin. Look at It's damn colorpicker, and it just works. It does not require any JS initialisation. That's why web designers, who are not familiar with JS, use it and does not your amazing plugin.
Most of JS libs are just something binded to DOM element, so stop lying to yourself: HTML-only api will make your lib more beautiful.
CornerJS has just 8kb (2.5kb gzipped) footprint, so you can include it care-free in your lib. If you are relying upon latest browsers that support MutationObserver, you can just use /src/corner.js, which is 2.5kb minified (without gzip).
How to really use it?
directive('myplugin', function(element){
$(element).myplugin();
})
Yes, that's all. You can use it so:
<div class="myplugin">
<div myplugin/>
<myplugin>
If you need to pass some params:
<div myplugin="width: 100, height: 200"/>
<myplugin width=100 height=200 />
directive('myplugin', function(element, opts){
console.log(opts); //=> {width: 100, height: 200}
$(element).myplugin(opts);
})
If you need to make a destructor
directive('myplugin', {
load: function(element){
$(element).myplugin();
},
unload: function(element){
$(element).myplugin('destroy');
}
})
And even share something between callbacks
directive('myplugin', {
load: function(element){
this.request = createRequest();
},
unload: function(element){
this.request.abort();
}
})
Or maybe you want to listen for attribute changes and process them?
directive('myplugin', {
alter: function(element, opts){
element.innerHTML = opts.content;
}
})
If only alter callback is defined, it is also called on element load. It is done due to a lot of cases when load and alter logic are identical.
Shorthand method directive(String directiveName, Function loadCallback) is equal to directive(String directiveName, {load: Function loadCallback}) Full method directive(String directiveName, { [load: Function loadCallback] [alter: Function alterCallback] [unload: Function unloadCallback] })
If you created something like
you should note that only one load event will shoot. Tags have priority over attributes, attributes have priority over classes.
If you have nested directives, note that load sequence has event capturing logic - from parent element to child ones. unload sequence is bubbling: from children to parent. It is useful when you want to use element as template container, e.g.
<div handlebars-template-with-data-from-url="some.url">
<video-player src="{{url}}">
</div>
directive('handlebars-template-with-data-from-url', function(el){
this.template = el.innerHTML;
el.innerHTML = '';
//...do magic...
});
In that case video-player will not be executed.
##Current bugs IE 9 and 10 do not support attribute removal from the node. It is allegedly connected with mutationobserver polyfill, and is uncommon situation(usually it is done by hands rather by manipulating DOM), so it should be just taken in mind that it is recommended to remove node, not attribute directive. Otherwise you can use class and tag directives.
FAQs
A new way to create beautiful HTML-binded APIs
The npm package corner.js receives a total of 9 weekly downloads. As such, corner.js popularity was classified as not popular.
We found that corner.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.