Skip to content

Instantly share code, notes, and snippets.

@bumbu
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bumbu/9822326 to your computer and use it in GitHub Desktop.
Save bumbu/9822326 to your computer and use it in GitHub Desktop.
Pathvisiojs modularisation
<!DOCTYPE html>
<html>
<head>
<title>Pathvisiojs Demos</title>
<script src="./../dist/lib/jquery/js/jquery.min.js"></script>
<script src="pathvisiojs.js"></script>
<script>
$(function(){
$('#path1').pathvisiojs()
$('#path2').pathvisiojs()
$('#path1').pathvisiojs()
$('#path2').pathvisiojs()
})
</script>
</head>
<body>
<div id="path1">1</div>
<div id="path2">2</div>
</body>
</html>
(function(window, $){
var PathvisioJs = function (element, options) {
this._init(element, options)
}
var PathvisioJs_counter = 0;
PathvisioJs.prototype._defaults = {}
PathvisioJs.prototype._init = function(element, options) {
this.$element = $(element).empty();
this.options = $.extend(true, {}, this._defaults, options)
// Make this instance unique
this.instanceId = ++PathvisioJs_counter;
// TEST
this.$element.append('<span>Instance id: '+this.instanceId+'</span>')
// Init render
this._initRender()
return this;
}
PathvisioJs.prototype._initRender = function() {
var renderer = require('./renderer.js')
renderer.init(this)
}
// Plugin entry point
$.fn.pathvisiojs = function (option) {
var _arguments = arguments
return this.each(function () {
var $this = $(this)
, data = $this.data('pathvisiojs')
, options = typeof option == 'object' && option
if (!data) {
$this.data('pathvisiojs', (data = new PathvisioJs(this, options)))
}
if (typeof option == 'string') {
if (data[option] === undefined) {
$.error("PathvisioJs has no such method")
} else if (typeof data[option] !== typeof function(){}) {
$.error("PathvisioJs." + option + " is not a function")
} else if (option.charAt(0) == '_') {
$.error("PathvisioJs." + option + " is a private function")
} else {
data[option].call(data, Array.prototype.slice.call(_arguments, 1))
}
}
})
}
})(window, jQuery)
module.exports = function() {
var init = function(pathvisiojs) {
console.log('renderer rendered')
pathvisiojs.$element.append('<span>This is renderer</span>')
}
return {
init: init
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment