framework/ember/app/controllers/discussion.js

104 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-12-20 14:26:46 +08:00
import Ember from 'ember';
2015-01-07 14:53:27 +08:00
import Stream from '../models/stream';
2014-12-20 14:26:46 +08:00
export default Ember.ObjectController.extend(Ember.Evented, {
needs: ['application', 'composer'],
queryParams: ['start'],
start: '1',
searchQuery: '',
loaded: false,
2015-01-07 14:53:27 +08:00
stream: null,
2014-12-20 14:26:46 +08:00
setup: function(discussion) {
this.set('model', discussion);
// Set up the post stream object. It needs to know about the discussion
// its representing the posts for, and we also need to inject the Ember
// data store.
2015-01-07 14:53:27 +08:00
var stream = Stream.create();
stream.set('discussion', discussion);
stream.set('store', this.get('store'));
this.set('stream', stream);
2014-12-20 14:26:46 +08:00
// Next, we need to load a list of the discussion's post IDs into the
// post stream object. If we don't already have this information, we'll
// need to reload the discussion model.
var promise = discussion.get('posts') ? Ember.RSVP.resolve(discussion) : discussion.reload();
// When we know we have the post IDs, we can set up the post stream with
// them. Then we're ready to load some posts!
var controller = this;
promise.then(function(discussion) {
2015-01-07 14:53:27 +08:00
stream.setup(discussion.get('postIds'));
2014-12-20 14:26:46 +08:00
controller.set('loaded', true);
controller.send('jumpToNumber', controller.get('start'));
});
},
actions: {
reply: function() {
this.set('controllers.composer.showing', true);
this.set('controllers.composer.title', 'Replying to <em>'+this.get('model.title')+'</em>');
},
jumpToNumber: function(number) {
// In some instances, we might be given a placeholder start index
// value. We need to convert this into a numerical value.
switch (number) {
case 'last':
number = this.get('model.lastPostNumber');
break;
case 'unread':
number = this.get('model.readNumber') + 1;
break;
}
number = Math.max(number, 1);
// Let's start by telling our listeners that we're going to load
// posts near this number. The discussion view will listen and
// consequently scroll down to the appropriate position in the
// discussion.
this.trigger('loadingNumber', number);
// Now we have to actually make sure the posts around this new start
// position are loaded. We will tell our listeners when they are.
// Again, the view will scroll down to the appropriate post.
var controller = this;
2015-01-07 14:53:27 +08:00
this.get('stream').loadNearNumber(number).then(function() {
2014-12-20 14:26:46 +08:00
Ember.run.scheduleOnce('afterRender', function() {
controller.trigger('loadedNumber', number);
});
});
},
jumpToIndex: function(index) {
// Let's start by telling our listeners that we're going to load
// posts at this index. The discussion view will listen and
// consequently scroll down to the appropriate position in the
// discussion.
this.trigger('loadingIndex', index);
// Now we have to actually make sure the posts around this index are
// loaded. We will tell our listeners when they are. Again, the view
// will scroll down to the appropriate post.
var controller = this;
2015-01-07 14:53:27 +08:00
this.get('stream').loadNearIndex(index).then(function() {
2014-12-20 14:26:46 +08:00
Ember.run.scheduleOnce('afterRender', function() {
controller.trigger('loadedIndex', index);
});
});
},
loadRange: function(start, end, backwards) {
2015-01-07 14:53:27 +08:00
this.get('stream').loadRange(start, end, backwards);
2014-12-20 14:26:46 +08:00
}
}
});