Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 8e8cce4

Browse files
committed
having each pageVw update their state after a dom reattach
1 parent 29bcde7 commit 8e8cce4

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed

js/router.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ module.exports = Backbone.Router.extend({
158158
},
159159

160160
execute: function(callback, args, name) {
161+
console.log(`boom: ${Backbone.history.getFragment()}`);
162+
161163
if (this.historyAction == 'default') {
162164
this.historyPosition += 1;
163165
this.historySize = this.historyPosition;
@@ -259,15 +261,12 @@ module.exports = Backbone.Router.extend({
259261

260262
if (cached && (now - cached.cachedAt < cached.view.cacheExpires)) {
261263
// we have an un-expired cached view, let's reattach it
264+
console.log('using cached');
262265
this.view = cached.view;
263266

264267
$('#content').html(this.view.$el);
265268
this.view.delegateEvents();
266269

267-
if (this.view.restoreScrollPosition) {
268-
this.$obContainer[0].scrollTop = this.view.__cachedScrollPos || 0;
269-
}
270-
271270
// if (this.view.__cachedAddressBarText) {
272271
// // ensure our address bar text reflects the state of the cached view
273272
// window.obEventBus.trigger('setAddressBar', {'addressText': this.view.__cachedAddressBarText});
@@ -279,7 +278,17 @@ module.exports = Backbone.Router.extend({
279278
view: this.view,
280279
route: requestedRoute
281280
});
281+
282+
if (this.view.restoreScrollPosition &&
283+
this.view.restoreScrollPosition.call(this.view, { route: requestedRoute })) {
284+
console.log('restore scroll');
285+
this.$obContainer[0].scrollTop = this.view.__cachedScrollPos || 0;
286+
} else {
287+
console.log('scroll top');
288+
this.$obContainer[0].scrollTop = 0;
289+
}
282290
} else {
291+
console.log('brand spanking new');
283292
this.view = new (Function.prototype.bind.apply(View, [null].concat(options.viewArgs)));
284293
// // clear address bar. Must happen after we set out view above.
285294
// window.obEventBus.trigger('setAddressBar', options.addressBarText);
@@ -369,6 +378,8 @@ module.exports = Backbone.Router.extend({
369378
},
370379

371380
home: function(state, searchText){
381+
!state && this.navigate('home/products', { replace: true });
382+
372383
this.newView(homeView, {
373384
viewArgs: {
374385
userModel: this.userModel,

js/views/homeVw.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,34 @@ module.exports = pageVw.extend({
7575
this.listenTo(app.router, 'cache-reattach', this.onCacheReattach);
7676
},
7777

78+
restoreScrollPosition: function(opts) {
79+
var splitRoute = opts.route.split('/'),
80+
routeSearchText = splitRoute[2] || '',
81+
cachedSearchText = this.searchItemsText || '';
82+
83+
if (splitRoute[1] === this.state) {
84+
return true;
85+
}
86+
},
87+
7888
onCacheReattach: function(e) {
7989
var splitRoute = e.route.split('/'),
80-
searchText;
90+
state;
8191

8292
if (e.view !== this) return;
8393

84-
if (splitRoute.length > 2 && (searchText = splitRoute[2]) !== this.searchItemsText) {
85-
// our view is cached to a search term different than the one the
86-
// user is searching for
87-
this.searchItems(searchText);
88-
this.obContainer[0].scrollTop = 0;
94+
if (splitRoute.length > 1) {
95+
// if our routed state doesn't equal our state, we'll
96+
// reset the scroll position.
97+
splitRoute[1] !== this.state && $('#obContainer').scrollTop(0);
98+
99+
this.setState(splitRoute[1]);
100+
splitRoute.length > 2 && splitRoute[2] !== this.searchItemsText &&
101+
this.searchItems(splitRoute[2]);
89102
}
90103

91104
this.obContainer.on('scroll', this.scrollHandler);
92-
},
105+
},
93106

94107
onCacheDetach: function(e) {
95108
if (e.view !== this) return;

js/views/pageVw.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@ var BaseVw = require('./baseVw'),
55

66
PageVw = BaseVw.extend({
77
cacheExpires: 1000 * 60 * 20,
8-
restoreScrollPosition: true
8+
9+
// should be function that returns a boolean
10+
restoreScrollPosition: function(opts) {
11+
var splitRoute = opts.route.split('/');
12+
13+
// !!!! if state will not be in the second position
14+
// in the route of your view (e.g. myPage/<state>) or
15+
// your view does not store it's state as a string in
16+
// this.state, then overwrite this method.
17+
18+
if (splitRoute[1] === this.state) {
19+
// if our routed state equals our cached state, we would
20+
// like to restore the cached scroll position.
21+
return true;
22+
}
23+
}
924
});
1025

1126
// this must be a "static" method and overridden as such (if you

js/views/settingsVw.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = pageVw.extend({
9292
this.firstLoadModerators = true;
9393

9494
this.listenTo(window.obEventBus, 'saveCurrentForm', function(){
95-
switch (self._state) {
95+
switch (self.state) {
9696
case 'general':
9797
self.saveGeneral();
9898
break;
@@ -121,8 +121,34 @@ module.exports = pageVw.extend({
121121
this.moderatorCount = 0;
122122

123123
this.fetchModel();
124+
125+
this.$obContainer = $('#obContainer');
126+
},
127+
128+
onCacheReattach: function(e) {
129+
var splitRoute = e.route.split('/'),
130+
state;
131+
132+
if (e.view !== this) return;
133+
134+
this.blockedUsersScrollHandler &&
135+
this.$obContainer.on('scroll', this.blockedUsersScrollHandler);
136+
137+
if (splitRoute.length > 1) {
138+
// if our routed state doesn't equal our state, we'll
139+
// reset the scroll position.
140+
splitRoute[1] !== this.state && $('#obContainer').scrollTop(0);
141+
this.setState(splitRoute[1]);
142+
}
124143
},
125144

145+
onCacheDetach: function(e) {
146+
if (e.view !== this) return;
147+
148+
this.blockedUsersScrollHandler &&
149+
this.$obContainer.off('scroll', this.blockedUsersScrollHandler);
150+
},
151+
126152
fetchModel: function(){
127153
var self = this;
128154
this.firstLoadModerators = true;
@@ -346,13 +372,12 @@ module.exports = pageVw.extend({
346372
});
347373

348374
// implement scroll based lazy loading of blocked users
349-
this.$obContainer = this.$obContainer || $('#obContainer');
350375
this.blockedUsersScrollHandler && this.$obContainer.off('scroll', this.blockedUsersScrollHandler);
351376
this.blockedUsersScrollHandler = __.throttle(function() {
352377
var colLen;
353378

354379
if (
355-
self.getState() === 'blocked' &&
380+
self.state === 'blocked' &&
356381
blockedUsersCl.length < getBlockedGuids().length &&
357382
domUtils.isScrolledIntoView(self.$lazyLoadTrigger[0], self.$obContainer[0])
358383
) {
@@ -552,7 +577,6 @@ module.exports = pageVw.extend({
552577
addTabToHistory: function(state){
553578
//add action to history
554579
Backbone.history.navigate("#settings/" + state, { replace: true });
555-
this.options.state = state;
556580
},
557581

558582
setTab: function(activeTab, showContent){
@@ -564,7 +588,7 @@ module.exports = pageVw.extend({
564588

565589
setState: function(state){
566590
if (state){
567-
this._state = state;
591+
this.state = state;
568592
this.setTab(this.$('.js-' + state + 'Tab'), this.$('.js-' + state));
569593

570594
if (state == "store") {
@@ -595,15 +619,11 @@ module.exports = pageVw.extend({
595619
}
596620
}
597621
} else {
598-
this._state = "general";
622+
this.state = "general";
599623
this.setTab(this.$('.js-generalTab'), this.$('.js-general'));
600624
}
601625
},
602626

603-
getState: function() {
604-
return this._state;
605-
},
606-
607627
tabClick: function(e){
608628
var tab = $(e.target).data('tab');
609629
this.setState(tab);
@@ -1100,9 +1120,8 @@ module.exports = pageVw.extend({
11001120
remove: function() {
11011121
this.blockedUsersVw && this.blockedUsersVw.remove();
11021122

1103-
if (this.blockedUsersScrollHandler && this.$obContainer.length) {
1123+
this.blockedUsersScrollHandler &&
11041124
this.$obContainer.off('scroll', this.blockedUsersScrollHandler);
1105-
}
11061125

11071126
this.serverConnectSyncHandler &&
11081127
app.serverConfigs.getActive().off(null, this.serverConnectSyncHandler);

js/views/userPageVw.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ UserPageVw = pageVw.extend({
330330
this.listenTo(app.router, 'cache-reattach', this.onCacheReattach);
331331
},
332332

333+
restoreScrollPosition: function(opts) {
334+
var splitRoute = opts.route.split('/');
335+
336+
if (splitRoute[2] === this.state) {
337+
return true;
338+
}
339+
},
340+
333341
onCacheReattach: function(e) {
334342
var splitRoute = e.route.split('/'),
335343
state;
@@ -341,10 +349,12 @@ UserPageVw = pageVw.extend({
341349
$('#obContainer').on('scroll', this.onScroll);
342350
this.setCustomStyles();
343351

344-
if (splitRoute.length > 2 && splitRoute[2] !== this.state) {
352+
if (splitRoute.length > 2) {
353+
// if our routed state doesn't equal our state, we'll
354+
// reset the scroll position.
355+
splitRoute[2] !== this.state && $('#obContainer').scrollTop(0);
345356
this.setState(splitRoute[2], splitRoute[3], { replaceState: true });
346357
}
347-
348358
},
349359

350360
onCacheDetach: function(e) {

0 commit comments

Comments
 (0)