Last active
November 5, 2020 15:02
-
-
Save eightyfive/232c0d1316d055d5242e70051fa00f77 to your computer and use it in GitHub Desktop.
Lure.js – Tiny Object-Oriented jQuery UI library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(root, $) { | |
var Lure = root.Lure = {}; | |
// ALL credits goes to: backbone.js & marionette.js | |
Lure.$ = $; | |
var idCounter = 1; | |
// Lure.Events | |
// --------------- | |
var Events = Lure.Events = { | |
on: function(name, callback, context) { | |
var events = this._events[name] || (this._events[name] = []); | |
events.push({callback: callback, ctx: context || this}); | |
return this; | |
}, | |
off: function(name) { | |
if (name) | |
delete this._events[name]; | |
else { | |
delete this._events; | |
this._events = {}; | |
} | |
return this; | |
}, | |
trigger: function(name) { | |
if (!this._events) return this; | |
var args = Array.prototype.slice.call(arguments, 1); | |
var events = this._events[name]; | |
if (events) { | |
for (var i=0, len=events.length;i<len;i++) { | |
(ev = events[i]).callback.apply(ev.ctx, args); | |
} | |
} | |
return this; | |
} | |
}; | |
// Lure.View | |
// ------------- | |
var View = Lure.View = function (options) { | |
options || (options = {}); | |
this.cid = 'lv'+idCounter++; | |
this._events = {}; | |
this.$el = options.el instanceof Lure.$ ? options.el : $(options.el); | |
this.el = this.$el[0]; | |
// Bind UI elements | |
var bindings = this.ui; | |
this.ui = {}; | |
for (var key in bindings) { | |
this.ui[key] = this.$(bindings[key]); | |
} | |
// Initialize jQuery plugins | |
for (var plugin in this.plugins) { | |
this.$el[plugin].call(this.$el, this.plugins[plugin]); | |
} | |
this.initialize.apply(this, arguments); | |
this.delegateEvents(); | |
}; | |
Lure.$.extend(View.prototype, Events, { | |
ui: {}, | |
plugins: {}, | |
initialize: function() {}, | |
$: function(selector) { | |
return this.$el.find(selector); | |
}, | |
delegateEvents: function(events) | |
{ | |
if (!(events || (events = this.events))) return this; | |
this.undelegateEvents(); | |
for (var key in events) { | |
var method = events[key]; | |
if (Lure.$.type(method) !== 'function') method = this[events[key]]; | |
if (!method) continue; | |
var match = key.match(/^(\S+)\s*(.*)$/); | |
var eventName = match[1], selector = match[2]; | |
method = Lure.$.proxy(method, this); | |
eventName += '.delegateEvents' + this.cid; | |
if (selector === '') | |
this.$el.on(eventName, method); | |
else | |
this.$el.on(eventName, selector, method); | |
} | |
return this; | |
}, | |
undelegateEvents: function() | |
{ | |
this.$el.off('.delegateEvents' + this.cid); | |
return this; | |
} | |
}); | |
// Extend | |
// ------------- | |
var extend = Lure.extend = View.extend = function(protoProps, staticProps) { | |
var parent = this; | |
var child = function(){ return parent.apply(this, arguments); }; | |
$.extend(child, parent, staticProps); | |
// Set the prototype chain to inherit from `parent`, without calling | |
// `parent`'s constructor function. | |
var Surrogate = function(){ this.constructor = child; }; | |
Surrogate.prototype = parent.prototype; | |
child.prototype = new Surrogate; | |
if (protoProps) $.extend(child.prototype, protoProps); | |
return child; | |
}; | |
})(window, window.$); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment