Skip to content

Commit

Permalink
Merge pull request ericdrowell#1033 from pronebird/master
Browse files Browse the repository at this point in the history
Add findOne() convenience method on Container
  • Loading branch information
lavrton committed Dec 13, 2014
2 parents 8083b8f + 41e0466 commit ab67dc9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@

return Kinetic.Collection.toCollection(retArr);
},
findOne: function(selector) {
return this.find(selector)[0];
},
_getNodeById: function(key) {
var node = Kinetic.ids[key];

Expand Down
15 changes: 15 additions & 0 deletions test/unit/Container-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,21 @@ suite('Container', function() {
layer.draw();
});

// ======================================================
test('test findOne() selector by quering existing and non-existing nodes by name', function () {
var stage = addStage();
var layer = new Kinetic.Layer();

var rect = new Kinetic.Rect({
name: 'rect'
});

layer.add(rect);

assert.equal(layer.findOne('.rect'), rect, 'findOne() on existing node must return a single node.');
assert.equal(layer.findOne('.doesNotExist'), undefined, 'findOne() on non-existing node must return undefined.');
});

// ======================================================
test('add layer then shape', function() {
var stage = addStage();
Expand Down

0 comments on commit ab67dc9

Please sign in to comment.