diff --git a/bower.json b/bower.json index 46c15b5..32a8e3c 100755 --- a/bower.json +++ b/bower.json @@ -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", diff --git a/lib/Tabs.js b/lib/Tabs.js index 936297d..fa44f36 100644 --- a/lib/Tabs.js +++ b/lib/Tabs.js @@ -1,9 +1,8 @@ define([ 'backbone', 'Backbone.View.Elements', - 'underscore', - 'backbone.anchor/lib/anchor' -], function (Backbone, ElementsView, _, anchor) { + 'underscore' +], function (Backbone, ElementsView, _) { 'use strict'; /** @@ -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); }, /** @@ -140,7 +110,6 @@ define([ return this; } this._setActiveTab(name); - anchor.set(this.getName(), name); return this; }, diff --git a/lib/TabsManager.js b/lib/TabsManager.js index 9f03b7c..10100e4 100644 --- a/lib/TabsManager.js +++ b/lib/TabsManager.js @@ -1,8 +1,9 @@ define([ 'underscore', 'backbone.factory/lib/SelectorsFactory', - './Tabs' -], function (_, SelectorsFactory, Tabs) { + './Tabs', + './WithAnchor' +], function (_, SelectorsFactory, Tabs, WithAnchor) { 'use strict'; /** @@ -17,7 +18,8 @@ define([ */ _selectors: function () { return _.defaults({ - tabs: '.tabs' + tabs: '.tabs', + anchorTabs: '.tabs_with_anchor' }, this._super()); }, @@ -28,7 +30,8 @@ define([ */ _products: function () { return _.defaults({ - '*': Tabs + '*': Tabs, + anchorTabs: Tabs.mix(WithAnchor) }, this._super()); }, diff --git a/lib/WithAnchor.js b/lib/WithAnchor.js new file mode 100644 index 0000000..34057d6 --- /dev/null +++ b/lib/WithAnchor.js @@ -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# */{ + /** + * @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; +});