Created
November 27, 2012 22:28
-
-
Save jonnii/4157592 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
App.Application = Ember.Object.extend({ | |
hasUnread: false, | |
name: null, | |
environment: null, | |
shortEnvironment: function () { | |
var env = this.get('environment'); | |
if (env == 'development') { | |
return 'dev'; | |
} | |
if (env == 'production') { | |
return 'prod'; | |
} | |
return env; | |
}.property('environment'), | |
formattedLastMessage: function () { | |
var when = this.get('last_message'); | |
if (when == null || when.length == 0) { | |
return 'never'; | |
} | |
var date = new Date(when); | |
return moment(date).fromNow(); | |
}.property('last_message'), | |
matches: function (filter) { | |
var name = this.get('name').toLowerCase(); | |
if (filter.applications.length != 0) { | |
var nameMatches = filter.applications.find(function (a) { | |
return name.indexOf(a) >= 0; | |
}); | |
if (nameMatches == null) { | |
return false; | |
} | |
} | |
if (filter.environments.length == 0) { | |
return true; | |
} | |
var environment = this.get('environment').toLowerCase(); | |
return filter.environments.contains(environment.toLowerCase()); | |
}, | |
markUnread: function () { | |
this.set('hasUnread', true); | |
}, | |
markRead: function () { | |
this.set('hasUnread', false); | |
} | |
}); | |
App.Application.reopenClass({ | |
applications: Em.A([]), | |
findAll: function () { | |
if (this.applications.get('isLoaded')) { | |
return this.applications; | |
} | |
var that = this; | |
$.getJSON('/api/applications', function (data) { | |
data.forEach(function (d) { | |
if (that.get(d.id)) { | |
return; | |
} | |
var application = App.Application.create(d); | |
application.set('isLoaded', true); | |
that.applications.pushObject(application); | |
}); | |
that.applications.set('isLoaded', true); | |
}); | |
return this.applications; | |
}, | |
get: function (id) { | |
return this.applications.find(function (a) { | |
return a.id == id; | |
}); | |
}, | |
find: function (id) { | |
var existing = this.get(id); | |
if (existing) { | |
return existing; | |
} | |
var that = this; | |
var application = App.Application.create({ id: id }); | |
$.getJSON('/api/applications/' + id, function (d) { | |
application.setProperties(d); | |
application.set('isLoaded', true); | |
that.applications.pushObject(application); | |
}); | |
return application; | |
}, | |
destroy: function (application) { | |
this.applications.removeObject(application); | |
$.ajax({ | |
url: '/api/applications/' + application.id, | |
type: "DELETE", | |
success: function () { | |
console.log("application deleted"); | |
} | |
}); | |
}, | |
subscribe: function (connection) { | |
console.log(" -> subscribing to application updates"); | |
var subscriber = connection.applicationHub; | |
var that = this; | |
subscriber.client.applicationUpdated = function (data) { | |
data.forEach(function (d) { | |
var persisted = that.find(d.id); | |
persisted.set('last_message', d.last_message); | |
persisted.set('total_messages', d.total_messages); | |
persisted.markUnread(); | |
}); | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment