Skip to content

Commit f9a6421

Browse files
committed
split Tabs of Tabs and WithAnchor
1 parent f3afded commit f9a6421

4 files changed

Lines changed: 74 additions & 38 deletions

File tree

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"backbone-super": "~1.0.4",
3232
"backbone.view.elements": "~1.0.1",
3333
"backbone.factory": "~1.1.0",
34-
"backbone.anchor": "~1.0.1"
34+
"backbone.anchor": "~1.0.1",
35+
"backbone.mix": "~1.0.1"
3536
},
3637
"devDependencies": {
3738
"mocha": "~2.0.1",

lib/Tabs.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
define([
22
'backbone',
33
'Backbone.View.Elements',
4-
'underscore',
5-
'backbone.anchor/lib/anchor'
6-
], function (Backbone, ElementsView, _, anchor) {
4+
'underscore'
5+
], function (Backbone, ElementsView, _) {
76
'use strict';
87

98
/**
@@ -73,35 +72,6 @@ define([
7372

7473
this.getName = _.once(this.getName);
7574
this._initActiveTab();
76-
this._linkWithAnchor();
77-
},
78-
79-
/**
80-
* @private
81-
*/
82-
_linkWithAnchor: function () {
83-
var name = this.getName();
84-
anchor.on('change:' + name, this._onHashChange, this);
85-
if (anchor.has(name)) {
86-
this._processAnchorChange(anchor.get(name));
87-
}
88-
},
89-
90-
/**
91-
* @param {string} tabName
92-
* @protected
93-
*/
94-
_processAnchorChange: function (tabName) {
95-
this.show(tabName);
96-
},
97-
98-
/**
99-
* @param {Backbone.Model} model
100-
* @param {string} tabName
101-
* @private
102-
*/
103-
_onHashChange: function (model, tabName) {
104-
this._processAnchorChange(tabName);
10575
},
10676

10777
/**
@@ -140,7 +110,6 @@ define([
140110
return this;
141111
}
142112
this._setActiveTab(name);
143-
anchor.set(this.getName(), name);
144113
return this;
145114
},
146115

lib/TabsManager.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
define([
22
'underscore',
33
'backbone.factory/lib/SelectorsFactory',
4-
'./Tabs'
5-
], function (_, SelectorsFactory, Tabs) {
4+
'./Tabs',
5+
'./WithAnchor'
6+
], function (_, SelectorsFactory, Tabs, WithAnchor) {
67
'use strict';
78

89
/**
@@ -17,7 +18,8 @@ define([
1718
*/
1819
_selectors: function () {
1920
return _.defaults({
20-
tabs: '.tabs'
21+
tabs: '.tabs',
22+
anchorTabs: '.tabs_with_anchor'
2123
}, this._super());
2224
},
2325

@@ -28,7 +30,8 @@ define([
2830
*/
2931
_products: function () {
3032
return _.defaults({
31-
'*': Tabs
33+
'*': Tabs,
34+
anchorTabs: Tabs.mix(WithAnchor)
3235
}, this._super());
3336
},
3437

lib/WithAnchor.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
define([
2+
'backbone.mix',
3+
'backbone.anchor/lib/anchor',
4+
'backbone-super'
5+
], function (Mixin, anchor) {
6+
'use strict';
7+
8+
/**
9+
* @mixin WithAnchors
10+
* @extends Tabs
11+
*/
12+
var WithAnchors = new Mixin(/** @lends WithAnchors# */{
13+
/**
14+
* @constructs
15+
*/
16+
initialize: function () {
17+
this._super();
18+
19+
this._linkWithAnchor();
20+
},
21+
22+
/**
23+
* @private
24+
*/
25+
_linkWithAnchor: function () {
26+
var name = this.getName();
27+
anchor.on('change:' + name, this._onHashChange, this);
28+
if (anchor.has(name)) {
29+
this._processAnchorChange(anchor.get(name));
30+
}
31+
},
32+
33+
/**
34+
* @param {string} tabName
35+
* @protected
36+
*/
37+
_processAnchorChange: function (tabName) {
38+
this.show(tabName);
39+
},
40+
41+
/**
42+
* @param {Backbone.Model} model
43+
* @param {string} tabName
44+
* @private
45+
*/
46+
_onHashChange: function (model, tabName) {
47+
this._processAnchorChange(tabName);
48+
},
49+
50+
/**
51+
* @public
52+
* @param {string} name
53+
* @returns {Tabs} this
54+
*/
55+
show: function (name) {
56+
this._super(name);
57+
58+
anchor.set(this.getName(), name);
59+
return this;
60+
}
61+
});
62+
return WithAnchors;
63+
});

0 commit comments

Comments
 (0)