/public/javascripts/app/models/stream.js
JavaScript | 57 lines | 43 code | 11 blank | 3 comment | 6 complexity | e0c1828a842991c46f10df3aa641cfbb MD5 | raw file
Possible License(s): AGPL-1.0, AGPL-3.0, JSON
- app.models.Stream = Backbone.Collection.extend({
- initialize : function(){
- this.posts = new app.collections.Posts();
- },
- url : function(){
- return _.any(this.posts.models) ? this.timeFilteredPath() : this.basePath()
- },
- _fetching : false,
- fetch: function() {
- if(this._fetching) { return false; }
- var self = this
- // we're fetching the collection... there is probably a better way to do this
- self._fetching = true;
- this.posts
- .fetch({
- add : true,
- url : self.url()
- })
- .done(
- function(resp){
- // we're done fetching... there is probably a better way to handle this
- self._fetching = false;
- self.trigger("fetched", self);
- // all loaded?
- if(resp.posts && (resp.posts.author || resp.posts.length == 0)) {
- self.trigger("allPostsLoaded", self);
- }
- }
- )
- return this;
- },
- basePath : function(){
- return document.location.pathname;
- },
- timeFilteredPath : function(){
- return this.basePath() + "?max_time=" + this.maxTime();
- },
- maxTime: function(){
- var lastPost = _.last(this.posts.models);
- var isOnParticipateStream = this.basePath().match(/participate/);
- return (isOnParticipateStream == null) ? lastPost.createdAt() : lastPost.interactedAt();
- },
- add : function(models){
- this.posts.add(models)
- }
- })