fix undefined name bug

This commit is contained in:
lavrton 2015-02-08 07:39:43 +07:00
parent 5df69badbf
commit 264a4bab1c
4 changed files with 19 additions and 12 deletions

View File

@ -3718,7 +3718,8 @@ var Konva = {};
*/ */
addName : function(name) { addName : function(name) {
if (!this.hasName(name)) { if (!this.hasName(name)) {
var newName = this.name() + ' ' + name; var oldName = this.name();
var newName = oldName ? (oldName + ' ' + name) : name;
this.setName(newName); this.setName(newName);
} }
return this; return this;
@ -3735,7 +3736,7 @@ var Konva = {};
* node.hasName('selected'); // return false * node.hasName('selected'); // return false
*/ */
hasName : function(name) { hasName : function(name) {
var names = this.name().split(/\s/g); var names = (this.name() || '').split(/\s/g);
return names.indexOf(name) !== -1; return names.indexOf(name) !== -1;
}, },
/** /**
@ -3751,7 +3752,7 @@ var Konva = {};
* node.name(); // return 'red' * node.name(); // return 'red'
*/ */
removeName : function(name) { removeName : function(name) {
var names = this.name().split(/\s/g); var names = (this.name() || '').split(/\s/g);
var index = names.indexOf(name); var index = names.indexOf(name);
if (index !== -1) { if (index !== -1) {
names.splice(index, 1); names.splice(index, 1);

4
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1442,7 +1442,8 @@
*/ */
addName : function(name) { addName : function(name) {
if (!this.hasName(name)) { if (!this.hasName(name)) {
var newName = this.name() + ' ' + name; var oldName = this.name();
var newName = oldName ? (oldName + ' ' + name) : name;
this.setName(newName); this.setName(newName);
} }
return this; return this;
@ -1459,7 +1460,7 @@
* node.hasName('selected'); // return false * node.hasName('selected'); // return false
*/ */
hasName : function(name) { hasName : function(name) {
var names = this.name().split(/\s/g); var names = (this.name() || '').split(/\s/g);
return names.indexOf(name) !== -1; return names.indexOf(name) !== -1;
}, },
/** /**
@ -1475,7 +1476,7 @@
* node.name(); // return 'red' * node.name(); // return 'red'
*/ */
removeName : function(name) { removeName : function(name) {
var names = this.name().split(/\s/g); var names = (this.name() || '').split(/\s/g);
var index = names.indexOf(name); var index = names.indexOf(name);
if (index !== -1) { if (index !== -1) {
names.splice(index, 1); names.splice(index, 1);

View File

@ -1103,17 +1103,22 @@ suite('Node', function() {
radius: 70, radius: 70,
fill: 'green', fill: 'green',
stroke: 'black', stroke: 'black',
strokeWidth: 4, strokeWidth: 4
name: 'myCircle foo'
}); });
layer.add(circle); layer.add(circle);
stage.add(layer); stage.add(layer);
assert.equal(circle.getName(),'myCircle foo'); assert.equal(circle.getName(), undefined);
circle.addName('foo');
assert.equal(circle.getName(),'foo');
circle.addName('myCircle');
assert.equal(circle.getName(),'foo myCircle');
// add existing name // add existing name
circle.addName('foo'); circle.addName('foo');
assert.equal(circle.getName(),'myCircle foo'); assert.equal(circle.getName(),'foo myCircle');
// check hasName // check hasName
assert.equal(circle.hasName('myCircle'), true); assert.equal(circle.hasName('myCircle'), true);