Skip to content

Commit

Permalink
added: setValueAt and clear for array
Browse files Browse the repository at this point in the history
fixed: it get the knot event handler from the value instead of target
  • Loading branch information
alexzhaosheng committed Jul 13, 2015
1 parent 37e53d7 commit 07bf399
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
55 changes: 25 additions & 30 deletions src/core/ArrayMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
hookArrayMethod("sort");
hookArrayMethod("reverse");

var _arrayVersion = 0;
function increaseArrayVersion(array){
array.__knot_arrayVersion = _arrayVersion++;

if(!isFinite(_arrayVersion)) {
_arrayVersion = 0;
}
}

Array.prototype.notifyChanged = function (removedIndexes, addedIndexes) {
if(this.__knot_attachedData) {
var arrayChangedInfo = null;
Expand All @@ -42,6 +51,17 @@
}
};

Array.prototype.setValueAt = function(index, value){
this[index] = value;
if(this.__knot_attachedData) {
increaseArrayVersion(this);
__private.DataObserver.notifyDataChanged(this, "*", null, null, {removed:[index], added:[index]});
}
};
Array.prototype.clear = function(){
this.splice(0, this.length);
};


_originalArrayMethods.push = Array.prototype.push;
Array.prototype.push = function () {
Expand All @@ -52,12 +72,7 @@
for(var i=0; i<arguments.length;i++){
added.push(oldLength + i);
}
if(!this.__knot_arrayVersion){
this.__knot_arrayVersion = 1;
}
else{
this.__knot_arrayVersion++;
}
increaseArrayVersion(this);
__private.DataObserver.notifyDataChanged(this, "*", null, null, {removed:[], added:added});
__private.DataObserver.notifyDataChanged(this, "length", oldLength, this.length, {property:"length"});
}
Expand All @@ -72,12 +87,7 @@
for(var i=0; i<arguments.length;i++){
added.push(i);
}
if(!this.__knot_arrayVersion){
this.__knot_arrayVersion = 1;
}
else{
this.__knot_arrayVersion++;
}
increaseArrayVersion(this);
__private.DataObserver.notifyDataChanged(this, "*", null, null, {removed:[], added:added});
__private.DataObserver.notifyDataChanged(this, "length", oldLength, this.length, {property:"length"});
}
Expand All @@ -89,12 +99,7 @@
var oldLength = this.length;
var ret = _originalArrayMethods.pop.apply(this, arguments);
if(this.__knot_attachedData) {
if(!this.__knot_arrayVersion){
this.__knot_arrayVersion = 1;
}
else{
this.__knot_arrayVersion++;
}
increaseArrayVersion(this);
__private.DataObserver.notifyDataChanged(this, "*", null, null, {removed:[oldLength-1], added:[]});
__private.DataObserver.notifyDataChanged(this, "length", oldLength, this.length, {property:"length"});
}
Expand All @@ -105,12 +110,7 @@
var oldLength = this.length;
var ret = _originalArrayMethods.shift.apply(this, arguments);
if(this.__knot_attachedData) {
if(!this.__knot_arrayVersion){
this.__knot_arrayVersion = 1;
}
else{
this.__knot_arrayVersion++;
}
increaseArrayVersion(this);
__private.DataObserver.notifyDataChanged(this, "*", null, null, {removed:[0], added:[]});
__private.DataObserver.notifyDataChanged(this, "length", oldLength, this.length, {property:"length"});
}
Expand All @@ -129,12 +129,7 @@
for(i=start; i<start+arguments.length-2; i++){
added.push(i);
}
if(!this.__knot_arrayVersion){
this.__knot_arrayVersion = 1;
}
else{
this.__knot_arrayVersion++;
}
increaseArrayVersion(this);
__private.DataObserver.notifyDataChanged(this, "*", null, null, {removed:removed, added:added});
__private.DataObserver.notifyDataChanged(this, "length", oldLength, this.length, {property:"length"});
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/HTMLAPProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@

var raiseEvent = function(childNode, evt){
if(options && options[evt]){
var f = __private.Utility.getValueOnPath(value, options[evt]);
var f = __private.Utility.getValueOnPath(target, options[evt]);
try{
f.apply(target, [childNode, value]);
f.apply(target, [childNode, __private.HTMLKnotBuilder.getOnNodeDataContext(value)]);
}
catch(err) {
__private.Log.warning("Raise " + evt + " event failed.", err);
Expand Down Expand Up @@ -226,7 +226,7 @@

var raiseEvent = function(childNode, value, evt){
if(options && options[evt]){
var f = __private.Utility.getValueOnPath(value, options[evt]);
var f = __private.Utility.getValueOnPath(node, options[evt]);
try{
f.apply(node, [childNode, value]);
}
Expand All @@ -243,7 +243,7 @@
var removed = additionalInfo.removed;
for(i=removed.length-1; i >= 0; i--){
n = node.children[removed[i]];
raiseEvent(n, null, "@removing");
raiseEvent(n, __private.HTMLKnotBuilder.getOnNodeDataContext(n), "@removing");
removeNodeCreatedFromTemplate(n);
raiseEvent(n, null, "@removed");
}
Expand Down Expand Up @@ -302,7 +302,7 @@

for (i = node.children.length - 1; i >= values.length; i--) {
n = node.children[i];
raiseEvent(n, null, "@removing");
raiseEvent(n, __private.HTMLKnotBuilder.getOnNodeDataContext(n), "@removing");
removeNodeCreatedFromTemplate(n);
raiseEvent(n, null, "@removed");
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/KnotInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@
return __private.GlobalSymbolHelper.registerNamedSymbol(name, value);
},

//register the factory method for a component
registerComponent: function(name, factory){
return __private.HTMLKnotBuilder.registerComponent(name, factory);
},

//get the component object on the HTML element (if there is)
getComponentObject: function(node){
return __private.HTMLKnotBuilder.getComponentObject(node);
},

//load the private package. The resource in package is accessed with tempalte
loadPrivatePackage: function(url){
return __private.CBSLoader.loadCBSPackage(url);
}
};

Expand Down
8 changes: 8 additions & 0 deletions test/ArrayMonitor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,13 @@

testArray.splice(1, 1);
assert.equal(areArraysTheSame(), true, "removed and added is properly set");

testArray.setValueAt(2, "abc");
assert.equal(areArraysTheSame(), true, "setValueAt works");
assert.equal(testArray[2], "abc", "setValueAt works");

testArray.clear();
assert.equal(areArraysTheSame(), true, "clear works");
assert.equal(testArray.length, 0, "clear works");
});
})(window);

0 comments on commit 07bf399

Please sign in to comment.