Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$onContentRefresh for custom attributes #314

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions hsp/rt.js
Expand Up @@ -214,6 +214,7 @@ var customAttributesRegistry = [];
* - $setValue(name, stringValue): called each time the attribute value changed, including when the initial value is set.
* - $setValueFromExp(name, expresionValues): called each time the attribute is refreshed, including when the initial value is set.
* - $onAttributesRefresh(): called at the end of the attributes'refresh process, i.e. once all attributes have their new value.
* - $onContentRefresh(): called when the content of the node holding the custom attribute has been refreshed.
* - $handleEvent(event): called when an event for which the custom attribute registered for is fired.
* - $dispose(): used to dispose the handler instance.
* It is instanciated on each element node with one of the custom attributes.
Expand Down
27 changes: 27 additions & 0 deletions hsp/rt/eltnode.js
Expand Up @@ -111,6 +111,24 @@ var EltNode = klass({
TNode.$dispose.call(this);
},

/**
* Create a node instance referencing the current node as base class Create as well the DOM element that will be
* appended to the parent node DOM element
* @return {TNode} the new node instance
*/
createNodeInstance : function (parent) {
var ni = TNode.createNodeInstance.call(this, parent);
if (ni._allCustAttrHandlers) {
for (var i = 0; i < ni._allCustAttrHandlers.length; i++) {
var handler = ni._allCustAttrHandlers[i].instance;
if (handler.$onContentRefresh) {
handler.$onContentRefresh();
}
}
}
return ni;
},

/**
* Create the DOM node element
*/
Expand Down Expand Up @@ -240,11 +258,20 @@ var EltNode = klass({
* Refresh the node
*/
refresh : function () {
var cdirtybackup = this.cdirty;
if (this.adirty) {
// attributes are dirty
this.refreshAttributes();
}
TNode.refresh.call(this);
if (cdirtybackup && this._allCustAttrHandlers) {
for (var i = 0; i < this._allCustAttrHandlers.length; i++) {
var handler = this._allCustAttrHandlers[i].instance;
if (handler.$onContentRefresh) {
handler.$onContentRefresh();
}
}
}
},

/**
Expand Down
34 changes: 34 additions & 0 deletions test/rt/customAttributes.spec.hsp
@@ -1,6 +1,7 @@
<script>
var hsp = require("hsp/rt");
var klass = klass=require("hsp/klass");
var $set = require('hsp/$set');
</script>

<template test1()>
Expand Down Expand Up @@ -52,6 +53,12 @@ var klass = klass=require("hsp/klass");
</div>
</template>

<template test8(d)>
<div style="height: 20px" childca>
<span>{d.value}</span>
</div>
</template>

<script>
describe("Custom attributes", function () {

Expand Down Expand Up @@ -117,10 +124,18 @@ describe("Custom attributes", function () {
}
});

var handleContentCount = 0;
var customChildHandler = klass({
$onContentRefresh: function() {
handleContentCount++;
}
});

hsp.registerCustomAttributes("verybigdiv", customHandler, 2);
hsp.registerCustomAttributes("customheightdiv", customHandler);
hsp.registerCustomAttributes(["customone", "customtwo"], groupHandler);
hsp.registerCustomAttributes("customButton", customButtonHandler, 0, ["button"]);
hsp.registerCustomAttributes("childca", customChildHandler);

it("tests a simple custom attribute", function () {
var n = test1();
Expand Down Expand Up @@ -207,5 +222,24 @@ describe("Custom attributes", function () {
n.$dispose();
expect(status).to.equal("Clean");
});

it("tests content changes", function() {
handleContentCount = 0;
var data = {
value: "a"
}
var n = test8(data);
var content = n.node.querySelector("span");
expect(handleContentCount).to.equal(1);
expect(content.innerHTML).to.equal("a");

$set(data, "value", "b");
hsp.refresh();

expect(handleContentCount).to.equal(2);
expect(content.innerHTML).to.equal("b");

n.$dispose();
});
});
</script>