Skip to content

Commit c7b2d9c

Browse files
committed
nextTick phantomjs fix, unit tests for batcher, config() api addition
1 parent 72e8e73 commit c7b2d9c

12 files changed

+200
-77
lines changed

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"src/deps-parser.js",
2121
"src/filters.js",
2222
"src/transition.js",
23-
"src/batch.js",
23+
"src/batcher.js",
2424
"src/directives/index.js",
2525
"src/directives/if.js",
2626
"src/directives/repeat.js",

src/batch.js renamed to src/batcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.queue = function (binding, method) {
1717
has[binding.id] = true
1818
if (!waiting) {
1919
waiting = true
20-
setTimeout(flush, 0)
20+
utils.nextTick(flush)
2121
}
2222
}
2323
}

src/binding.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var batch = require('./batch'),
1+
var batcher = require('./batcher'),
22
id = 0
33

44
/**
@@ -28,7 +28,7 @@ var BindingProto = Binding.prototype
2828
*/
2929
BindingProto.update = function (value) {
3030
this.value = value
31-
batch.queue(this, 'update')
31+
batcher.queue(this, 'update')
3232
}
3333

3434
BindingProto._update = function () {
@@ -44,7 +44,7 @@ BindingProto._update = function () {
4444
* Force all instances to re-evaluate themselves
4545
*/
4646
BindingProto.refresh = function () {
47-
batch.queue(this, 'refresh')
47+
batcher.queue(this, 'refresh')
4848
}
4949

5050
BindingProto._refresh = function () {

src/config.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1-
module.exports = {
2-
3-
prefix : 'v',
4-
async : true,
5-
debug : false,
6-
silent : false,
7-
enterClass : 'v-enter',
8-
leaveClass : 'v-leave',
9-
attrs : {}
10-
11-
}
1+
var prefix = 'v',
2+
specialAttributes = [
3+
'pre',
4+
'text',
5+
'repeat',
6+
'partial',
7+
'component',
8+
'component-id',
9+
'transition'
10+
],
11+
config = module.exports = {
12+
13+
async : true,
14+
debug : false,
15+
silent : false,
16+
enterClass : 'v-enter',
17+
leaveClass : 'v-leave',
18+
attrs : {},
19+
20+
get prefix () {
21+
return prefix
22+
},
23+
set prefix (val) {
24+
prefix = val
25+
updatePrefix()
26+
}
27+
28+
}
29+
30+
function updatePrefix () {
31+
specialAttributes.forEach(function (attr) {
32+
config.attrs[attr] = prefix + '-' + attr
33+
})
34+
}
35+
36+
updatePrefix()

src/main.js

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ var config = require('./config'),
77
/**
88
* Set config options
99
*/
10-
ViewModel.config = function (opts) {
11-
if (opts) {
10+
ViewModel.config = function (opts, val) {
11+
if (typeof opts === 'string') {
12+
if (val === undefined) {
13+
return config[opts]
14+
} else {
15+
config[opts] = val
16+
}
17+
} else {
1218
utils.extend(config, opts)
13-
if (opts.prefix) updatePrefix()
1419
}
1520
return this
1621
}
@@ -151,27 +156,4 @@ function mergeHook (fn, parentFn) {
151156
}
152157
}
153158

154-
/**
155-
* Update prefix for some special directives
156-
* that are used in compilation.
157-
*/
158-
var specialAttributes = [
159-
'pre',
160-
'text',
161-
'repeat',
162-
'partial',
163-
'component',
164-
'component-id',
165-
'transition'
166-
]
167-
168-
function updatePrefix () {
169-
specialAttributes.forEach(setPrefix)
170-
}
171-
172-
function setPrefix (attr) {
173-
config.attrs[attr] = config.prefix + '-' + attr
174-
}
175-
176-
updatePrefix()
177159
module.exports = ViewModel

src/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ var config = require('./config'),
55
console = window.console,
66
ViewModel // late def
77

8+
// PhantomJS doesn't support rAF, yet it has the global
9+
// variable exposed. Use setTimeout so tests can work.
10+
var defer = navigator.userAgent.indexOf('PhantomJS') > -1
11+
? window.setTimeout
12+
: (window.webkitRequestAnimationFrame ||
13+
window.requestAnimationFrame ||
14+
window.setTimeout)
15+
816
/**
917
* Create a prototype-less object
1018
* which is a better hash/map
@@ -180,5 +188,12 @@ var utils = module.exports = {
180188
if (!config.silent && console) {
181189
console.warn(join.call(arguments, ' '))
182190
}
191+
},
192+
193+
/**
194+
* used to defer batch updates
195+
*/
196+
nextTick: function (cb) {
197+
defer(cb, 0)
183198
}
184199
}

src/viewmodel.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var Compiler = require('./compiler'),
22
utils = require('./utils'),
33
transition = require('./transition'),
4-
def = utils.defProtected
4+
def = utils.defProtected,
5+
nextTick = utils.nextTick
56

67
/**
78
* ViewModel exposed to the user that holds data,
@@ -103,7 +104,7 @@ def(VMProto, '$appendTo', function (target, cb) {
103104
var el = this.$el
104105
transition(el, 1, function () {
105106
target.appendChild(el)
106-
if (cb) setTimeout(cb, 0)
107+
if (cb) nextTick(cb)
107108
}, this.$compiler)
108109
})
109110

@@ -113,7 +114,7 @@ def(VMProto, '$remove', function (cb) {
113114
if (!parent) return
114115
transition(el, -1, function () {
115116
parent.removeChild(el)
116-
if (cb) setTimeout(cb, 0)
117+
if (cb) nextTick(cb)
117118
}, this.$compiler)
118119
})
119120

@@ -124,7 +125,7 @@ def(VMProto, '$before', function (target, cb) {
124125
if (!parent) return
125126
transition(el, 1, function () {
126127
parent.insertBefore(el, target)
127-
if (cb) setTimeout(cb, 0)
128+
if (cb) nextTick(cb)
128129
}, this.$compiler)
129130
})
130131

@@ -140,7 +141,7 @@ def(VMProto, '$after', function (target, cb) {
140141
} else {
141142
parent.appendChild(el)
142143
}
143-
if (cb) setTimeout(cb, 0)
144+
if (cb) nextTick(cb)
144145
}, this.$compiler)
145146
})
146147

test/unit/runner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<script src="specs/api.js"></script>
4848
<script src="specs/viewmodel.js"></script>
4949
<script src="specs/transition.js"></script>
50+
<script src="specs/batcher.js"></script>
5051
<script>
5152
if (navigator.userAgent.indexOf('PhantomJS') < 0) {
5253
mocha.run(Cover.report)

test/unit/specs/api.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
describe('UNIT: API', function () {
22

3+
var utils = require('vue/src/utils'),
4+
nextTick = utils.nextTick
5+
36
describe('config()', function () {
7+
8+
var config = require('vue/src/config')
49

510
it('should work when changing prefix', function () {
611
var testId = 'config-1'
@@ -15,6 +20,15 @@ describe('UNIT: API', function () {
1520
assert.strictEqual(document.querySelector('#' + testId + ' span').innerHTML, testId)
1621
})
1722

23+
it('should get', function () {
24+
assert.strictEqual(Vue.config('debug'), false)
25+
})
26+
27+
it('should set', function () {
28+
Vue.config('test', 1)
29+
assert.strictEqual(config.test, 1)
30+
})
31+
1832
after(function () {
1933
Vue.config({
2034
prefix: 'v'
@@ -109,8 +123,7 @@ describe('UNIT: API', function () {
109123
className: 'hihi',
110124
data: { hi: 'ok' }
111125
},
112-
Test = Vue.extend(opts),
113-
utils = require('vue/src/utils')
126+
Test = Vue.extend(opts)
114127

115128
it('should register a Component constructor', function () {
116129
Vue.component(testId, Test)
@@ -146,8 +159,7 @@ describe('UNIT: API', function () {
146159
describe('partial()', function () {
147160

148161
var testId = 'api-partial-test',
149-
partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>',
150-
utils = require('vue/src/utils')
162+
partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>'
151163

152164
it('should register the partial as a dom fragment', function () {
153165
Vue.partial(testId, partial)
@@ -189,8 +201,7 @@ describe('UNIT: API', function () {
189201
describe('transition()', function () {
190202

191203
var testId = 'api-trans-test',
192-
transition = {},
193-
utils = require('vue/src/utils')
204+
transition = {}
194205

195206
it('should register a transition object', function () {
196207
Vue.transition(testId, transition)
@@ -230,17 +241,17 @@ describe('UNIT: API', function () {
230241
document.body.appendChild(t.$el)
231242

232243
t.show = true
233-
setTimeout(function () {
244+
nextTick(function () {
234245
assert.ok(enterCalled)
235246
assert.strictEqual(t.$el.style.display, '')
236247
t.show = false
237-
setTimeout(function () {
248+
nextTick(function () {
238249
assert.ok(leaveCalled)
239250
assert.strictEqual(t.$el.style.display, 'none')
240251
t.$destroy()
241252
done()
242-
}, 0)
243-
}, 0)
253+
})
254+
})
244255
})
245256

246257
})
@@ -509,10 +520,10 @@ describe('UNIT: API', function () {
509520
})
510521
assert.strictEqual(t.$el.innerHTML, 'YES')
511522
t.ok = false
512-
setTimeout(function () {
523+
nextTick(function () {
513524
assert.strictEqual(t.$el.innerHTML, 'NO')
514525
done()
515-
}, 0)
526+
})
516527
})
517528

518529
})
@@ -635,17 +646,17 @@ describe('UNIT: API', function () {
635646
document.body.appendChild(t.$el)
636647

637648
t.show = true
638-
setTimeout(function () {
649+
nextTick(function () {
639650
assert.ok(enterCalled)
640651
assert.strictEqual(t.$el.style.display, '')
641652
t.show = false
642-
setTimeout(function () {
653+
nextTick(function () {
643654
assert.ok(leaveCalled)
644655
assert.strictEqual(t.$el.style.display, 'none')
645656
t.$destroy()
646657
done()
647-
}, 0)
648-
}, 0)
658+
})
659+
})
649660

650661
})
651662

0 commit comments

Comments
 (0)