Skip to content

Commit

Permalink
clean up compiler comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 24, 2013
1 parent a1582e4 commit 591eb8d
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,45 @@ var Emitter = require('./emitter'),
function Compiler (vm, options) {

var compiler = this

// indicate that we are intiating this instance
// so we should not run any transitions
compiler.init = true

// process and extend options
options = compiler.options = options || makeHash()
var data = compiler.data = options.data || {}
utils.processOptions(options)
extend(compiler, options.compilerOptions)

// copy data, methods & compiler options
var data = compiler.data = options.data || {}
extend(vm, data, true)
extend(vm, options.methods, true)
extend(compiler, options.compilerOptions)

// initialize element
var el = compiler.setupElement(options)
log('\nnew VM instance:', el.tagName, '\n')

// set compiler properties
compiler.vm = vm
def(vm, '$', makeHash())
def(vm, '$el', el)
def(vm, '$compiler', compiler)

// keep track of directives and expressions
// so they can be unbound during destroy()
compiler.dirs = []
compiler.exps = []
compiler.childCompilers = [] // keep track of child compilers
compiler.emitter = new Emitter() // the emitter used for nested VM communication

// Store things during parsing to be processed afterwards,
// because we want to have created all bindings before
// observing values / parsing dependencies.
var computed = compiler.computed = []
compiler.computed = []
compiler.childCompilers = []
compiler.emitter = new Emitter()

// prototypal inheritance of bindings
// inherit parent bindings
var parent = compiler.parentCompiler
compiler.bindings = parent
? Object.create(parent.bindings)
: makeHash()
compiler.rootCompiler = parent
? getRoot(parent)
: compiler

// set inenumerable VM properties
def(vm, '$', makeHash())
def(vm, '$el', el)
def(vm, '$compiler', compiler)
def(vm, '$root', compiler.rootCompiler.vm)

// set parent VM
Expand All @@ -82,13 +79,17 @@ function Compiler (vm, options) {

// setup observer
compiler.setupObserver()

// beforeCompile hook
compiler.execHook('beforeCompile', 'created')

// the user might have set some props on the vm
// so copy it back to the data...
extend(data, vm)

// observe the data
Observer.observe(data, '', compiler.observer)

// for repeated items, create an index binding
// which should be inenumerable but configurable
if (compiler.repeat) {
Expand All @@ -97,6 +98,7 @@ function Compiler (vm, options) {
compiler.createBinding('$index')
}

// allow the $data object to be swapped
Object.defineProperty(vm, '$data', {
enumerable: false,
get: function () {
Expand All @@ -114,10 +116,15 @@ function Compiler (vm, options) {
// now parse the DOM, during which we will create necessary bindings
// and bind the parsed directives
compiler.compile(el, true)

// extract dependencies for computed properties
if (computed.length) DepsParser.parse(computed)
if (compiler.computed.length) {
DepsParser.parse(compiler.computed)
}

// done!
compiler.init = false

// post compile / ready hook
compiler.execHook('afterCompile', 'ready')
}
Expand Down

0 comments on commit 591eb8d

Please sign in to comment.