forked from thomasdavis/backbonetutorials
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
663d44f
commit 3cda528
Showing
7 changed files
with
181 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
define([ | ||
'jquery', | ||
'underscore', | ||
'backbone', | ||
'vm', | ||
'collections/twitter', | ||
'text!templates/twitter/list.html' | ||
], function($, _, Backbone, Vm, TwitterCollection, TwitterListTemplate){ | ||
var TwitterWidget = Backbone.View.extend({ | ||
el: '.twitter-widget', | ||
initialize: function () { | ||
// isLoading is a useful flag to make sure we don't send off more than | ||
// one request at a time | ||
this.isLoading = false; | ||
this.twitterCollection = new TwitterCollection(); | ||
}, | ||
render: function () { | ||
this.loadResults(); | ||
}, | ||
loadResults: function () { | ||
var that = this; | ||
// we are starting a new load of results so set isLoading to true | ||
this.isLoading = true; | ||
// fetch is Backbone.js native function for calling and parsing the collection url | ||
this.twitterCollection.fetch({ | ||
success: function (tweets) { | ||
// Once the results are returned lets populate our template | ||
$(that.el).append(_.template(TwitterListTemplate, {tweets: tweets.models, _:_})); | ||
// Now we have finished loading set isLoading back to false | ||
that.isLoading = false; | ||
} | ||
}); | ||
}, | ||
// This will simply listen for scroll events on the current el | ||
events: { | ||
'scroll': 'checkScroll' | ||
}, | ||
checkScroll: function () { | ||
var triggerPoint = 100; // 100px from the bottom | ||
if( !this.isLoading && this.el.scrollTop + this.el.clientHeight + triggerPoint > this.el.scrollHeight ) { | ||
this.twitterCollection.page += 1; // Load next page | ||
this.loadResults(); | ||
} | ||
} | ||
}); | ||
return TwitterWidget; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters