Skip to content

Instantly share code, notes, and snippets.

@gibbitz
Forked from bshack/usage.js
Last active March 24, 2016 23:25
Show Gist options
  • Save gibbitz/8e3d0ad18370f6e1f578 to your computer and use it in GitHub Desktop.
Save gibbitz/8e3d0ad18370f6e1f578 to your computer and use it in GitHub Desktop.
simple js view for being extended
(function() {
'use strict';
const View = require('./viewBase.js');
new View({
el: document.querySelector('a'),
model: {'foo': 'bar'}
});
})();
(function() {
'use strict';
module.exports = function(_options) {
// default property values
// provides a template object for _options overrides
var defaults = {
init : function(){
this.addListeners();
window.console.log('init', this);
},
el : document.createElement('div'),
model : {},
addListeners: function(){
this.el.addEventListener('click', self.eventClick);
},
eventClick : function(_e){
window.console.log(_e, this);
_e.preventDefault();
},
render: function(){
}
};
// return a "class" instance
return new function ViewBase(_opts){
Object.assign(this.constructor.prototype, defaults, _opts);
this.constructor.prototype.init.call(this);
this.constructor.prototype.render.call(this);
}(_options);
};
})();
@bshack
Copy link

bshack commented Mar 24, 2016

i like this style the best:

var selectorView = new ViewBase({
el:'.selector-class',
events:{
'mouseOver' : function(_event){
// handle code here
}
}
});

How could we support events on child elements? Backbone does this:

events: {
'click header button': 'eventMenuToggle',
'click #accessible-ajax-example-button': 'eventAccessibleAjax'
},

This looks pretty good, think I will roll with this for my views when i don't want to do something third party.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment