소스 검색

move el selector resolution into $mount

Evan You 11 년 전
부모
커밋
2440562249
3개의 변경된 파일30개의 추가작업 그리고 9개의 파일을 삭제
  1. 8 0
      src/api/lifecycle.js
  2. 3 9
      src/compile/transclude.js
  3. 19 0
      test/unit/specs/api/lifecycle_spec.js

+ 8 - 0
src/api/lifecycle.js

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

+ 3 - 9
src/compile/transclude.js

@@ -14,14 +14,6 @@ var templateParser = require('../parse/template')
  */
 
 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) {
     return transcludeBlock(el)
   } else if (options.template) {
@@ -152,7 +144,9 @@ var concat = [].concat
 function getOutlets (el) {
   return _.isArray(el)
     ? 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.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 () {
         var linker = compile(el, Vue.options)