Clicking the #site-logo will refresh the latest list.

This commit is contained in:
Robin Ward 2013-07-04 17:31:06 -04:00
parent 4a3bc1fb43
commit 2d821d1ff4
5 changed files with 113 additions and 39 deletions

View File

@ -13,18 +13,6 @@ Discourse.URL = Em.Object.createWithMixins({
// Used for matching a /more URL
MORE_REGEXP: /\/more$/,
/**
@private
Get a handle on the application's router. Note that currently it uses `__container__` which is not
advised but there is no other way to access the router.
@method router
**/
router: function() {
return Discourse.__container__.lookup('router:main');
}.property(),
/**
Browser aware replaceState. Will only be invoked if the browser supports it.
@ -70,11 +58,65 @@ Discourse.URL = Em.Object.createWithMixins({
path = path.replace(rootURL, '');
}
/*
If the URL is in the topic form, /t/something/:topic_id/:post_number
then we want to apply some special logic. If the post_number changes within the
same topic, use replaceState and instruct our controller to load more posts.
*/
// TODO: Extract into rules we can inject into the URL handler
if (this.navigatedToHome(oldPath, path)) { return; }
if (this.navigatedToListMore(oldPath, path)) { return; }
if (this.navigatedToHome(oldPath, path)) { return; }
// Be wary of looking up the router. In this case, we have links in our
// HTML, say form compiled markdown posts, that need to be routed.
var router = this.get('router');
router.router.updateURL(path);
return router.handleURL(path);
},
/**
Replaces the query parameters in the URL. Use no parameters to clear them.
@method replaceQueryParams
**/
queryParams: Em.computed.alias('router.location.queryParams'),
/**
Redirect to a URL.
This has been extracted so it can be tested.
@method redirectTo
**/
redirectTo: function(url) {
window.location = Discourse.getURL(url);
},
/**
@private
If we're viewing more topics, scroll to where we were previously.
@method navigatedToListMore
@param {String} oldPath the previous path we were on
@param {String} path the path we're navigating to
**/
navigatedToListMore: function(oldPath, path) {
// If we transition from a /more path, scroll to the top
if (this.MORE_REGEXP.exec(oldPath) && (oldPath.indexOf(path) === 0)) {
window.scrollTo(0, 0);
}
return false;
},
/**
@private
If the URL is in the topic form, /t/something/:topic_id/:post_number
then we want to apply some special logic. If the post_number changes within the
same topic, use replaceState and instruct our controller to load more posts.
@method navigatedToPost
@param {String} oldPath the previous path we were on
@param {String} path the path we're navigating to
**/
navigatedToPost: function(oldPath, path) {
var newMatches = this.TOPIC_REGEXP.exec(path),
newTopicId = newMatches ? newMatches[2] : null;
@ -99,28 +141,33 @@ Discourse.URL = Em.Object.createWithMixins({
});
// Abort routing, we have replaced our state.
return;
return true;
}
}
// If we transition from a /more path, scroll to the top
if (this.MORE_REGEXP.exec(oldPath) && (oldPath.indexOf(path) === 0)) {
window.scrollTo(0, 0);
}
// Be wary of looking up the router. In this case, we have links in our
// HTML, say form compiled markdown posts, that need to be routed.
var router = this.get('router');
router.router.updateURL(path);
return router.handleURL(path);
return false;
},
/**
Replaces the query parameters in the URL. Use no parameters to clear them.
@private
@method replaceQueryParams
Handle the custom case of routing to the root path from itself.
@param {String} oldPath the previous path we were on
@param {String} path the path we're navigating to
**/
queryParams: Em.computed.alias('router.location.queryParams'),
navigatedToHome: function(oldPath, path) {
var defaultFilter = "/" + Discourse.ListController.filters[0];
if (path === "/" && (oldPath === "/" || oldPath === defaultFilter)) {
// Refresh our list
this.controllerFor('list').refresh();
return true;
}
return false;
},
/**
@private
@ -137,13 +184,27 @@ Discourse.URL = Em.Object.createWithMixins({
/**
@private
Redirect to a URL.
This has been extracted so it can be tested.
Get a handle on the application's router. Note that currently it uses `__container__` which is not
advised but there is no other way to access the router.
@method redirectTo
@property router
**/
redirectTo: function(url) {
window.location = Discourse.getURL(url);
router: function() {
return Discourse.__container__.lookup('router:main');
}.property(),
/**
@private
Get a controller. Note that currently it uses `__container__` which is not
advised but there is no other way to access the router.
@method controllerFor
@param {String} name the name of the controller
**/
controllerFor: function(name) {
return Discourse.__container__.lookup('controller:' + name);
}
});

View File

@ -24,6 +24,19 @@ Discourse.ListController = Discourse.Controller.extend({
});
}.property(),
/**
Refresh our current topic list
@method refresh
**/
refresh: function() {
var listTopicsController = this.get('controllers.listTopics');
listTopicsController.set('model.loaded', false);
this.load(this.get('filterMode')).then(function (topicList) {
listTopicsController.set('model', topicList);
});
},
/**
Load a list based on a filter

View File

@ -31,7 +31,7 @@ Discourse.FilteredListRoute = Discourse.Route.extend({
var listTopicsController = this.controllerFor('listTopics');
listController.set('filterMode', this.filter);
var listContent = listTopicsController.get('content');
var listContent = listTopicsController.get('model');
if (listContent) {
listContent.set('loaded', false);
}
@ -39,7 +39,7 @@ Discourse.FilteredListRoute = Discourse.Route.extend({
listController.load(this.filter).then(function(topicList) {
listController.set('category', null);
listController.set('canCreateTopic', topicList.get('can_create_topic'));
listTopicsController.set('content', topicList);
listTopicsController.set('model', topicList);
});
}
});

View File

@ -31,7 +31,6 @@ task 'integration:create_fixtures' => :environment do
File.write(filename, content)
end
end

View File

@ -70,6 +70,7 @@ d.write('<style>#ember-testing-container { position: absolute; background: white
Discourse.rootElement = '#ember-testing';
Discourse.setupForTesting();
Discourse.injectTestHelpers();
Discourse.bindDOMEvents();
Discourse.Router.map(function() {