Skip to content

Commit

Permalink
sd-id
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 19, 2013
1 parent 094a1fe commit 5fa908d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
31 changes: 31 additions & 0 deletions examples/child-id.html
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<script src="../dist/seed.js"></script>
</head>
<body>
<div id="parent">
<div sd-id="child" sd-viewmodel="child">
{{msg}}
</div>
</div>
<script>
Seed.viewmodel('child', Seed.extend({
init: function () {
console.log('child init!')
},
proto: {
hi: function () {
console.log('hi from child!')
}
},
scope: {
msg: 'I am a child'
}
}))
var app = new Seed({el:'#parent'})
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions examples/simple-dir.html
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<script src="../dist/seed.js"></script>
</head>
<body>
<div id="hi" sd-test sd-test2></div>
<script>
var a = new Seed({
el: '#hi',
directives: {
test: {
bind: function () {
console.log('bind')
},
unbind: function () {
console.log('unbind')
}
},
test2: function () {
console.log('test2 bind')
}
}
})
a.$destroy()
</script>
</body>
</html>
30 changes: 23 additions & 7 deletions src/compiler.js
Expand Up @@ -7,14 +7,20 @@ var Emitter = require('./emitter'),
TextParser = require('./text-parser'),
DepsParser = require('./deps-parser'),
ExpParser = require('./exp-parser'),

// cache methods
slice = Array.prototype.slice,
log = utils.log,
def = utils.defProtected,

// special directives
idAttr,
vmAttr,
preAttr,
repeatAttr,
partialAttr,
transitionAttr,
preAttr
transitionAttr


/**
* The DOM compiler
Expand All @@ -40,8 +46,9 @@ function Compiler (vm, options) {

compiler.vm = vm
// special VM properties are inumerable
def(vm, '$compiler', compiler)
def(vm, '$', {})
def(vm, '$el', compiler.el)
def(vm, '$compiler', compiler)

// keep track of directives and expressions
// so they can be unbound during destroy()
Expand All @@ -65,6 +72,12 @@ function Compiler (vm, options) {
? getRoot(parent)
: compiler

// register child id on parent
var childId = compiler.el.getAttribute(idAttr)
if (childId && parent) {
parent.vm.$[childId] = vm
}

// setup observer
compiler.setupObserver()

Expand Down Expand Up @@ -182,12 +195,14 @@ CompilerProto.compile = function (node, root) {
if (node.nodeType === 1) {
// a normal node
if (node.hasAttribute(preAttr)) return
var repeatExp = node.getAttribute(repeatAttr),
vmId = node.getAttribute(vmAttr),
var vmId = node.getAttribute(vmAttr),
repeatExp = node.getAttribute(repeatAttr),
partialId = node.getAttribute(partialAttr)
// we need to check for any possbile special directives
// e.g. sd-repeat, sd-viewmodel & sd-partial
if (repeatExp) { // repeat block
// repeat block cannot have sd-id at the same time.
node.removeAttribute(idAttr)
var directive = Directive.parse(repeatAttr, repeatExp, compiler, node)
if (directive) {
compiler.bindDirective(directive)
Expand Down Expand Up @@ -592,11 +607,12 @@ CompilerProto.destroy = function () {
*/
function refreshPrefix () {
var prefix = config.prefix
repeatAttr = prefix + '-repeat'
idAttr = prefix + '-id'
vmAttr = prefix + '-viewmodel'
preAttr = prefix + '-pre'
repeatAttr = prefix + '-repeat'
partialAttr = prefix + '-partial'
transitionAttr = prefix + '-transition'
preAttr = prefix + '-pre'
}

/**
Expand Down

0 comments on commit 5fa908d

Please sign in to comment.