Sfoglia il codice sorgente

move el selector resolution into $mount

Evan You 11 anni fa
parent
commit
2440562249

+ 8 - 0
src/api/lifecycle.js

@@ -15,6 +15,14 @@ exports.$mount = function (el) {
     _.warn('$mount() should be called only once.')
     _.warn('$mount() should be called only once.')
     return
     return
   }
   }
+  if (typeof el === 'string') {
+    var selector = el
+    el = document.querySelector(el)
+    if (!el) {
+      _.warn('Cannot find element: ' + selector)
+      return
+    }
+  }
   this._compile(el)
   this._compile(el)
   this._isCompiled = true
   this._isCompiled = true
   this._callHook('compiled')
   this._callHook('compiled')

+ 3 - 9
src/compile/transclude.js

@@ -14,14 +14,6 @@ var templateParser = require('../parse/template')
  */
  */
 
 
 module.exports = function transclude (el, options) {
 module.exports = function transclude (el, options) {
-  var type = typeof el
-  if (type === 'string') {
-    var selector = el
-    el = document.querySelector(el)
-    if (!el) {
-      _.warn('Cannot find element: ' + selector)
-    }
-  }
   if (el instanceof DocumentFragment) {
   if (el instanceof DocumentFragment) {
     return transcludeBlock(el)
     return transcludeBlock(el)
   } else if (options.template) {
   } else if (options.template) {
@@ -152,7 +144,9 @@ var concat = [].concat
 function getOutlets (el) {
 function getOutlets (el) {
   return _.isArray(el)
   return _.isArray(el)
     ? concat.apply([], el.map(getOutlets))
     ? concat.apply([], el.map(getOutlets))
-    : _.toArray(el.querySelectorAll('content'))
+    : el.nodeType === 1
+      ? _.toArray(el.querySelectorAll('content'))
+      : []
 }
 }
 
 
 /**
 /**

+ 19 - 0
test/unit/specs/api/lifecycle_spec.js

@@ -27,6 +27,25 @@ if (_.inBrowser) {
         expect(el.__vue__).toBe(vm)
         expect(el.__vue__).toBe(vm)
         expect(el.textContent).toBe('hi!')
         expect(el.textContent).toBe('hi!')
       })
       })
+
+      it('selector', function () {
+        el.id = 'mount-test'
+        document.body.appendChild(el)
+        var vm = new Vue({
+          data: { test: 'hi!' }
+        })
+        vm.$mount('#mount-test')
+        expect(vm.$el).toBe(el)
+        expect(el.__vue__).toBe(vm)
+        expect(el.textContent).toBe('hi!')
+        document.body.removeChild(el)
+      })
+
+      it('warn invalid selector', function () {
+        var vm = new Vue()
+        vm.$mount('#none-exist')
+        expect(_.warn).toHaveBeenCalled()
+      })
       
       
       it('precompiled linker', function () {
       it('precompiled linker', function () {
         var linker = compile(el, Vue.options)
         var linker = compile(el, Vue.options)