-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
…functionality. Add plugin tests. See #1
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,43 +30,49 @@ exports = module.exports = Reactive; | |
* @api public | ||
*/ | ||
|
||
function Reactive(el, model, opt) { | ||
if (!(this instanceof Reactive)) return new Reactive(el, model, opt); | ||
function Reactive(model, opt) { | ||
if (!(this instanceof Reactive)) return new Reactive(model, opt); | ||
opt = opt || {}; | ||
|
||
if (typeof el === 'string') { | ||
el = domify(el); | ||
} | ||
|
||
var self = this; | ||
self.opt = opt || {}; | ||
self.model = model || {}; | ||
self.adapter = (opt.adapter || Adapter)(self.model); | ||
self.el = el; | ||
self.view = opt.delegate || Object.create(null); | ||
|
||
self.bindings = opt.bindings || Object.create(null); | ||
|
||
// TODO undo this crap and just export bindings regularly | ||
// not that binding order matters!! | ||
bindings({ | ||
bind: function(name, fn) { | ||
self.bindings[name] = fn; | ||
} | ||
}); | ||
|
||
self._bind(this.el, []); | ||
this.use(bindings) | ||
} | ||
|
||
Emitter(Reactive.prototype); | ||
|
||
/** | ||
* Render the view. | ||
* | ||
* @param {String|DOM} el | ||
* @return {Reactive} | ||
* @api public | ||
*/ | ||
|
||
Reactive.prototype.render = function (el) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
var self = this; | ||
|
||
if (typeof el === 'string') { | ||
el = domify(el); | ||
} | ||
|
||
self.el = el; | ||
self._bind(this.el, []); | ||
return self; | ||
} | ||
|
||
/** | ||
* Subscribe to changes on `prop`. | ||
* | ||
* @param {String} prop | ||
* @param {Function} fn | ||
* @return {Reactive} | ||
* @api private | ||
* @api public | ||
*/ | ||
|
||
Reactive.prototype.sub = function(prop, fn){ | ||
|
@@ -288,24 +294,18 @@ Reactive.prototype._bind = function() { | |
Reactive.prototype.bind = function(name, fn) { | ||
var self = this; | ||
if ('object' == typeof name) { | ||
for (var key in name) { | ||
this.bind(key, name[key]); | ||
} | ||
return; | ||
Object.keys(name).forEach(function (key) { | ||
self.bind(key, name[key]); | ||
}) | ||
return self; | ||
} | ||
|
||
var els = query.all('[' + name + ']', this.el); | ||
if (this.el.hasAttribute && this.el.hasAttribute(name)) { | ||
els = [].slice.call(els); | ||
els.unshift(this.el); | ||
if (!self.el) { | ||
self.bindings[name] = fn; | ||
return self; | ||
} | ||
if (!els.length) return; | ||
|
||
debug('bind [%s] (%d elements)', name, els.length); | ||
for (var i = 0; i < els.length; i++) { | ||
var binding = new Binding(name, this, els[i], fn); | ||
binding.bind(); | ||
} | ||
throw new Error('.bind() cannot be called after .render()') | ||
}; | ||
|
||
/** | ||
|
@@ -326,6 +326,7 @@ Reactive.prototype.destroy = function() { | |
self.adapter.unsubscribeAll(); | ||
self.emit('destroyed'); | ||
self.removeAllListeners(); | ||
delete self.el; | ||
}; | ||
|
||
/** | ||
|
@@ -339,6 +340,22 @@ Reactive.prototype.use = function(fn) { | |
return this; | ||
}; | ||
|
||
/** | ||
* Create a Dommit view with the given model but the same bindings, etc. | ||
* | ||
* @param {Object} model | ||
* @return {Reactive} subview | ||
* @api public | ||
*/ | ||
|
||
Reactive.prototype.subview = function (model) { | ||
return new Reactive(model, { | ||
bindings: this.bindings, | ||
adapter: this.opt.adapter, | ||
bindings: this.bindings, | ||
delegate: this.view || {} | ||
}) | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
defunctzombie
Contributor
|
||
|
||
function inDomTree(el) { | ||
try { | ||
|
Does this render onto an element? or is the
el
meant to be a string template? Can you render more than once? I feel that the template should be specified when creating the view and render should be just the thing that binds maybe?