Skip to content

Instantly share code, notes, and snippets.

@kotobukid
Created November 3, 2012 15:51
Show Gist options
  • Save kotobukid/4007710 to your computer and use it in GitHub Desktop.
Save kotobukid/4007710 to your computer and use it in GitHub Desktop.
//Backbone.js, underscore.js, jQueryに依存
//Backboneグローバルオブジェクトを名前空間とする
if (!window.Backbone) {
window.Backbone = function () {
"use strict";
};
}
(function (Backbone) {
"use strict";
var TabControl, _, $;
_ = window._;
$ = window.jQuery;
TabControl = Backbone.View.extend({ //タブの動作を一元管理するクラス
initialize: function (options) {
var root, settings;
root = this;
settings = _.extend({
globalNavigated: function () {},
globalNavigate: function () {},
headers: '.tabs' //ここに渡した要素のなかにあるa要素をタブのヘッダーとみなす
}, options || {});
this.setElement(settings.el);
this.globalNavigated = settings.globalNavigated;
this.globalNavigate = settings.globalNavigate;
this.headers = settings.headers;
this.$(this.headers + ' a').live('click', function (e) {
root.globalNavigate.call();
e.preventDefault();
$(e.currentTarget).tab('show');
root.globalNavigated.call();
return false;
});
console.log('tc initialized.');
},
test: function () {
console.log('Header > Content');
this.$('a', this.headers).each(function (index, elem) {
if ($($(elem).attr('href')).length === 1) {
console.log('\t' + ($(elem).attr('href')) + ' exists.');
} else {
console.log('Tab content invalid.');
}
});
console.log('Content > Header');
this.$('.tab-pane').each(function (index, elem) {
if ($('a[href="#' + $(elem).attr('id') + '"]').length === 1) {
console.log('\t' + $(elem).attr('id') + ' exists.');
} else {
console.log('Tab headers invalid.');
}
});
}
});
TabControl.prototype.open = function (options) {
var settings, result;
settings = _.extend({
el: 'body',
href : '',
navigate: function () {return true; },
navigated: function () {}
}, options || {});
result = settings.navigate.call();
if ((result) && (settings.href)) {
this.$('a[href="' + settings.href + '"]', this.headers).tab('show'); //this.headersで指定した要素の中にあるa要素にしか反応しないので要注意
this.$(settings.href).addClass('active');
settings.navigated.call();
}
};
Backbone.TabControl = TabControl;
}(window.Backbone));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment