ampersand-virtual-dom-mixin
Advanced tools
Comparing version 0.1.1 to 0.1.2
113
mixin.js
@@ -13,3 +13,2 @@ var parse = require('html-parse-stringify').parse; | ||
}, | ||
renderOnModelChange: function () { | ||
@@ -20,4 +19,3 @@ if (this.model) { | ||
}, | ||
renderWithTemplate: function (spec) { | ||
renderWithTemplate: function (data) { | ||
var firstRender = !this.tree || !this.el; | ||
@@ -29,8 +27,6 @@ var renderedTemplate, newTree; | ||
} else { | ||
renderedTemplate = this.template(spec || this); | ||
renderedTemplate = this.template(data || this); | ||
} | ||
newTree = this.tovdom(parse(renderedTemplate, { | ||
components: this.components || {} | ||
}), this); | ||
newTree = this.astToVdom(parse(renderedTemplate.trim())); | ||
@@ -40,11 +36,16 @@ if (firstRender) { | ||
this.tree = newTree; | ||
this.el = el; | ||
if (this.setElement) { | ||
this.el.appendChild(el); | ||
this._backboneEl = el; | ||
} else { | ||
this.el = el; | ||
} | ||
} else { | ||
var patches = diff(this.tree, newTree); | ||
this.tree = newTree; | ||
patch(this.el, patches); | ||
patch(this._backboneEl || this.el, patches); | ||
} | ||
}, | ||
tovdom: function (ast) { | ||
astToVdom: function (ast) { | ||
if (ast.type === 'text') { | ||
@@ -55,28 +56,30 @@ return ast.content; | ||
if (ast.type === 'tag') { | ||
return h(ast.name, this.parseTagAttrs(ast.attrs), ast.children.map(this.tovdom, this)); | ||
} | ||
if (!(this.components||{})[ast.name]) { | ||
return h(ast.name, this.parseTagAttrs(ast.attrs), ast.children.map(this.astToVdom, this)); | ||
} else { | ||
if (ast.type === 'component') { | ||
var Constructor = this.components[ast.name]; | ||
var attrs = this.parseComponentAttrs(ast.attrs); | ||
//TODO: this whole bit needs some work; | ||
var Constructor = this.components[ast.name]; | ||
var attrs = this.parseComponentAttrs(ast.attrs); | ||
return { | ||
type: 'Widget', | ||
name: 'MyWidget', | ||
id: ast.attrs.key, | ||
key: ast.attrs.key, | ||
init: function () { | ||
this.view = new Constructor(attrs); | ||
this.view.render(); | ||
return this.view.el; | ||
}, | ||
update: function (previous, dom) { | ||
this.view = previous.view; | ||
this.view.set(attrs); | ||
return this.view.el; | ||
}, | ||
destroy: function () { | ||
this.view.remove(); | ||
} | ||
}; | ||
return { | ||
type: 'Widget', | ||
name: 'MyWidget', | ||
id: ast.attrs.key, | ||
key: ast.attrs.key, | ||
init: function () { | ||
this.view = new Constructor(attrs); | ||
this.view.render(); | ||
return this.view.el; | ||
}, | ||
update: function (previous, dom) { | ||
this.view = previous.view; | ||
this.view.set(attrs); | ||
return this.view.el; | ||
}, | ||
destroy: function () { | ||
this.view.remove(); | ||
} | ||
}; | ||
} | ||
} | ||
@@ -86,3 +89,2 @@ }, | ||
parseComponentAttrs: function (attrs) { | ||
var res = {}; | ||
var key, val, path, match; | ||
@@ -96,6 +98,6 @@ var isCurlyRe = /^\s*{\s*([^}]+)\s*}\s*$/; | ||
if (!match) { | ||
res[key] = coerce(val); | ||
attrs[key] = coerce(val); | ||
} else { | ||
path = match[1].split('.'); | ||
res[key] = path.reduce(function (o, pathPart) { | ||
attrs[key] = path.reduce(function (o, pathPart) { | ||
return o && o[pathPart]; | ||
@@ -105,28 +107,21 @@ }, this); | ||
} | ||
return res; | ||
return attrs; | ||
}, | ||
parseTagAttrs: function (attrs) { | ||
var res = {}; | ||
res.attributes = attrs.attributes || {}; | ||
var key, val; | ||
for (key in attrs) { | ||
val = attrs[key]; | ||
if (key.slice(0,2) === 'on' && val.trim().match(/^{[^}]+}$/)) { | ||
val = val.trim().slice(1,-1); | ||
if (typeof this[val] === 'function') { | ||
res[key] = this[val].bind(this); | ||
} else { | ||
res[key] = attrs[key]; | ||
} | ||
} else if (key.slice(0,5) === 'data-') { | ||
res.attributes[key] = attrs[key]; | ||
} else { | ||
if (key === 'class') { | ||
key = 'className'; | ||
} | ||
res[key] = val; | ||
attrs = { | ||
attributes: attrs | ||
}; | ||
var keep = ['key', 'namespace']; | ||
var i = keep.length; | ||
var k; | ||
while(i--) { | ||
k = keep[i]; | ||
if (attrs.attributes[k]) { | ||
attrs[k] = attrs.attributes[k]; | ||
delete attrs.attributes[k]; | ||
} | ||
} | ||
return res; | ||
return attrs; | ||
} | ||
@@ -133,0 +128,0 @@ }; |
{ | ||
"name": "ampersand-virtual-dom-mixin", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"dependencies": { | ||
@@ -11,5 +11,3 @@ "amp-is-string": "^1.0.0", | ||
"main": "mixin.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
"scripts": {} | ||
} |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
2
0
4484
113