Skip to content

Commit

Permalink
split Tabs of Tabs and WithAnchor
Browse files Browse the repository at this point in the history
  • Loading branch information
heymexa committed Jun 30, 2015
1 parent f3afded commit f9a6421
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 38 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"backbone-super": "~1.0.4",
"backbone.view.elements": "~1.0.1",
"backbone.factory": "~1.1.0",
"backbone.anchor": "~1.0.1"
"backbone.anchor": "~1.0.1",
"backbone.mix": "~1.0.1"
},
"devDependencies": {
"mocha": "~2.0.1",
Expand Down
35 changes: 2 additions & 33 deletions lib/Tabs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
define([
'backbone',
'Backbone.View.Elements',
'underscore',
'backbone.anchor/lib/anchor'
], function (Backbone, ElementsView, _, anchor) {
'underscore'
], function (Backbone, ElementsView, _) {
'use strict';

/**
Expand Down Expand Up @@ -73,35 +72,6 @@ define([

this.getName = _.once(this.getName);
this._initActiveTab();
this._linkWithAnchor();
},

/**
* @private
*/
_linkWithAnchor: function () {
var name = this.getName();
anchor.on('change:' + name, this._onHashChange, this);
if (anchor.has(name)) {
this._processAnchorChange(anchor.get(name));
}
},

/**
* @param {string} tabName
* @protected
*/
_processAnchorChange: function (tabName) {
this.show(tabName);
},

/**
* @param {Backbone.Model} model
* @param {string} tabName
* @private
*/
_onHashChange: function (model, tabName) {
this._processAnchorChange(tabName);
},

/**
Expand Down Expand Up @@ -140,7 +110,6 @@ define([
return this;
}
this._setActiveTab(name);
anchor.set(this.getName(), name);
return this;
},

Expand Down
11 changes: 7 additions & 4 deletions lib/TabsManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
define([
'underscore',
'backbone.factory/lib/SelectorsFactory',
'./Tabs'
], function (_, SelectorsFactory, Tabs) {
'./Tabs',
'./WithAnchor'
], function (_, SelectorsFactory, Tabs, WithAnchor) {
'use strict';

/**
Expand All @@ -17,7 +18,8 @@ define([
*/
_selectors: function () {
return _.defaults({
tabs: '.tabs'
tabs: '.tabs',
anchorTabs: '.tabs_with_anchor'

This comment has been minimized.

Copy link
@jifeon

jifeon Jul 2, 2015

Member

I think modifier should be with-anchor, not with with value anchor

This comment has been minimized.

Copy link
@apsavin

apsavin Jul 2, 2015

Contributor

May be, it's better to use .tabs_without-anchor for tabs without anchor and left all other tabs with just .tabs?

}, this._super());
},

Expand All @@ -28,7 +30,8 @@ define([
*/
_products: function () {
return _.defaults({
'*': Tabs
'*': Tabs,

This comment has been minimized.

Copy link
@jifeon

jifeon Jul 2, 2015

Member

I think tabs with anchor should be provided by default, else these are breaking changes. And we need use tabs with anchor more often

This comment has been minimized.

Copy link
@apsavin

apsavin Jul 2, 2015

Contributor

+1
Tabs without url modification - strange, rare situation.

anchorTabs: Tabs.mix(WithAnchor)
}, this._super());
},

Expand Down
63 changes: 63 additions & 0 deletions lib/WithAnchor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
define([
'backbone.mix',
'backbone.anchor/lib/anchor',
'backbone-super'
], function (Mixin, anchor) {
'use strict';

/**
* @mixin WithAnchors
* @extends Tabs
*/
var WithAnchors = new Mixin(/** @lends WithAnchors# */{

This comment has been minimized.

Copy link
@jifeon

jifeon Jul 2, 2015

Member

Anchor or Anchors?

/**
* @constructs
*/
initialize: function () {
this._super();

this._linkWithAnchor();
},

/**
* @private
*/
_linkWithAnchor: function () {
var name = this.getName();
anchor.on('change:' + name, this._onHashChange, this);
if (anchor.has(name)) {
this._processAnchorChange(anchor.get(name));
}
},

/**
* @param {string} tabName
* @protected
*/
_processAnchorChange: function (tabName) {
this.show(tabName);
},

/**
* @param {Backbone.Model} model
* @param {string} tabName
* @private
*/
_onHashChange: function (model, tabName) {
this._processAnchorChange(tabName);
},

/**
* @public
* @param {string} name
* @returns {Tabs} this
*/
show: function (name) {
this._super(name);

anchor.set(this.getName(), name);
return this;
}
});
return WithAnchors;
});

1 comment on commit f9a6421

@jifeon
Copy link
Member

@jifeon jifeon commented on f9a6421 Jul 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apsavin Could look at this too, please.

Please sign in to comment.