Skip to content

Instantly share code, notes, and snippets.

@erichocean
Created April 21, 2010 22:09
Show Gist options
  • Save erichocean/374466 to your computer and use it in GitHub Desktop.
Save erichocean/374466 to your computer and use it in GitHub Desktop.
Created by
----------
In our schema, we'd like to know who created a specific ``Task``. In hub.js,
users are represented as instances of the ``hub.User`` class (or a subclass),
and there is an API call to get the ``hub.User`` that created a particular
record, so we'll create a computed property that calls that::
TaskList.Task = hub.Record.extend({
isDone: hub.attr(Boolean, { default: false }),
description: hub.attr(String, { default: "" }),
createdBy: function(key, val) {
if (val) hub.debug("You can't change the creator of an object.");
return hub.whoCreated(this);
}.property().cacheable()
});
Since this is a pretty standard thing to do, hub.js provides a pre-existing
function that does just that::
TaskList.Task = hub.Record.extend({
isDone: hub.attr(Boolean, { default: false }),
description: hub.attr(String, { default: "" }),
createdBy: hub.createdBy
});
And here's the implementation of ``hub.createdBy()``::
hub.createdBy = function(key, val) {
if (val) hub.debug("You can't change the creator of a record.") ;
return hub.whoCreated(this) ;
}.property().cacheable();
A similar function exists for the ``hub.Device`` that the record was created on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment