Add "navigate" to the baseview to reduce boilerplate.
This commit is contained in:
Родитель
9b316b6255
Коммит
907f2596fc
|
@ -28,7 +28,7 @@ function (_, Backbone, jQuery, Session) {
|
|||
// If the user must be authenticated and they are not, send
|
||||
// them to the signin screen.
|
||||
if (this.mustAuth && ! Session.sessionToken) {
|
||||
this.router.navigate('signin', { trigger: true });
|
||||
this.navigate('signin');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -152,6 +152,13 @@ function (_, Backbone, jQuery, Session) {
|
|||
if (event.which === ENTER_BUTTON_CODE) {
|
||||
window.history.back();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* navigate to another screen
|
||||
*/
|
||||
navigate: function (page) {
|
||||
this.router.navigate(page, { trigger: true });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ function (_, BaseView, Template, FxaClient, Session, Url, PasswordMixin) {
|
|||
service: Url.searchParam('service'),
|
||||
redirectTo: Url.searchParam('redirectTo')
|
||||
});
|
||||
router.navigate('reset_password_complete', { trigger: true });
|
||||
this.navigate('reset_password_complete');
|
||||
},
|
||||
|
||||
_onResetCompleteFailure: function (err) {
|
||||
|
|
|
@ -45,7 +45,7 @@ function (_, BaseView, Template, Session, FxaClient, PasswordMixin) {
|
|||
var self = this;
|
||||
client.deleteAccount(email, password)
|
||||
.then(function () {
|
||||
self.router.navigate('signup', { trigger: true });
|
||||
self.navigate('signup');
|
||||
})
|
||||
.done(null, function (err) {
|
||||
self.displayError(err.message);
|
||||
|
|
|
@ -48,7 +48,7 @@ function (_, BaseView, Template, FxaClient) {
|
|||
},
|
||||
|
||||
_onRequestResetSuccess: function () {
|
||||
router.navigate('confirm_reset_password', { trigger: true });
|
||||
this.navigate('confirm_reset_password');
|
||||
},
|
||||
|
||||
_onRequestResetFailure: function (err) {
|
||||
|
|
|
@ -39,7 +39,7 @@ function (_, BaseView, Template, FxaClient, Session) {
|
|||
var self = this;
|
||||
client.signOut()
|
||||
.then(function () {
|
||||
self.router.navigate('signin', { trigger: true });
|
||||
self.navigate('signin');
|
||||
}, function (err) {
|
||||
self.displayError(err.msg || err.message);
|
||||
});
|
||||
|
|
|
@ -41,14 +41,15 @@ function (_, BaseView, SignInTemplate, Session, FxaClient, PasswordMixin) {
|
|||
var password = this.$('.password').val();
|
||||
|
||||
var client = new FxaClient();
|
||||
var self = this;
|
||||
client.signIn(email, password)
|
||||
.then(function (accountData) {
|
||||
if (accountData.verified) {
|
||||
router.navigate('settings', { trigger: true });
|
||||
self.navigate('settings');
|
||||
} else {
|
||||
return client.signUpResend()
|
||||
.then(function () {
|
||||
router.navigate('confirm', { trigger: true });
|
||||
self.navigate('confirm');
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
|
@ -32,14 +32,13 @@ function (_, BaseView, Template, Session, FxaClient, PasswordMixin, Url) {
|
|||
|
||||
initialize: function (options) {
|
||||
options = options || {};
|
||||
this.router = options.router || window.router;
|
||||
|
||||
this.service = Url.searchParam('service');
|
||||
},
|
||||
|
||||
beforeRender: function () {
|
||||
if (document.cookie.indexOf('tooyoung') > -1) {
|
||||
this.router.navigate('cannot_create_account', { trigger: true });
|
||||
this.navigate('cannot_create_account');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -115,7 +114,7 @@ function (_, BaseView, Template, Session, FxaClient, PasswordMixin, Url) {
|
|||
// hangs around like a bad smell.
|
||||
document.cookie = 'tooyoung=1;';
|
||||
|
||||
this.router.navigate('cannot_create_account', { trigger: true });
|
||||
this.navigate('cannot_create_account');
|
||||
},
|
||||
|
||||
_createAccount: function () {
|
||||
|
@ -126,7 +125,7 @@ function (_, BaseView, Template, Session, FxaClient, PasswordMixin, Url) {
|
|||
var client = new FxaClient();
|
||||
client.signUp(email, password, customizeSync)
|
||||
.done(_.bind(function () {
|
||||
this.router.navigate('confirm', { trigger: true });
|
||||
this.navigate('confirm');
|
||||
}, this),
|
||||
_.bind(function (err) {
|
||||
this.displayError(err.message);
|
||||
|
|
|
@ -12,26 +12,32 @@ define([
|
|||
'views/base',
|
||||
'lib/translator',
|
||||
'stache!templates/test_template',
|
||||
'../../mocks/dom-event'
|
||||
'../../mocks/dom-event',
|
||||
'../../mocks/router'
|
||||
],
|
||||
function (mocha, chai, jQuery, BaseView, Translator, Template, DOMEventMock) {
|
||||
function (mocha, chai, jQuery, BaseView, Translator, Template, DOMEventMock, RouterMock) {
|
||||
/*global describe, beforeEach, afterEach, it*/
|
||||
var assert = chai.assert;
|
||||
|
||||
describe('views/base', function () {
|
||||
var view;
|
||||
var view, router;
|
||||
|
||||
beforeEach(function () {
|
||||
translator = new Translator();
|
||||
translator.set({
|
||||
'the error message': 'a translated error message'
|
||||
});
|
||||
|
||||
router = new RouterMock();
|
||||
var View = BaseView.extend({
|
||||
template: Template,
|
||||
translator: translator
|
||||
template: Template
|
||||
});
|
||||
|
||||
view = new View({
|
||||
translator: translator,
|
||||
router: router
|
||||
});
|
||||
|
||||
view = new View();
|
||||
view.render();
|
||||
jQuery('body').append(view.el);
|
||||
});
|
||||
|
@ -41,6 +47,7 @@ function (mocha, chai, jQuery, BaseView, Translator, Template, DOMEventMock) {
|
|||
view.destroy();
|
||||
jQuery(view.el).remove();
|
||||
view = null;
|
||||
router = null;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -86,11 +93,19 @@ function (mocha, chai, jQuery, BaseView, Translator, Template, DOMEventMock) {
|
|||
});
|
||||
});
|
||||
|
||||
describe('BaseView.preventDefaultThen', function() {
|
||||
var event;
|
||||
describe('navigate', function () {
|
||||
it('navigates to a page', function (done) {
|
||||
router.on('navigate', function (newPage) {
|
||||
assert.equal(newPage, 'signin');
|
||||
done();
|
||||
});
|
||||
view.navigate('signin');
|
||||
});
|
||||
});
|
||||
|
||||
it('can take the name of a function as the name of the event handler', function(done) {
|
||||
view.eventHandler = function(event) {
|
||||
describe('BaseView.preventDefaultThen', function () {
|
||||
it('can take the name of a function as the name of the event handler', function (done) {
|
||||
view.eventHandler = function (event) {
|
||||
assert.isTrue(event.isDefaultPrevented());
|
||||
done();
|
||||
};
|
||||
|
@ -99,17 +114,17 @@ function (mocha, chai, jQuery, BaseView, Translator, Template, DOMEventMock) {
|
|||
backboneHandler.call(view, new DOMEventMock());
|
||||
});
|
||||
|
||||
it('can take a function as the event handler', function(done) {
|
||||
it('can take a function as the event handler', function (done) {
|
||||
function eventHandler(event) {
|
||||
assert.isTrue(event.isDefaultPrevented());
|
||||
done();
|
||||
};
|
||||
}
|
||||
|
||||
var backboneHandler = BaseView.preventDefaultThen(eventHandler);
|
||||
backboneHandler.call(view, new DOMEventMock());
|
||||
});
|
||||
|
||||
it('can take no arguments at all', function() {
|
||||
it('can take no arguments at all', function () {
|
||||
var backboneHandler = BaseView.preventDefaultThen();
|
||||
|
||||
var eventMock = new DOMEventMock();
|
||||
|
|
Загрузка…
Ссылка в новой задаче