|
1 | | -var Compiler = require('./compiler'), |
2 | | - def = require('./utils').defProtected |
| 1 | +var Compiler = require('./compiler'), |
| 2 | + def = require('./utils').defProtected, |
| 3 | + transition = require('./transition') |
3 | 4 |
|
4 | 5 | /** |
5 | 6 | * ViewModel exposed to the user that holds data, |
@@ -94,6 +95,56 @@ def(VMProto, '$emit', function () { |
94 | 95 | }) |
95 | 96 | }) |
96 | 97 |
|
| 98 | +// DOM convenience methods |
| 99 | + |
| 100 | +def(VMProto, '$appendTo', function (target) { |
| 101 | + target = query(target) |
| 102 | + var el = this.$el |
| 103 | + transition(el, 1, function () { |
| 104 | + target.appendChild(el) |
| 105 | + }, this.$compiler) |
| 106 | +}) |
| 107 | + |
| 108 | +def(VMProto, '$remove', function () { |
| 109 | + var el = this.$el, |
| 110 | + parent = el.parentNode |
| 111 | + if (!parent) return |
| 112 | + transition(el, -1, function () { |
| 113 | + parent.removeChild(el) |
| 114 | + }, this.$compiler) |
| 115 | +}) |
| 116 | + |
| 117 | +def(VMProto, '$before', function (target) { |
| 118 | + target = query(target) |
| 119 | + var el = this.$el, |
| 120 | + parent = target.parentNode |
| 121 | + if (!parent) return |
| 122 | + transition(el, 1, function () { |
| 123 | + parent.insertBefore(el, target) |
| 124 | + }, this.$compiler) |
| 125 | +}) |
| 126 | + |
| 127 | +def(VMProto, '$after', function (target) { |
| 128 | + target = query(target) |
| 129 | + var el = this.$el, |
| 130 | + parent = target.parentNode, |
| 131 | + next = target.nextSibling |
| 132 | + if (!parent) return |
| 133 | + transition(el, 1, function () { |
| 134 | + if (next) { |
| 135 | + parent.insertBefore(el, next) |
| 136 | + } else { |
| 137 | + parent.appendChild(el) |
| 138 | + } |
| 139 | + }, this.$compiler) |
| 140 | +}) |
| 141 | + |
| 142 | +function query (el) { |
| 143 | + return typeof el === 'string' |
| 144 | + ? document.querySelector(el) |
| 145 | + : el |
| 146 | +} |
| 147 | + |
97 | 148 | /** |
98 | 149 | * If a VM doesn't contain a path, go up the prototype chain |
99 | 150 | * to locate the ancestor that has it. |
|
0 commit comments