|
|
@@ -1,21 +1,21 @@
|
|
|
var config = require('./config'),
|
|
|
- Directive = require('./directive'),
|
|
|
- Directives = require('./directives'),
|
|
|
- Filters = require('./filters')
|
|
|
+ Directive = require('./directive')
|
|
|
|
|
|
-function Seed (el, data) {
|
|
|
+var map = Array.prototype.map,
|
|
|
+ each = Array.prototype.forEach
|
|
|
+
|
|
|
+function Seed (el, data, options) {
|
|
|
|
|
|
if (typeof el === 'string') {
|
|
|
el = document.querySelector(el)
|
|
|
}
|
|
|
|
|
|
this.el = el
|
|
|
- this._bindings = {}
|
|
|
this.scope = {}
|
|
|
+ this._bindings = {}
|
|
|
+ this._options = options || {}
|
|
|
|
|
|
// process nodes for directives
|
|
|
- var els = el.querySelectorAll(config.selector)
|
|
|
- ;[].forEach.call(els, this._compileNode.bind(this))
|
|
|
this._compileNode(el)
|
|
|
|
|
|
// initialize all variables by invoking setters
|
|
|
@@ -27,28 +27,58 @@ function Seed (el, data) {
|
|
|
|
|
|
Seed.prototype._compileNode = function (node) {
|
|
|
var self = this
|
|
|
- cloneAttributes(node.attributes).forEach(function (attr) {
|
|
|
- var directive = Directive.parse(attr)
|
|
|
- if (directive) {
|
|
|
- self._bind(node, directive)
|
|
|
- }
|
|
|
- })
|
|
|
+
|
|
|
+ if (node.nodeType === 3) {
|
|
|
+ // text node
|
|
|
+ self._compileTextNode(node)
|
|
|
+ } else if (node.attributes && node.attributes.length) {
|
|
|
+ // clone attributes because the list can change
|
|
|
+ var attrs = map.call(node.attributes, function (attr) {
|
|
|
+ return {
|
|
|
+ name: attr.name,
|
|
|
+ value: attr.value
|
|
|
+ }
|
|
|
+ })
|
|
|
+ attrs.forEach(function (attr) {
|
|
|
+ var directive = Directive.parse(attr)
|
|
|
+ if (directive) {
|
|
|
+ self._bind(node, directive)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!node['sd-block'] && node.childNodes.length) {
|
|
|
+ each.call(node.childNodes, function (child) {
|
|
|
+ self._compileNode(child)
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+Seed.prototype._compileTextNode = function (node) {
|
|
|
+
|
|
|
}
|
|
|
|
|
|
Seed.prototype._bind = function (node, directive) {
|
|
|
|
|
|
+ directive.seed = this
|
|
|
directive.el = node
|
|
|
+
|
|
|
node.removeAttribute(directive.attr.name)
|
|
|
|
|
|
- var key = directive.key,
|
|
|
- binding = this._bindings[key] || this._createBinding(key)
|
|
|
+ var key = directive.key,
|
|
|
+ epr = this._options.eachPrefixRE
|
|
|
+ if (epr) {
|
|
|
+ key = key.replace(epr, '')
|
|
|
+ }
|
|
|
+
|
|
|
+ var binding = this._bindings[key] || this._createBinding(key)
|
|
|
|
|
|
// add directive to this binding
|
|
|
binding.directives.push(directive)
|
|
|
|
|
|
// invoke bind hook if exists
|
|
|
if (directive.bind) {
|
|
|
- directive.bind(node, binding.value)
|
|
|
+ directive.bind.call(directive, binding.value)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -89,8 +119,9 @@ Seed.prototype.dump = function () {
|
|
|
Seed.prototype.destroy = function () {
|
|
|
for (var key in this._bindings) {
|
|
|
this._bindings[key].directives.forEach(unbind)
|
|
|
+ delete this._bindings[key]
|
|
|
}
|
|
|
- this.el.parentNode.remove(this.el)
|
|
|
+ this.el.parentNode.removeChild(this.el)
|
|
|
function unbind (directive) {
|
|
|
if (directive.unbind) {
|
|
|
directive.unbind()
|
|
|
@@ -98,14 +129,4 @@ Seed.prototype.destroy = function () {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// clone attributes so they don't change
|
|
|
-function cloneAttributes (attributes) {
|
|
|
- return [].map.call(attributes, function (attr) {
|
|
|
- return {
|
|
|
- name: attr.name,
|
|
|
- value: attr.value
|
|
|
- }
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
module.exports = Seed
|