Created
August 26, 2012 12:46
-
-
Save kim3er/3478709 to your computer and use it in GitHub Desktop.
Knockout with Sammy (http://learn.knockoutjs.com/#/?tutorial=webmail)
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
function WebmailViewModel() { | |
// Data | |
var self = this; | |
self.folders = ['Inbox', 'Archive', 'Sent', 'Spamh']; | |
self.chosenFolderId = ko.observable(); | |
self.chosenFolderData = ko.observable(); | |
self.chosenMailData = ko.observable(); | |
// Behaviours | |
self.goToFolder = function(folder) { location.hash = folder }; | |
self.goToMail = function(mail) { location.hash = mail.folder + '/' + mail.id }; | |
// Client-side routes | |
Sammy(function() { | |
this.get('#:folder', function() { | |
self.chosenFolderId(this.params.folder); | |
self.chosenMailData(null); | |
$.get("/mail", { folder: this.params.folder }, self.chosenFolderData); | |
}); | |
this.get('#:folder/:mailId', function() { | |
self.chosenFolderId(this.params.folder); | |
self.chosenFolderData(null); | |
$.get("/mail", { mailId: this.params.mailId }, self.chosenMailData); | |
}); | |
this.get('', function() { this.app.runRoute('get', '#Inbox') }); | |
}).run(); | |
}; | |
ko.applyBindings(new WebmailViewModel()); |
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
<script src="/scripts/lib/sammy.js" type="text/javascript"></script> | |
<!-- Folders --> | |
<ul class="folders" data-bind="foreach: folders"> | |
<li data-bind="text: $data, | |
css: { selected: $data == $root.chosenFolderId() }, | |
click: $root.goToFolder"></li> | |
</ul> | |
<!-- Mails grid --> | |
<table class="mails" data-bind="with: chosenFolderData"> | |
<thead><tr><th>From</th><th>To</th><th>Subject</th><th>Date</th></tr></thead> | |
<tbody data-bind="foreach: mails"> | |
<tr data-bind="click: $root.goToMail"> | |
<td data-bind="text: from"></td> | |
<td data-bind="text: to"></td> | |
<td data-bind="text: subject"></td> | |
<td data-bind="text: date"></td> | |
</tr> | |
</tbody> | |
<!-- Chosen mail --> | |
<div class="viewMail" data-bind="with: chosenMailData"> | |
<div class="mailInfo"> | |
<h1 data-bind="text: subject"></h1> | |
<p><label>From</label>: <span data-bind="text: from"></span></p> | |
<p><label>To</label>: <span data-bind="text: to"></span></p> | |
<p><label>Date</label>: <span data-bind="text: date"></span></p> | |
</div> | |
<p class="message" data-bind="html: messageContent" /> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!