Skip to content

Commit 4f1c643

Browse files
committed
Merge pull request #8 from vitkarpov/fix-#5
#5: find
2 parents 8434bfa + 3aeb87d commit 4f1c643

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

dist/jblocks.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ var methods = {
125125
},
126126
/**
127127
* Returns block from cache or create it if doesn't exist
128-
* @return {Block} block
128+
* @return {jQuery}
129129
*/
130130
'get': function () {
131131
return this.map(function () {
@@ -144,6 +144,19 @@ var methods = {
144144

145145
return block;
146146
});
147+
},
148+
/**
149+
* Returns blocks inside
150+
* @return {jQuery}
151+
*/
152+
'find': function(filter) {
153+
var selector = '[data-b]';
154+
if (filter) {
155+
selector = '[data-b="' + filter + '"]';
156+
}
157+
return this.find(selector).map(function() {
158+
return $(this).jblocks('get')[0];
159+
});
147160
}
148161
};
149162

@@ -173,6 +186,7 @@ $.jblocks = function (proto) {
173186

174187
// to get Block from global outspace
175188
$.Block = Block;
189+
176190
},{"./Block":1,"./helpers":3}],3:[function(require,module,exports){
177191
var id = 0;
178192

src/jblocks.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var methods = {
1818
},
1919
/**
2020
* Returns block from cache or create it if doesn't exist
21-
* @return {Block} block
21+
* @return {jQuery}
2222
*/
2323
'get': function () {
2424
return this.map(function () {
@@ -37,6 +37,19 @@ var methods = {
3737

3838
return block;
3939
});
40+
},
41+
/**
42+
* Returns blocks inside
43+
* @return {jQuery}
44+
*/
45+
'find': function(filter) {
46+
var selector = '[data-b]';
47+
if (filter) {
48+
selector = '[data-b="' + filter + '"]';
49+
}
50+
return this.find(selector).map(function() {
51+
return $(this).jblocks('get')[0];
52+
});
4053
}
4154
};
4255

@@ -65,4 +78,4 @@ $.jblocks = function (proto) {
6578

6679

6780
// to get Block from global outspace
68-
$.Block = Block;
81+
$.Block = Block;

tests/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<div id="mocha"></div>
1919

2020
<script type="text/javascript">
21+
$.jblocks({
22+
name: 'bar'
23+
});
2124
$.jblocks({
2225
name: 'counter',
2326
events: {

tests/tests.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var html = [
33
'<button class="js-inc">+</button>',
44
'<button class="js-dec">-</button>',
55
'<span class="js-info">0</span>',
6-
'</div>'
6+
'</div>',
7+
'<div class="bar" data-b="bar"></div>'
78
].join();
89

910
describe('jblocks', function() {
@@ -41,6 +42,18 @@ describe('jblocks', function() {
4142
});
4243
});
4344

45+
describe('#find', function() {
46+
it('should find blocks inside', function() {
47+
$(document).jblocks('find').each(function() {
48+
this.should.be.an.instanceOf($.Block);
49+
});
50+
});
51+
it('should use filter as second arg', function() {
52+
var blocks = $(document).jblocks('find', 'counter');
53+
blocks.length.should.be.eql(1);
54+
});
55+
});
56+
4457
describe('#initBlocks', function() {
4558
it('should init blocks', function() {
4659
$(document).jblocks('init');

0 commit comments

Comments
 (0)