Created
October 17, 2008 00:28
-
-
Save erichocean/17315 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
require('core'); | |
require('models/record'); | |
OI.Folder = OI.Record.extend({ | |
dataSource: SC.Store, | |
_mailboxes: null, | |
init: function() { this._isLoading = false; this._mailboxes = { lastUpdatedAt: null, value: [] }; }, | |
mailboxes: function() { | |
var that = this; | |
var userLastUpdatedAtStr; | |
if ( OI.productionMode ) { | |
var userLastUpdatedAt = OI.userController.get('lastUpdatedAt'); | |
if (userLastUpdatedAt) { | |
userLastUpdatedAtStr = userLastUpdatedAt.toJSON().replace(/"/,''); | |
} | |
if ( this._mailboxes.lastUpdatedAt === null ) { | |
if ( !this._isLoading ) { | |
this._isLoading = true; // give ourselves time to load | |
// we want to refresh any folders that have been loaded, plus this folder | |
// update mailboxes with the current value from the database via an asynchronous AJAX.Request... | |
OI.executeAjax('/refresh', { | |
method: 'get', | |
parameters: { | |
type: 'mailboxes', | |
new_folder_id: this.get('guid'), | |
folder_ids: OI.loadedFolderGuids, | |
mailbox_ids: OI.loadedMailboxGuids, | |
last_updated_at: userLastUpdatedAtStr | |
}, | |
onSuccess: function(transport) { | |
that.propertyWillChange('mailboxes'); | |
OI._parseServerUpdate( transport ); // OI.Mailbox objects are stored in the response | |
that._mailboxes.lastUpdatedAt = OI.userController.get('lastUpdatedAt'); // updated by OI._parseServerUpdate | |
that._mailboxes.value = that._sortedMailboxes(); | |
that.propertyDidChange('mailboxes'); | |
OI.loadedFolderGuids.push(that.get('guid')); | |
that._isLoading = false; | |
}, | |
onFailure: function() { | |
that._isLoading = false; | |
that._tries = that._tries ? that._tries + 1 : 1; | |
if ( that._tries < 3 ) { | |
that.notifyPropertyChange('messages'); // will trigger a re-try (needs testing) | |
} else { | |
// FIXME: notify user there was an error loading the mailboxes | |
console.log( "Failed to load mailboxes for '%@' folder.".fmt(that.get('name'))); | |
} | |
} | |
}); | |
} | |
} else if ( this._mailboxes.lastUpdatedAt < userLastUpdatedAt) { | |
// update mailboxes with the current value from the the store immediately | |
this.propertyWillChange('mailboxes'); | |
this._mailboxes.lastUpdatedAt = userLastUpdatedAt; | |
this._mailboxes.value = this._sortedMailboxes(); | |
this.propertyDidChange('mailboxes'); | |
} | |
} else { | |
this._mailboxes.value = this._sortedMailboxes(); // dev-mode only | |
} | |
return this._mailboxes.value ; // initially, value is [] | |
}.property(), | |
refresh: function() { | |
this.notifyPropertyChange('mailboxes'); | |
}, | |
decrementUnreadBy: function(count) { | |
var new_count = this.get('count') - count; | |
this.set('count',new_count > 0 ? new_count : null); | |
}, | |
flush: function () { | |
var ary = this._mailboxes.value; | |
var len = ary.length; | |
// remove our mailboxes from the store | |
for ( var idx = 0; idx < len; idx++ ) { | |
ary[idx].flush(); | |
} | |
this.init(); // start from scratch | |
}, | |
hasMailboxes: function() { | |
var mailboxes = this.get('mailboxes'); | |
if (mailboxes && mailboxes.length > 0) { return true; } | |
}.property('mailboxes'), | |
reload: function() { | |
this.flush(); | |
this.refresh(); | |
}, | |
_sortedMailboxes: function() { | |
var unsortedAry = OI.Mailbox.findAll({ folder: this.get('guid'), hasMessages: true }); | |
var order = ['priority DESC', 'lowerCaseName']; | |
return unsortedAry.sort(function(a,b) { return a.compareTo(b,order); }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment