Skip to content

Instantly share code, notes, and snippets.

@shash7
Last active January 4, 2016 01:08
Show Gist options
  • Save shash7/8545956 to your computer and use it in GitHub Desktop.
Save shash7/8545956 to your computer and use it in GitHub Desktop.
Component based framework for js apps.Dependent on jquery.
/*
*
* Custom MVC library
*
*
* Api reference :
* var mvc = new Mvc // options > debug : false
* var page = new mvc.Component({
* name : 'shash7', // some value
* Oye : function() { // some function
* console.log('gg');
* },
* events : { // some events (use with on/off)
* 'loaded' : 'render'
* }
*
* get : function(name){}, // get reference of other views
* on : function(){}, // bind events (declare events in your events object
* off : function(){}, // unbind events
* disable : function(el){}, // disable given el and add 'disable' class
* enable : function(el){} // enable given el and remove 'disable' class
* }
*/
;(function($, document) {
'use strict';
function Mvc(options) {
options = options || {};
var debug = options.debug || false;
var viewList = [];
/* -------- View -------- */
var Component = function(attr) {
viewList.push({ name : attr.name || viewList.length, attr : attr });
attr.data = {};
attr.get = function(name) {
if (!name) {
return viewList;
} else {
var result = false;
viewList.map(function(data) {
if (name === data.name) {
result = data;
}
});
return result;
}
};
attr.on = function() {
var that = this;
var events = this.events || {};
$.each(events, function(i, data) {
if (i.match(' ') === null) {
$(document).on(i, that[data]);
} else {
var arr = i.split(' ');
$(document).on(arr[1], arr[0], that[data]);
}
});
message('on');
};
attr.off = function() {
var that = this;
var events = this.events || {};
$.each(events, function(i, data) {
if (i.match(' ') === null) {
$(document).off(i, that[data]);
} else {
var arr = i.split(' ');
$(document).off(arr[1], arr[0], that[data]);
}
});
message('off');
};
attr.trigger = function(name, data) {
$.event.trigger(name, data);
};
attr.disable = function(el) {
el.disabled = true;
$(el).addClass('disable');
};
attr.enable = function(el) {
el.disabled = false;
$(el).removeClass('disable');
};
attr.allOn = function() {
viewList.map(function(data) {
data.attr.on();
});
};
attr.allOff = function() {
viewList.map(function(data) {
data.attr.off();
});
};
return attr;
};
function message(str) {
if(debug) {
console.log(str);
}
}
return {
Component : Component
};
}
window.Mvc = Mvc;
})($, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment