blossom
Version:
Modern, Cross-Platform Application Framework
92 lines (74 loc) • 3.11 kB
JavaScript
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// Portions ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
/*globals module test ok equals same */
var Mail;
suite("Sample Model from a webmail app", {
setup: function() {
// namespace
Mail = SC.Object.create({
store: SC.Store.create()
});
// Messages are stored in mailboxes.
Mail.Mailbox = SC.Record.extend({
name: SC.Record.attr(String, {
isRequired: true
}),
// here is the mailbox type. must be one of INBOX, TRASH, OTHER
mailbox: SC.Record.attr(String, {
isRequired: true,
only: 'INBOX TRASH OTHER'.w()
}),
// this is the sortKey that should be used to order the mailbox.
sortKey: SC.Record.attr(String, {
isRequired: true,
only: 'subject date from to'.w()
}),
// load the list of messages. We use the mailbox guid to load the
// messages. Messages use a foreign key to move the message around.
// an edit should cause this fetched property to reload.
//
// when you get messages, it will fetch "mailboxMessages" from the
// owner store, passing the receiver as the param unless you implement
// the mailboxMessageParams property.
messages: SC.Record.fetch('Mail.Message')
});
// A message has a subject, date, sender, mailboxes, and messageDetail
// which is a to-one relationship. mailboxes is kept as an array of
// guids.
Mail.Message = SC.Record.extend({
date: SC.Record.attr(Date, { isRequired: true }),
mailboxes: SC.Record.toMany('Mail.Mailbox', {
inverse: 'messages',
isMaster: true,
minimum: 1 // you cannot have less than one mailbox.
}),
// describe the message detail.
messageDetail: SC.Record.toOne('Mail.MessageDetail', {
inverse: "message", // MessageDetail.message should == this.primaryKey
isDependent: true
}),
// access the named property through another property.
body: SC.Record.through('messageDetail'),
cc: SC.Record.through('messageDetail'),
bcc: SC.Record.through('messageDetail'),
subject: SC.Record.through('messageDetail')
});
Mail.Contact = SC.Record.extend({
firstName: SC.Record.attr(String),
lastName: SC.Record.attr(String),
email: SC.Record.attr(String)
});
// define server. RestServer knows how to automatically save records to
// the server. You need to define your fetch requests here though.
Mail.server = SC.RestServer.create({
// fetch request for mailboxes.
fetchMailboxes: function(params) {
return this.fetchRequest('/ma/mailboxes?alt=json') ;
}
});
}
});