Created
May 14, 2013 17:46
-
-
Save ericf/5577957 to your computer and use it in GitHub Desktop.
A snippet from rsvp.js from my wedding site.
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
Y.Model.prototype.promiseSave = function (options) { | |
var model = this; | |
return new Y.Promise(function (resolve, reject) { | |
model.save(options, function (err, res) { | |
return err ? reject(err) : resolve(res); | |
}); | |
}); | |
}; | |
var app = new Y.App({ | |
container : '#main', | |
viewContainer : '#main', | |
contentSelector: '#main', | |
linkSelector : null, | |
transitions: true, | |
root : '/rsvp/', | |
views: { | |
rsvp : {type: Y.RsvpView}, | |
attending : {type: Y.AttendingView}, | |
notAttending: {type: Y.NotAttendingView} | |
} | |
}); | |
app.invitation = new Y.Invitation(YUI.Env.LE.invitation); | |
app.initialContent = Y.one('#main > [data-view]'); | |
app.rsvp = function (isAttending) { | |
var guests = this.invitation.get('guests'), | |
saves = [app.updateInvitation({rsvpd: true, send_paper: false})]; | |
guests.invoke('set', 'is_attending', isAttending); | |
saves.push.apply(saves, guests.invoke('promiseSave')); | |
Y.batch.apply(null, saves).then(function () { | |
app.replace(''); | |
}); | |
}; | |
app.updateInvitation = function (updates) { | |
this.invitation.setAttrs(updates); | |
if (this.invitation.isModified()) { | |
return this.invitation.promiseSave(); | |
} | |
return Y.when(true); | |
}; | |
app.on('*:rsvp', function (e) { | |
this.rsvp(e.isAttending); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment