@@ -11,7 +11,6 @@ var Emitter = require('./emitter'),
1111
1212 // cache methods
1313 slice = [ ] . slice ,
14- each = [ ] . forEach ,
1514 makeHash = utils . hash ,
1615 extend = utils . extend ,
1716 def = utils . defProtected ,
@@ -27,7 +26,7 @@ var Emitter = require('./emitter'),
2726 // list of priority directives
2827 // that needs to be checked in specific order
2928 priorityDirectives = [
30- 'i' + 'f ',
29+ 'if ' ,
3130 'repeat' ,
3231 'view' ,
3332 'component'
@@ -39,7 +38,8 @@ var Emitter = require('./emitter'),
3938 */
4039function Compiler ( vm , options ) {
4140
42- var compiler = this
41+ var compiler = this ,
42+ key , i
4343
4444 // default state
4545 compiler . init = true
@@ -94,17 +94,22 @@ function Compiler (vm, options) {
9494 // create bindings for computed properties
9595 var computed = options . computed
9696 if ( computed ) {
97- for ( var key in computed ) {
97+ for ( key in computed ) {
9898 compiler . createBinding ( key )
9999 }
100100 }
101101
102102 // copy paramAttributes
103- if ( options . paramAttributes ) {
104- options . paramAttributes . forEach ( function ( attr ) {
105- var val = compiler . eval ( el . getAttribute ( attr ) )
106- vm [ attr ] = utils . checkNumber ( val )
107- } )
103+ var params = options . paramAttributes
104+ if ( params ) {
105+ i = params . length
106+ while ( i -- ) {
107+ vm [ params [ i ] ] = utils . checkNumber (
108+ compiler . eval (
109+ el . getAttribute ( params [ i ] )
110+ )
111+ )
112+ }
108113 }
109114
110115 // beforeCompile hook
@@ -131,9 +136,10 @@ function Compiler (vm, options) {
131136 compiler . compile ( el , true )
132137
133138 // bind deferred directives (child components)
134- compiler . deferred . forEach ( function ( dir ) {
135- compiler . bindDirective ( dir )
136- } )
139+ i = compiler . deferred . length
140+ while ( i -- ) {
141+ compiler . bindDirective ( compiler . deferred [ i ] )
142+ }
137143
138144 // extract dependencies for computed properties
139145 compiler . parseDeps ( )
@@ -158,27 +164,32 @@ CompilerProto.setupElement = function (options) {
158164 ? document . querySelector ( options . el )
159165 : options . el || document . createElement ( options . tagName || 'div' )
160166
161- var template = options . template
167+ var template = options . template ,
168+ child , frag , replacer , i , attr , attrs
169+
162170 if ( template ) {
163171 // collect anything already in there
164172 /* jshint boss: true */
165- var child ,
166- frag = this . rawContent = document . createDocumentFragment ( )
173+ frag = this . rawContent = document . createDocumentFragment ( )
167174 while ( child = el . firstChild ) {
168175 frag . appendChild ( child )
169176 }
170177 // replace option: use the first node in
171178 // the template directly
172179 if ( options . replace && template . childNodes . length === 1 ) {
173- var replacer = template . childNodes [ 0 ] . cloneNode ( true )
180+ replacer = template . childNodes [ 0 ] . cloneNode ( true )
174181 if ( el . parentNode ) {
175182 el . parentNode . insertBefore ( replacer , el )
176183 el . parentNode . removeChild ( el )
177184 }
178185 // copy over attributes
179- each . call ( el . attributes , function ( attr ) {
180- replacer . setAttribute ( attr . name , attr . value )
181- } )
186+ if ( el . hasAttributes ( ) ) {
187+ i = el . attributes . length
188+ while ( i -- ) {
189+ attr = el . attributes [ i ]
190+ replacer . setAttribute ( attr . name , attr . value )
191+ }
192+ }
182193 // replace
183194 el = replacer
184195 } else {
@@ -189,9 +200,9 @@ CompilerProto.setupElement = function (options) {
189200 // apply element options
190201 if ( options . id ) el . id = options . id
191202 if ( options . className ) el . className = options . className
192- var attrs = options . attributes
203+ attrs = options . attributes
193204 if ( attrs ) {
194- for ( var attr in attrs ) {
205+ for ( attr in attrs ) {
195206 el . setAttribute ( attr , attrs [ attr ] )
196207 }
197208 }
@@ -224,19 +235,21 @@ CompilerProto.setupObserver = function () {
224235 . on ( 'mutate' , onSet )
225236
226237 // register hooks
227- hooks . forEach ( function ( hook ) {
228- var fns = options [ hook ]
238+ var i = hooks . length , j , hook , fns
239+ while ( i -- ) {
240+ hook = hooks [ i ]
241+ fns = options [ hook ]
229242 if ( Array . isArray ( fns ) ) {
230- var i = fns . length
243+ j = fns . length
231244 // since hooks were merged with child at head,
232245 // we loop reversely.
233- while ( i -- ) {
234- registerHook ( hook , fns [ i ] )
246+ while ( j -- ) {
247+ registerHook ( hook , fns [ j ] )
235248 }
236249 } else if ( fns ) {
237250 registerHook ( hook , fns )
238251 }
239- } )
252+ }
240253
241254 // broadcast attached/detached hooks
242255 observer
@@ -300,8 +313,7 @@ CompilerProto.observeData = function (data) {
300313 $dataBinding . update ( data )
301314
302315 // allow $data to be swapped
303- defGetSet ( compiler . vm , '$data' , {
304- enumerable : false ,
316+ Object . defineProperty ( compiler . vm , '$data' , {
305317 get : function ( ) {
306318 compiler . observer . emit ( 'get' , '$data' )
307319 return compiler . data
@@ -622,7 +634,7 @@ CompilerProto.defineProp = function (key, binding) {
622634
623635 binding . value = data [ key ]
624636
625- defGetSet ( compiler . vm , key , {
637+ Object . defineProperty ( compiler . vm , key , {
626638 get : function ( ) {
627639 return compiler . data [ key ]
628640 } ,
@@ -640,14 +652,14 @@ CompilerProto.defineProp = function (key, binding) {
640652CompilerProto . defineMeta = function ( key , binding ) {
641653 var vm = this . vm ,
642654 ob = this . observer ,
643- value = binding . value = key in vm
655+ value = binding . value = hasOwn . call ( vm , key )
644656 ? vm [ key ]
645657 : this . data [ key ]
646658 // remove initital meta in data, since the same piece
647659 // of data can be observed by different VMs, each have
648660 // its own associated meta info.
649661 delete this . data [ key ]
650- defGetSet ( vm , key , {
662+ Object . defineProperty ( vm , key , {
651663 get : function ( ) {
652664 if ( Observer . shouldGet ) ob . emit ( 'get' , key )
653665 return value
@@ -676,7 +688,7 @@ CompilerProto.defineExp = function (key, binding, directive) {
676688 */
677689CompilerProto . defineComputed = function ( key , binding , value ) {
678690 this . markComputed ( binding , value )
679- defGetSet ( this . vm , key , {
691+ Object . defineProperty ( this . vm , key , {
680692 get : binding . value . $get ,
681693 set : binding . value . $set
682694 } )
@@ -884,11 +896,4 @@ function getRoot (compiler) {
884896 return compiler
885897}
886898
887- /**
888- * for convenience & minification
889- */
890- function defGetSet ( obj , key , def ) {
891- Object . defineProperty ( obj , key , def )
892- }
893-
894899module . exports = Compiler
0 commit comments