Created
August 11, 2015 08:10
-
-
Save i-van/c564bfaf5ec04fb88936 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Route from './../core/route'; | |
import Application from '../core/application'; | |
import Auth from './../services/auth.js'; | |
import Products from '../models/products'; | |
import OwnedProducts from '../models/owned_products'; | |
import ClosetView from '../views/closet.js'; | |
export default Route.extend({ | |
ownedProducts: null, | |
busy: false, | |
initialize: function() { | |
this.ownedProducts = new OwnedProducts(); | |
this.ownedProducts.products = new Products(); | |
}, | |
run: function() { | |
if (!Auth.isLoggedIn()) { | |
return Application.getInstance().Router.back(); | |
} | |
var closet = new ClosetView({ collection: this.ownedProducts }) | |
, app = Application.getInstance(); | |
app.closetLayout.render(); | |
closet.on('scroll_at_bottom', () => { | |
this.fetchMore(); | |
}); | |
app.closetLayout.content.show(closet); | |
if (!this.ownedProducts.length) { | |
this.fetchMore(); | |
} | |
}, | |
fetchMore: function() { | |
if (this.busy) { | |
return; | |
} | |
var ownedProducts = new OwnedProducts(); | |
ownedProducts.limit = this.ownedProducts.limit; | |
ownedProducts.offset = this.ownedProducts.offset; | |
ownedProducts.products = this.ownedProducts.products; | |
this.busy = true; | |
ownedProducts.fetchMore() | |
.then(() => { | |
var ids = ownedProducts.map(item => item.get('productId')).join(','); | |
return ownedProducts.products.fetch({ | |
add: true, | |
remove: false, | |
merge: true, | |
data: { id: ids } | |
}); | |
}) | |
.then(() => { | |
this.ownedProducts.add(ownedProducts.models, { | |
add: true, | |
remove: false, | |
merge: true | |
}); | |
//clean | |
ownedProducts.limit = null; | |
ownedProducts.offset = null; | |
ownedProducts.products = null; | |
ownedProducts = null; | |
this.busy = false; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment