Skip to content

Instantly share code, notes, and snippets.

@xbill82
Last active August 29, 2015 14:18
Show Gist options
  • Save xbill82/43a7a0645a2b0f008fe5 to your computer and use it in GitHub Desktop.
Save xbill82/43a7a0645a2b0f008fe5 to your computer and use it in GitHub Desktop.
Store and Promises
define(['require', 'marionette', 'App', 'collections/Gigs'],
function (require) {
'use strict';
var App = require('App'),
Marionette = require('marionette'),
Gigs = require('collections/Gigs');
var GigStore = Marionette.Controller.extend({
initialize: function() {
var that = this;
App.reqres.setHandler('store:gigs:getRecentUpcoming', function() {
return that.getRecentUpcomingGigs();
});
App.reqres.setHandler('store:gigs:getUpcoming', function() {
return that.getUpcomingGigs();
});
App.reqres.setHandler('store:gigs:getOld', function() {
return that.getOldGigs();
});
},
getRecentUpcomingGigs: function() {
var gigs = new Gigs();
var deferred = $.Deferred();
gigs.setSome();
gigs.setLimit(5);
gigs.fetch({
success: function(collection, response, options) {
deferred.resolve(collection);
},
error: function(collection, response, options) {
deferred.reject();
},
});
return deferred.promise();
},
getUpcomingGigs: function() {
var gigs = new Gigs();
var deferred = $.Deferred();
gigs.setUpcoming();
gigs.fetch({
success: function(collection, response, options) {
deferred.resolve(collection);
},
error: function(collection, response, options) {
deferred.reject();
},
});
return deferred.promise();
},
getOldGigs: function() {
var gigs = new Gigs();
var deferred = $.Deferred();
gigs.setOld();
gigs.fetch({
success: function(collection, response, options) {
deferred.resolve(collection);
},
error: function(collection, response, options) {
deferred.reject();
},
});
return deferred.promise();
},
});
return new GigStore();
});
define( [ 'require', 'App', 'marionette', 'handlebars', 'views/calendar/RecentUpcomingGigsView',
'views/show/ShowsListView', "views/guestbook/PicksView", 'text!./templates/home.html',
"handlebars-helpers-my", 'Constants'],
function(require) {
var Marionette = require('marionette'),
App = require('App'),
Handlebars = require('handlebars'),
HomeCalendarView = require('views/calendar/RecentUpcomingGigsView'),
ShowsListView = require('views/show/ShowsListView'),
GuestbookPicksView = require("views/guestbook/PicksView"),
Constants = require('Constants'),
template = require('text!./templates/home.html');
return Marionette.LayoutView.extend( {
template: Handlebars.compile(template),
regions: {
calendar: '.calendar .table-responsive',
shows: '.shows .row',
guestbook: '.guestbook-picks'
},
onShow: function(e) {
this.renderCalendar();
this.renderShows();
this.renderGuestbookPicks();
},
renderCalendar: function() {
var spinner = new Spinner(Constants.SPINNER_TINY).spin();
$(this.calendar.$el.selector).append(spinner.el);
var fetchingGigs = App.reqres.request('store:gigs:getRecentUpcoming');
var that = this;
$.when(fetchingGigs)
.done(function(fetchedGigs) {
that.calendar.show(new HomeCalendarView({
collection: fetchedGigs
}));
})
.fail(function() {
require(['views/calendar/FetchFailView'], function(FetchFail) {
that.calendar.show(new FetchFail());
});
});
},
renderShows: function() {
this.shows.show(new ShowsListView({collection: App.shows}));
},
renderGuestbookPicks: function() {
this.guestbook.show(new GuestbookPicksView());
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment