forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewmodel.js
180 lines (162 loc) · 4.48 KB
/
viewmodel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var Compiler = require('./compiler'),
utils = require('./utils'),
transition = require('./transition'),
Batcher = require('./batcher'),
slice = [].slice,
def = utils.defProtected,
nextTick = utils.nextTick,
// batch $watch callbacks
watcherBatcher = new Batcher(),
watcherId = 1
/**
* ViewModel exposed to the user that holds data,
* computed properties, event handlers
* and a few reserved methods
*/
function ViewModel (options) {
// just compile. options are passed directly to compiler
new Compiler(this, options)
}
// All VM prototype methods are inenumerable
// so it can be stringified/looped through as raw data
var VMProto = ViewModel.prototype
/**
* Convenience function to get a value from
* a keypath
*/
def(VMProto, '$get', function (key) {
var val = utils.get(this, key)
return val === undefined && this.$parent
? this.$parent.$get(key)
: val
})
/**
* Convenience function to set an actual nested value
* from a flat key string. Used in directives.
*/
def(VMProto, '$set', function (key, value) {
utils.set(this, key, value)
})
/**
* watch a key on the viewmodel for changes
* fire callback with new value
*/
def(VMProto, '$watch', function (key, callback) {
// save a unique id for each watcher
var id = watcherId++,
self = this
function on () {
var args = slice.call(arguments)
watcherBatcher.push({
id: id,
override: true,
execute: function () {
callback.apply(self, args)
}
})
}
callback._fn = on
self.$compiler.observer.on('change:' + key, on)
})
/**
* unwatch a key
*/
def(VMProto, '$unwatch', function (key, callback) {
// workaround here
// since the emitter module checks callback existence
// by checking the length of arguments
var args = ['change:' + key],
ob = this.$compiler.observer
if (callback) args.push(callback._fn)
ob.off.apply(ob, args)
})
/**
* unbind everything, remove everything
*/
def(VMProto, '$destroy', function () {
this.$compiler.destroy()
})
/**
* broadcast an event to all child VMs recursively.
*/
def(VMProto, '$broadcast', function () {
var children = this.$compiler.children,
i = children.length,
child
while (i--) {
child = children[i]
child.emitter.applyEmit.apply(child.emitter, arguments)
child.vm.$broadcast.apply(child.vm, arguments)
}
})
/**
* emit an event that propagates all the way up to parent VMs.
*/
def(VMProto, '$dispatch', function () {
var compiler = this.$compiler,
emitter = compiler.emitter,
parent = compiler.parent
emitter.applyEmit.apply(emitter, arguments)
if (parent) {
parent.vm.$dispatch.apply(parent.vm, arguments)
}
})
/**
* delegate on/off/once to the compiler's emitter
*/
;['emit', 'on', 'off', 'once'].forEach(function (method) {
// internal emit has fixed number of arguments.
// exposed emit uses the external version
// with fn.apply.
var realMethod = method === 'emit'
? 'applyEmit'
: method
def(VMProto, '$' + method, function () {
var emitter = this.$compiler.emitter
emitter[realMethod].apply(emitter, arguments)
})
})
// DOM convenience methods
def(VMProto, '$appendTo', function (target, cb) {
target = query(target)
var el = this.$el
transition(el, 1, function () {
target.appendChild(el)
if (cb) nextTick(cb)
}, this.$compiler)
})
def(VMProto, '$remove', function (cb) {
var el = this.$el
transition(el, -1, function () {
if (el.parentNode) {
el.parentNode.removeChild(el)
}
if (cb) nextTick(cb)
}, this.$compiler)
})
def(VMProto, '$before', function (target, cb) {
target = query(target)
var el = this.$el
transition(el, 1, function () {
target.parentNode.insertBefore(el, target)
if (cb) nextTick(cb)
}, this.$compiler)
})
def(VMProto, '$after', function (target, cb) {
target = query(target)
var el = this.$el
transition(el, 1, function () {
if (target.nextSibling) {
target.parentNode.insertBefore(el, target.nextSibling)
} else {
target.parentNode.appendChild(el)
}
if (cb) nextTick(cb)
}, this.$compiler)
})
function query (el) {
return typeof el === 'string'
? document.querySelector(el)
: el
}
module.exports = ViewModel