Skip to content

Commit

Permalink
nextTick phantomjs fix, unit tests for batcher, config() api addition
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 24, 2013
1 parent 72e8e73 commit c7b2d9c
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 77 deletions.
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"src/deps-parser.js",
"src/filters.js",
"src/transition.js",
"src/batch.js",
"src/batcher.js",
"src/directives/index.js",
"src/directives/if.js",
"src/directives/repeat.js",
Expand Down
2 changes: 1 addition & 1 deletion src/batch.js → src/batcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.queue = function (binding, method) {
has[binding.id] = true
if (!waiting) {
waiting = true
setTimeout(flush, 0)
utils.nextTick(flush)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/binding.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var batch = require('./batch'),
var batcher = require('./batcher'),
id = 0

/**
Expand Down Expand Up @@ -28,7 +28,7 @@ var BindingProto = Binding.prototype
*/
BindingProto.update = function (value) {
this.value = value
batch.queue(this, 'update')
batcher.queue(this, 'update')
}

BindingProto._update = function () {
Expand All @@ -44,7 +44,7 @@ BindingProto._update = function () {
* Force all instances to re-evaluate themselves
*/
BindingProto.refresh = function () {
batch.queue(this, 'refresh')
batcher.queue(this, 'refresh')
}

BindingProto._refresh = function () {
Expand Down
47 changes: 36 additions & 11 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
module.exports = {

prefix : 'v',
async : true,
debug : false,
silent : false,
enterClass : 'v-enter',
leaveClass : 'v-leave',
attrs : {}

}
var prefix = 'v',
specialAttributes = [
'pre',
'text',
'repeat',
'partial',
'component',
'component-id',
'transition'
],
config = module.exports = {

async : true,
debug : false,
silent : false,
enterClass : 'v-enter',
leaveClass : 'v-leave',
attrs : {},

get prefix () {
return prefix
},
set prefix (val) {
prefix = val
updatePrefix()
}

}

function updatePrefix () {
specialAttributes.forEach(function (attr) {
config.attrs[attr] = prefix + '-' + attr
})
}

updatePrefix()
34 changes: 8 additions & 26 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ var config = require('./config'),
/**
* Set config options
*/
ViewModel.config = function (opts) {
if (opts) {
ViewModel.config = function (opts, val) {
if (typeof opts === 'string') {
if (val === undefined) {
return config[opts]
} else {
config[opts] = val
}
} else {
utils.extend(config, opts)
if (opts.prefix) updatePrefix()
}
return this
}
Expand Down Expand Up @@ -151,27 +156,4 @@ function mergeHook (fn, parentFn) {
}
}

/**
* Update prefix for some special directives
* that are used in compilation.
*/
var specialAttributes = [
'pre',
'text',
'repeat',
'partial',
'component',
'component-id',
'transition'
]

function updatePrefix () {
specialAttributes.forEach(setPrefix)
}

function setPrefix (attr) {
config.attrs[attr] = config.prefix + '-' + attr
}

updatePrefix()
module.exports = ViewModel
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ var config = require('./config'),
console = window.console,
ViewModel // late def

// PhantomJS doesn't support rAF, yet it has the global
// variable exposed. Use setTimeout so tests can work.
var defer = navigator.userAgent.indexOf('PhantomJS') > -1
? window.setTimeout
: (window.webkitRequestAnimationFrame ||
window.requestAnimationFrame ||
window.setTimeout)

/**
* Create a prototype-less object
* which is a better hash/map
Expand Down Expand Up @@ -180,5 +188,12 @@ var utils = module.exports = {
if (!config.silent && console) {
console.warn(join.call(arguments, ' '))
}
},

/**
* used to defer batch updates
*/
nextTick: function (cb) {
defer(cb, 0)
}
}
11 changes: 6 additions & 5 deletions src/viewmodel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var Compiler = require('./compiler'),
utils = require('./utils'),
transition = require('./transition'),
def = utils.defProtected
def = utils.defProtected,
nextTick = utils.nextTick

/**
* ViewModel exposed to the user that holds data,
Expand Down Expand Up @@ -103,7 +104,7 @@ def(VMProto, '$appendTo', function (target, cb) {
var el = this.$el
transition(el, 1, function () {
target.appendChild(el)
if (cb) setTimeout(cb, 0)
if (cb) nextTick(cb)
}, this.$compiler)
})

Expand All @@ -113,7 +114,7 @@ def(VMProto, '$remove', function (cb) {
if (!parent) return
transition(el, -1, function () {
parent.removeChild(el)
if (cb) setTimeout(cb, 0)
if (cb) nextTick(cb)
}, this.$compiler)
})

Expand All @@ -124,7 +125,7 @@ def(VMProto, '$before', function (target, cb) {
if (!parent) return
transition(el, 1, function () {
parent.insertBefore(el, target)
if (cb) setTimeout(cb, 0)
if (cb) nextTick(cb)
}, this.$compiler)
})

Expand All @@ -140,7 +141,7 @@ def(VMProto, '$after', function (target, cb) {
} else {
parent.appendChild(el)
}
if (cb) setTimeout(cb, 0)
if (cb) nextTick(cb)
}, this.$compiler)
})

Expand Down
1 change: 1 addition & 0 deletions test/unit/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<script src="specs/api.js"></script>
<script src="specs/viewmodel.js"></script>
<script src="specs/transition.js"></script>
<script src="specs/batcher.js"></script>
<script>
if (navigator.userAgent.indexOf('PhantomJS') < 0) {
mocha.run(Cover.report)
Expand Down
43 changes: 27 additions & 16 deletions test/unit/specs/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
describe('UNIT: API', function () {

var utils = require('vue/src/utils'),
nextTick = utils.nextTick

describe('config()', function () {

var config = require('vue/src/config')

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

it('should get', function () {
assert.strictEqual(Vue.config('debug'), false)
})

it('should set', function () {
Vue.config('test', 1)
assert.strictEqual(config.test, 1)
})

after(function () {
Vue.config({
prefix: 'v'
Expand Down Expand Up @@ -109,8 +123,7 @@ describe('UNIT: API', function () {
className: 'hihi',
data: { hi: 'ok' }
},
Test = Vue.extend(opts),
utils = require('vue/src/utils')
Test = Vue.extend(opts)

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

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

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

var testId = 'api-trans-test',
transition = {},
utils = require('vue/src/utils')
transition = {}

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

t.show = true
setTimeout(function () {
nextTick(function () {
assert.ok(enterCalled)
assert.strictEqual(t.$el.style.display, '')
t.show = false
setTimeout(function () {
nextTick(function () {
assert.ok(leaveCalled)
assert.strictEqual(t.$el.style.display, 'none')
t.$destroy()
done()
}, 0)
}, 0)
})
})
})

})
Expand Down Expand Up @@ -509,10 +520,10 @@ describe('UNIT: API', function () {
})
assert.strictEqual(t.$el.innerHTML, 'YES')
t.ok = false
setTimeout(function () {
nextTick(function () {
assert.strictEqual(t.$el.innerHTML, 'NO')
done()
}, 0)
})
})

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

t.show = true
setTimeout(function () {
nextTick(function () {
assert.ok(enterCalled)
assert.strictEqual(t.$el.style.display, '')
t.show = false
setTimeout(function () {
nextTick(function () {
assert.ok(leaveCalled)
assert.strictEqual(t.$el.style.display, 'none')
t.$destroy()
done()
}, 0)
}, 0)
})
})

})

Expand Down

0 comments on commit c7b2d9c

Please sign in to comment.