Skip to content

Commit

Permalink
#68 ⭐ standardizing model for storing states
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Sep 18, 2015
1 parent 246f5ca commit 89372bf
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/app.view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
(function(_, Backbone, $, APP) {
// containers
var state = Backbone.View.prototype.state || new Backbone.Model();
// defaults
state.set({
loaded : false,
scroll : false,
visible : false
});

APP.View = Backbone.View.extend({
options : {
Expand All @@ -23,11 +31,9 @@
states: {
"scroll": "_scroll"
},
state: {
loaded : false,
scroll : false,
visible : false
},

state: state,

initialize: function( options ){
var self = this;
// fallback
Expand Down Expand Up @@ -224,7 +230,7 @@
if( !this.options.data || (this.options.data && !_.isEmpty(this._toJSON()) ) ){
$(this.el).removeClass("loading");
// set the appropriate flag
this.state.loaded = true;
this.state.set("loaded", true);
// bubble up the event
this.trigger("loaded");
}
Expand Down Expand Up @@ -306,7 +312,7 @@

//
_scroll: function () {
//this.state.scroll = true;
//this.state.set("scroll", true);
},
// checks if the view is visible
isVisible: function(){
Expand All @@ -327,13 +333,13 @@
// condition
var visible = ( (elementOffset.top >= minTop && elementOffset.top < maxTop) && (elementOffset.left >= minLeft && elementOffset.left < maxLeft) );
// trigger state if needed
if( visible && !this.state.visible ){
if( visible && !this.state.get("visible") ){
this.trigger("visible");
} else {
this.trigger("hidden");
}
// save state for later...
this.state.visible = visible;
this.state.set("visible", visible);

return visible;
}
Expand Down

0 comments on commit 89372bf

Please sign in to comment.