Commit e98a74f353b7212d30146e7700944d65f5422188
struct: add extend option to set
Matt McKegg committed on 2/24/2017, 5:26:51 PMParent: e32aa3719b669ed0dd1f7ad2e1b86390c19ae61a
Files changed
struct.js | changed |
struct.js | ||
---|---|---|
@@ -1,14 +1,15 @@ | ||
1 | 1 … | var Value = require('./value') |
2 | 2 … | var LazyWatcher = require('./lib/lazy-watcher') |
3 | 3 … | var isSame = require('./lib/is-same') |
4 … | +var extend = require('xtend') | |
4 | 5 … | |
5 | 6 … | module.exports = Struct |
6 | 7 … | |
7 | 8 … | var blackList = { |
8 | 9 … | 'length': 'Clashes with `Function.prototype.length`.\n', |
9 | 10 … | 'name': 'Clashes with `Function.prototype.name`\n', |
10 | - 'destroy': '`destroy` is a reserved key of struct\n' | |
11 … | + 'set': '`set` is a reserved key of struct\n' | |
11 | 12 … | } |
12 | 13 … | |
13 | 14 … | function Struct (properties, opts) { |
14 | 15 … | var object = Object.create({}) |
@@ -43,13 +44,18 @@ | ||
43 | 44 … | object[key] = obs() |
44 | 45 … | observable[key] = obs |
45 | 46 … | }) |
46 | 47 … | |
47 | - observable.set = function (values) { | |
48 … | + observable.set = function (values, opts) { | |
48 | 49 … | var lastValue = suspendBroadcast |
50 … | + | |
49 | 51 … | suspendBroadcast = true |
50 | 52 … | values = values || {} |
51 | 53 … | |
54 … | + if (opts && opts.merge) { | |
55 … | + values = extend(object, values) | |
56 … | + } | |
57 … | + | |
52 | 58 … | // update inner observables |
53 | 59 … | keys.forEach(function (key) { |
54 | 60 … | if (observable[key]() !== values[key]) { |
55 | 61 … | observable[key].set(values[key]) |
Built with git-ssb-web