Skip to content

Commit 0f1600f

Browse files
authored
Merge pull request #148 from canjs/major
Merge major branch for 6.0 release
2 parents ed42007 + b65b63c commit 0f1600f

17 files changed

+594
-902
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ before_script:
55
- sleep 2
66
script: npm run ci
77
addons:
8-
firefox: '53.0'
8+
firefox: latest
99
sauce_connect: true
1010
dist: xenial
1111
services:

can-view-live.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
2-
var live = require("./lib/core");
3-
require("./lib/attr");
4-
require("./lib/attrs");
5-
require("./lib/html");
6-
require("./lib/list");
7-
require("./lib/text");
2+
var live = {};
3+
live.attr = require("./lib/attr");
4+
live.attrs = require("./lib/attrs");
5+
live.html = require("./lib/html");
6+
live.list = require("./lib/list");
7+
live.text = require("./lib/text");
88

99

1010
module.exports = live;

lib/attr.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"use strict";
2-
var live = require('./core');
32
var canReflect = require('can-reflect');
4-
var queues = require("can-queues");
53
var attr = require("can-attribute-observable/behaviors");
4+
var helpers = require('./helpers');
65

76
/**
87
* @function can-view-live.attr attr
@@ -29,31 +28,21 @@ var attr = require("can-attribute-observable/behaviors");
2928
* This listens for the changes in the observable and uses those changes to
3029
* set the specified attribute.
3130
*/
32-
live.attr = function(el, attributeName, compute) {
33-
function liveUpdateAttr(newVal) {
34-
queues.domUIQueue.enqueue(attr.set, attr, [el, attributeName, newVal]);
35-
}
31+
module.exports = function(el, attributeName, compute) {
32+
var handlerName = "";
3633
//!steal-remove-start
3734
if(process.env.NODE_ENV !== 'production') {
3835
// register that the handler changes the parent element
39-
canReflect.assignSymbols(liveUpdateAttr, {
40-
"can.getChangesDependencyRecord": function() {
41-
var s = new Set();
42-
s.add(el);
43-
return {
44-
valueDependencies: s
45-
};
46-
}
47-
});
48-
Object.defineProperty(liveUpdateAttr, "name", {
49-
value: "live.attr update::"+canReflect.getName(compute),
50-
});
36+
handlerName = "live.attr update::"+canReflect.getName(compute);
5137
}
5238
//!steal-remove-end
5339

54-
// Bind a single attribute on an element to a compute
55-
live.listen(el, compute, liveUpdateAttr);
56-
57-
// do initial set of attribute as well
58-
attr.set(el, attributeName, canReflect.getValue(compute));
40+
new helpers.ListenUntilRemovedAndInitialize(compute,
41+
function liveUpdateAttr(newVal) {
42+
attr.set(el,attributeName, newVal);
43+
},
44+
el,
45+
"dom",
46+
handlerName
47+
);
5948
};

lib/attrs.js

Lines changed: 45 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,72 @@
11
"use strict";
22
// This provides live binding for stache attributes.
3-
var live = require('./core');
43
var viewCallbacks = require('can-view-callbacks');
5-
var domMutate = require('can-dom-mutate');
64
var domMutateNode = require('can-dom-mutate/node');
75
var canReflect = require('can-reflect');
8-
var canReflectDeps = require('can-reflect-dependencies');
96

10-
live.attrs = function(el, compute, scope, options) {
7+
var helpers = require('./helpers');
118

9+
module.exports = function(el, compute, scope, options) {
10+
var handlerName = "";
1211
if (!canReflect.isObservableLike(compute)) {
1312
// Non-live case (`compute` was not a compute):
1413
// set all attributes on the element and don't
1514
// worry about setting up live binding since there
1615
// is not compute to bind on.
17-
var attrs = live.getAttributeParts(compute);
16+
var attrs = helpers.getAttributeParts(compute);
1817
for (var name in attrs) {
1918
domMutateNode.setAttribute.call(el, name, attrs[name]);
2019
}
2120
return;
2221
}
2322

24-
// last set of attributes
25-
var oldAttrs = {};
26-
27-
// set up a callback for handling changes when the compute
28-
// changes
29-
function liveAttrsUpdate(newVal) {
30-
var newAttrs = live.getAttributeParts(newVal),
31-
name;
32-
for (name in newAttrs) {
33-
var newValue = newAttrs[name],
34-
// `oldAttrs` was set on the last run of setAttrs in this context
35-
// (for this element and compute)
36-
oldValue = oldAttrs[name];
37-
// Only fire a callback
38-
// if the value of the attribute has changed
39-
if (newValue !== oldValue) {
40-
// set on DOM attributes (dispatches an "attributes" event as well)
41-
domMutateNode.setAttribute.call(el, name, newValue);
42-
// get registered callback for attribute name and fire
43-
var callback = viewCallbacks.attr(name);
44-
if (callback) {
45-
callback(el, {
46-
attributeName: name,
47-
scope: scope,
48-
options: options
49-
});
50-
}
51-
}
52-
// remove key found in new attrs from old attrs
53-
delete oldAttrs[name];
54-
}
55-
// any attrs left at this point are not set on the element now,
56-
// so remove them.
57-
for (name in oldAttrs) {
58-
domMutateNode.removeAttribute.call(el, name);
59-
}
60-
oldAttrs = newAttrs;
61-
}
62-
6323
//!steal-remove-start
6424
if(process.env.NODE_ENV !== 'production') {
65-
// register that the handler changes the parent element
66-
canReflect.assignSymbols(liveAttrsUpdate, {
67-
"can.getChangesDependencyRecord": function() {
68-
var s = new Set();
69-
s.add(el);
70-
return {
71-
valueDependencies: s
72-
};
73-
}
74-
});
75-
76-
Object.defineProperty(liveAttrsUpdate, "name", {
77-
value: "live.attrs update::"+canReflect.getName(compute),
78-
});
79-
canReflectDeps.addMutatedBy(el, compute);
25+
handlerName = "live.attrs update::"+canReflect.getName(compute);
8026
}
8127
//!steal-remove-end
8228

83-
// set attributes on any change to the compute
84-
canReflect.onValue(compute, liveAttrsUpdate,"domUI");
8529

86-
var removalDisposal;
87-
var teardownHandler = function() {
88-
canReflect.offValue(compute, liveAttrsUpdate,"domUI");
89-
if (removalDisposal) {
90-
removalDisposal();
91-
removalDisposal = undefined;
92-
}
30+
// last set of attributes
31+
var oldAttrs = {};
9332

94-
//!steal-remove-start
95-
if(process.env.NODE_ENV !== 'production') {
96-
canReflectDeps.deleteMutatedBy(el, compute);
97-
}
98-
//!steal-remove-end
99-
};
100-
// unbind on element removal
101-
removalDisposal = domMutate.onNodeRemoval(el, function () {
102-
var doc = el.ownerDocument;
103-
var ownerNode = doc.contains ? doc : doc.documentElement;
104-
if (!ownerNode.contains(el)) {
105-
teardownHandler();
106-
}
107-
});
10833

109-
// set up a current attribute set and assign to oldAttrs
110-
liveAttrsUpdate(canReflect.getValue(compute));
34+
new helpers.ListenUntilRemovedAndInitialize(compute,
35+
function canViewLive_updateAttributes(newVal) {
36+
var newAttrs = helpers.getAttributeParts(newVal),
37+
name;
38+
for (name in newAttrs) {
39+
var newValue = newAttrs[name],
40+
// `oldAttrs` was set on the last run of setAttrs in this context
41+
// (for this element and compute)
42+
oldValue = oldAttrs[name];
43+
// Only fire a callback
44+
// if the value of the attribute has changed
45+
if (newValue !== oldValue) {
46+
// set on DOM attributes (dispatches an "attributes" event as well)
47+
domMutateNode.setAttribute.call(el, name, newValue);
48+
// get registered callback for attribute name and fire
49+
var callback = viewCallbacks.attr(name);
50+
if (callback) {
51+
callback(el, {
52+
attributeName: name,
53+
scope: scope,
54+
options: options
55+
});
56+
}
57+
}
58+
// remove key found in new attrs from old attrs
59+
delete oldAttrs[name];
60+
}
61+
// any attrs left at this point are not set on the element now,
62+
// so remove them.
63+
for (name in oldAttrs) {
64+
domMutateNode.removeAttribute.call(el, name);
65+
}
66+
oldAttrs = newAttrs;
67+
},
68+
el,
69+
"dom",
70+
handlerName);
71+
11172
};

0 commit comments

Comments
 (0)