Skip to content

Commit

Permalink
added conditional subscription exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim de Beer committed Feb 26, 2016
1 parent 7ed2ec3 commit dee73b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/element/index.js
Expand Up @@ -115,7 +115,9 @@ var Element = module.exports = new Observable({
var dstamp = this._datarender && data && data._lstamp
var force = this._force || !dstamp && this._datarender || this._forceUpdates
if (this.$) {
console.error('????', this)
if (this._subscriptionCondition) {
console.log('!!!!!')
if (!this._subscriptionCondition.call(this, data, event, prev)) {
return
}
Expand Down
26 changes: 15 additions & 11 deletions lib/element/map.js
@@ -1,6 +1,7 @@
'use strict'
var setWithPath = require('vigour-util/setwithpath')
var isNumber = require('vigour-util/is/number')
var isPlain = require('vigour-util/is/plainobj')
exports.properties = {
_subscriptionCondition: true,
storedmap: true,
Expand All @@ -10,19 +11,22 @@ exports.properties = {
this.$ = null
return
}

if (isPlain(val)) {
if (!val.val) {
throw new Error('$ needs val.val')
}
val = val.val
if (val.condition) {
this._subscriptionCondition = val.condition
} else if (this._subscriptionCondition) {
delete this._subscriptionCondition
}
}

this.__input = void 0
if (val !== true && !(val instanceof Array)) {
if (typeof val === 'object') {
if (!val.val) {
throw new Error('$ needs val.val')
}
val = val.val
if (val.condition) {
this._subscriptionCondition = val.condition
} else if (this._subscriptionCondition) {
delete this._subscriptionCondition
}
} else if (!val) {
if (!val || typeof val === 'object') {
throw new Error('wrong type for .$ pass a string, number or Array')
}
if (isNumber(val)) {
Expand Down
19 changes: 8 additions & 11 deletions test/index.js
Expand Up @@ -118,21 +118,18 @@ test('conditional subscription', function (t) {
var app = e({
components: {
title: {
text: {
$: {
val: 'title',
condition (data, key) {
// lets go!
console.log(data, key, this.path)
}
$: {
val: true,
condition (data, key) {
// lets go!
console.log(data, key, this.path)
}
}
},
text: { $: 'title' }
}
},
DOM: fakeDom,
title: {
type: 'title'
},
title: { type: 'title' },
val: new Data({
title: 'hello'
})
Expand Down

0 comments on commit dee73b8

Please sign in to comment.